simplify error handling

This commit is contained in:
2024-05-15 21:28:17 -06:00
parent 93cfdcc3ba
commit aabd624df9

View File

@ -83,20 +83,28 @@ static void bt_ready(int err)
int main(void)
{
const struct device *dev;
const struct device *display, *ina, *baro, *imu, *lis, *hdc;
int err;
uint16_t x_res;
uint16_t y_res;
uint16_t rows;
uint8_t ppt;
uint8_t font_width;
uint8_t font_height;
struct sensor_value voltage, current, pressure, temperature, accel_x, accel_y, accel_z, accel_z_ref, humidity;
int err;
char str_v[15] = {0};
char str_i[15] = {0};
char str_p[16] = {0};
char str_t[16] = {0};
char str_ax[16] = {0};
char str_ay[16] = {0};
char str_az[16] = {0};
char str_az_ref[16] = {0};
char str_h[16] = {0};
printk("Starting Mellifera version %s...\n", APP_VERSION_STRING);
// printk("Board: %s\n", BOARD);
LOG_INF("Starting Beacon Demo");
LOG_INF("Starting Mellifera version %s...", APP_VERSION_STRING);
// LOG_INF("Board: %s", BOARD);
// Initialize the Bluetooth Subsystem
err = bt_enable(bt_ready);
@ -105,61 +113,59 @@ int main(void)
LOG_ERR("Bluetooth init failed (err %d)", err);
}
dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_display));
if (!device_is_ready(dev))
display = DEVICE_DT_GET(DT_CHOSEN(zephyr_display));
if (!device_is_ready(display))
{
LOG_ERR("Device %s not ready", dev->name);
LOG_ERR("Device %s not ready", display->name);
return 0;
}
if (display_set_pixel_format(dev, PIXEL_FORMAT_MONO10) != 0)
if (display_set_pixel_format(display, PIXEL_FORMAT_MONO10) != 0)
{
if (display_set_pixel_format(dev, PIXEL_FORMAT_MONO01) != 0)
if (display_set_pixel_format(display, PIXEL_FORMAT_MONO01) != 0)
{
LOG_ERR("Failed to set required pixel format");
return 0;
}
}
LOG_INF("Initialized %s", dev->name);
LOG_INF("Initialized %s", display->name);
if (cfb_framebuffer_init(dev))
if (cfb_framebuffer_init(display))
{
LOG_ERR("Framebuffer initialization failed!");
return 0;
}
cfb_framebuffer_clear(dev, true);
cfb_framebuffer_clear(display, true);
display_blanking_off(dev);
display_blanking_off(display);
x_res = cfb_get_display_parameter(dev, CFB_DISPLAY_WIDTH);
y_res = cfb_get_display_parameter(dev, CFB_DISPLAY_HEIGH);
rows = cfb_get_display_parameter(dev, CFB_DISPLAY_ROWS);
ppt = cfb_get_display_parameter(dev, CFB_DISPLAY_PPT);
x_res = cfb_get_display_parameter(display, CFB_DISPLAY_WIDTH);
y_res = cfb_get_display_parameter(display, CFB_DISPLAY_HEIGH);
rows = cfb_get_display_parameter(display, CFB_DISPLAY_ROWS);
ppt = cfb_get_display_parameter(display, CFB_DISPLAY_PPT);
for (int idx = 0; idx < 42; idx++)
{
if (cfb_get_font_size(dev, idx, &font_width, &font_height))
if (cfb_get_font_size(display, idx, &font_width, &font_height))
{
break;
}
cfb_framebuffer_set_font(dev, idx);
printf("font width %d, font height %d\n",
font_width, font_height);
cfb_framebuffer_set_font(display, idx);
LOG_INF("font width %d, font height %d",
font_width, font_height);
}
cfb_framebuffer_set_font(dev, 0);
cfb_framebuffer_set_font(display, 0);
printf("x_res %d, y_res %d, ppt %d, rows %d, cols %d\n",
x_res,
y_res,
ppt,
rows,
cfb_get_display_parameter(dev, CFB_DISPLAY_COLS));
LOG_INF("x_res %d, y_res %d, ppt %d, rows %d, cols %d",
x_res,
y_res,
ppt,
rows,
cfb_get_display_parameter(display, CFB_DISPLAY_COLS));
cfb_set_kerning(dev, 3);
const struct device *ina, *bmp, *bmx, *lis, *hdc;
cfb_set_kerning(display, 3);
ina = DEVICE_DT_GET(DT_NODELABEL(ina231));
if (!device_is_ready(ina))
@ -168,20 +174,20 @@ int main(void)
return 0;
}
bmp = DEVICE_DT_GET(DT_NODELABEL(bmp388));
if (!device_is_ready(bmp))
baro = DEVICE_DT_GET(DT_NODELABEL(bmp388));
if (!device_is_ready(baro))
{
LOG_ERR("Device %s not ready", bmp->name);
LOG_ERR("Device %s not ready", baro->name);
return 0;
}
bmx = DEVICE_DT_GET(DT_NODELABEL(bmx055));
if (!device_is_ready(bmx))
imu = DEVICE_DT_GET(DT_NODELABEL(bmx055));
if (!device_is_ready(imu))
{
LOG_ERR("Device %s not ready", bmx->name);
LOG_ERR("Device %s not ready", imu->name);
return 0;
}
LOG_INF("Initialized %s", bmx->name);
LOG_INF("Initialized %s", imu->name);
lis = DEVICE_DT_GET(DT_NODELABEL(lis2dh));
if (!device_is_ready(lis))
@ -200,117 +206,73 @@ int main(void)
while (1)
{
int ret;
struct sensor_value voltage, current, pressure, temperature, accel_x, accel_y, accel_z, accel_z_ref, humidity;
char str_v[15] = {0};
char str_i[15] = {0};
char str_p[16] = {0};
char str_t[16] = {0};
char str_ax[16] = {0};
char str_ay[16] = {0};
char str_az[16] = {0};
char str_az_ref[16] = {0};
char str_h[16] = {0};
ret = sensor_sample_fetch(ina);
if (ret < 0)
if (sensor_sample_fetch(ina) < 0)
{
LOG_ERR("Could not fetch sample (%d)", ret);
return 0;
LOG_ERR("Could not fetch sample");
}
ret = sensor_channel_get(ina, SENSOR_CHAN_VOLTAGE, &voltage);
if (ret < 0)
if (sensor_channel_get(ina, SENSOR_CHAN_VOLTAGE, &voltage) < 0)
{
LOG_ERR("Could not get voltage (%d)", ret);
return 0;
LOG_ERR("Could not get voltage");
}
ret = sensor_channel_get(ina, SENSOR_CHAN_CURRENT, &current);
if (ret < 0)
if (sensor_channel_get(ina, SENSOR_CHAN_CURRENT, &current) < 0)
{
LOG_ERR("Could not get current (%d)", ret);
return 0;
LOG_ERR("Could not get current");
}
ret = sensor_sample_fetch(bmp);
if (ret < 0)
if (sensor_sample_fetch(baro) < 0)
{
LOG_ERR("Could not fetch sample (%d)", ret);
return 0;
LOG_ERR("Could not fetch sample");
}
ret = sensor_channel_get(bmp, SENSOR_CHAN_PRESS, &pressure);
if (ret < 0)
if (sensor_channel_get(baro, SENSOR_CHAN_PRESS, &pressure) < 0)
{
LOG_ERR("Could not get pressure (%d)", ret);
return 0;
LOG_ERR("Could not get pressure");
}
// ret = sensor_channel_get(bmp, SENSOR_CHAN_AMBIENT_TEMP, &temperature);
// if (ret < 0)
// if (sensor_channel_get(baro, SENSOR_CHAN_AMBIENT_TEMP, &temperature) < 0)
// {
// LOG_ERR("Could not get temperature (%d)", ret);
// return 0;
// LOG_ERR("Could not get temperature");
// }
ret = sensor_sample_fetch(bmx);
if (ret < 0)
if (sensor_sample_fetch(imu) < 0)
{
LOG_ERR("Could not fetch sample (%d)", ret);
return 0;
LOG_ERR("Could not fetch sample from imu");
}
ret = sensor_channel_get(bmx, SENSOR_CHAN_DIE_TEMP, &temperature);
if (ret < 0)
if (sensor_channel_get(imu, SENSOR_CHAN_DIE_TEMP, &temperature) < 0)
{
LOG_ERR("Could not get temperature (%d)", ret);
return 0;
LOG_ERR("Could not get temperature");
}
ret = sensor_channel_get(bmx, SENSOR_CHAN_ACCEL_X, &accel_x);
if (ret < 0)
if (sensor_channel_get(imu, SENSOR_CHAN_ACCEL_X, &accel_x) < 0)
{
LOG_ERR("Could not get acceleration (%d)", ret);
return 0;
LOG_ERR("Could not get acceleration");
}
ret = sensor_channel_get(bmx, SENSOR_CHAN_ACCEL_Y, &accel_y);
if (ret < 0)
if (sensor_channel_get(imu, SENSOR_CHAN_ACCEL_Y, &accel_y) < 0)
{
LOG_ERR("Could not get acceleration (%d)", ret);
return 0;
LOG_ERR("Could not get acceleration");
}
ret = sensor_channel_get(bmx, SENSOR_CHAN_ACCEL_Z, &accel_z);
if (ret < 0)
if (sensor_channel_get(imu, SENSOR_CHAN_ACCEL_Z, &accel_z) < 0)
{
LOG_ERR("Could not get acceleration (%d)", ret);
return 0;
LOG_ERR("Could not get acceleration");
}
ret = sensor_sample_fetch(lis);
if (ret < 0)
if (sensor_sample_fetch(lis) < 0)
{
LOG_ERR("Could not fetch sample (%d)", ret);
return 0;
LOG_ERR("Could not fetch sample");
}
ret = sensor_channel_get(lis, SENSOR_CHAN_ACCEL_Z, &accel_z_ref);
if (ret < 0)
if (sensor_channel_get(lis, SENSOR_CHAN_ACCEL_Z, &accel_z_ref) < 0)
{
LOG_ERR("Could not get acceleration (%d)", ret);
return 0;
LOG_ERR("Could not get acceleration");
}
ret = sensor_sample_fetch(hdc);
if (ret < 0)
if (sensor_sample_fetch(hdc) < 0)
{
LOG_ERR("Could not fetch sample (%d)", ret);
return 0;
LOG_ERR("Could not fetch sample");
}
ret = sensor_channel_get(hdc, SENSOR_CHAN_AMBIENT_TEMP, &temperature);
if (ret < 0)
if (sensor_channel_get(hdc, SENSOR_CHAN_AMBIENT_TEMP, &temperature) < 0)
{
LOG_ERR("Could not get sample (%d)", ret);
return 0;
LOG_ERR("Could not get sample");
}
ret = sensor_channel_get(hdc, SENSOR_CHAN_HUMIDITY, &humidity);
if (ret < 0)
if (sensor_channel_get(hdc, SENSOR_CHAN_HUMIDITY, &humidity) < 0)
{
LOG_ERR("Could not get sample (%d)", ret);
return 0;
LOG_ERR("Could not get sample");
}
sprintf(str_v, "V :%7.5f", voltage.val1 + voltage.val2 * 1e-6);
@ -323,31 +285,27 @@ int main(void)
sprintf(str_az_ref, "Zr:%+7.3f", accel_z_ref.val1 + accel_z_ref.val2 * 1e-6);
sprintf(str_h, "H :%7.3f", humidity.val1 + humidity.val2 * 1e-6);
// printf("%s\t%s\t%s\t%s\n", str_v, str_i, str_p, str_t);
// LOG_INF("%s\t%s\t%s\t%s", str_v, str_i, str_p, str_t);
cfb_framebuffer_clear(dev, false);
if (cfb_print(dev, str_t, 0, 0))
cfb_framebuffer_clear(display, false);
if (cfb_print(display, str_t, 0, 0))
{
printf("Failed to print a string\n");
continue;
LOG_ERR("Failed to print a string");
}
if (cfb_print(dev, str_h, 0, 16))
if (cfb_print(display, str_h, 0, 16))
{
printf("Failed to print a string\n");
continue;
LOG_ERR("Failed to print a string");
}
// if (cfb_print(dev, str_az, 0, 16 * 2))
// if (cfb_print(display, str_az, 0, 16 * 2))
// {
// printf("Failed to print a string\n");
// continue;
// LOG_ERR("Failed to print a string");
// }
// if (cfb_print(dev, str_az_ref, 0, 16 * 3))
// if (cfb_print(display, str_az_ref, 0, 16 * 3))
// {
// printf("Failed to print a string\n");
// continue;
// LOG_ERR("Failed to print a string");
// }
cfb_framebuffer_finalize(dev);
cfb_framebuffer_finalize(display);
#if defined(CONFIG_ARCH_POSIX)
k_sleep(K_MSEC(20));
#endif