374 lines
9.3 KiB
C
374 lines
9.3 KiB
C
/*
|
|
* Copyright (c) 2021 Nordic Semiconductor ASA
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
#include <zephyr/kernel.h>
|
|
#include <zephyr/drivers/sensor.h>
|
|
#include <zephyr/logging/log.h>
|
|
#include <zephyr/display/cfb.h>
|
|
|
|
#include <app/drivers/blink.h>
|
|
|
|
#include <app_version.h>
|
|
|
|
#include <zephyr/bluetooth/bluetooth.h>
|
|
#include <zephyr/bluetooth/hci.h>
|
|
|
|
LOG_MODULE_REGISTER(main, CONFIG_APP_LOG_LEVEL);
|
|
|
|
#define BLINK_PERIOD_MS_STEP 100U
|
|
#define BLINK_PERIOD_MS_MAX 1000U
|
|
|
|
#define DEVICE_NAME CONFIG_BT_DEVICE_NAME
|
|
#define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1)
|
|
|
|
/*
|
|
* Set Advertisement data. Based on the Eddystone specification:
|
|
* https://github.com/google/eddystone/blob/master/protocol-specification.md
|
|
* https://github.com/google/eddystone/tree/master/eddystone-url
|
|
*/
|
|
static const struct bt_data ad[] = {
|
|
BT_DATA_BYTES(BT_DATA_FLAGS, BT_LE_AD_NO_BREDR),
|
|
BT_DATA_BYTES(BT_DATA_UUID16_ALL, 0xaa, 0xfe),
|
|
BT_DATA_BYTES(BT_DATA_SVC_DATA16,
|
|
0xaa, 0xfe, /* Eddystone UUID */
|
|
0x10, /* Eddystone-URL frame type */
|
|
0x00, /* Calibrated Tx power at 0m */
|
|
0x00, /* URL Scheme Prefix http://www. */
|
|
'z', 'e', 'p', 'h', 'y', 'r',
|
|
'p', 'r', 'o', 'j', 'e', 'c', 't',
|
|
0x08) /* .org */
|
|
};
|
|
|
|
/* Set Scan Response data */
|
|
static const struct bt_data sd[] = {
|
|
BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),
|
|
};
|
|
|
|
static void bt_ready(int err)
|
|
{
|
|
char addr_s[BT_ADDR_LE_STR_LEN];
|
|
bt_addr_le_t addr = {0};
|
|
size_t count = 1;
|
|
|
|
if (err)
|
|
{
|
|
LOG_ERR("Bluetooth init failed (err %d)", err);
|
|
return;
|
|
}
|
|
|
|
LOG_INF("Bluetooth initialized");
|
|
|
|
// Start advertising
|
|
err = bt_le_adv_start(BT_LE_ADV_NCONN_IDENTITY, ad, ARRAY_SIZE(ad),
|
|
sd, ARRAY_SIZE(sd));
|
|
if (err)
|
|
{
|
|
LOG_ERR("Advertising failed to start (err %d)", err);
|
|
return;
|
|
}
|
|
|
|
/* For connectable advertising you would use
|
|
* bt_le_oob_get_local(). For non-connectable non-identity
|
|
* advertising an non-resolvable private address is used;
|
|
* there is no API to retrieve that.
|
|
*/
|
|
|
|
bt_id_get(&addr, &count);
|
|
bt_addr_le_to_str(&addr, addr_s, sizeof(addr_s));
|
|
|
|
LOG_INF("Beacon started, advertising as %s", addr_s);
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
const struct device *display, *ina, *baro, *accel, *gyro, *mag, *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, gyro_x, gyro_y, gyro_z, mag_x, mag_y, mag_z, 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_gx[16] = {0};
|
|
char str_gy[16] = {0};
|
|
char str_gz[16] = {0};
|
|
char str_mx[16] = {0};
|
|
char str_my[16] = {0};
|
|
char str_mz[16] = {0};
|
|
char str_h[16] = {0};
|
|
|
|
LOG_INF("Starting Mellifera version %s...", APP_VERSION_STRING);
|
|
// LOG_INF("Board: %s", BOARD);
|
|
|
|
// Initialize the Bluetooth Subsystem
|
|
err = bt_enable(bt_ready);
|
|
if (err)
|
|
{
|
|
LOG_ERR("Bluetooth init failed (err %d)", err);
|
|
}
|
|
|
|
display = DEVICE_DT_GET(DT_CHOSEN(zephyr_display));
|
|
if (!device_is_ready(display))
|
|
{
|
|
LOG_ERR("Device %s not ready", display->name);
|
|
return 0;
|
|
}
|
|
|
|
if (display_set_pixel_format(display, PIXEL_FORMAT_MONO10) != 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", display->name);
|
|
|
|
if (cfb_framebuffer_init(display))
|
|
{
|
|
LOG_ERR("Framebuffer initialization failed!");
|
|
return 0;
|
|
}
|
|
|
|
cfb_framebuffer_clear(display, true);
|
|
|
|
display_blanking_off(display);
|
|
|
|
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(display, idx, &font_width, &font_height))
|
|
{
|
|
break;
|
|
}
|
|
cfb_framebuffer_set_font(display, idx);
|
|
LOG_INF("font width %d, font height %d",
|
|
font_width, font_height);
|
|
}
|
|
cfb_framebuffer_set_font(display, 0);
|
|
|
|
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(display, 3);
|
|
|
|
ina = DEVICE_DT_GET(DT_NODELABEL(ina231));
|
|
if (!device_is_ready(ina))
|
|
{
|
|
LOG_ERR("Device %s not ready", ina->name);
|
|
return 0;
|
|
}
|
|
|
|
baro = DEVICE_DT_GET(DT_NODELABEL(bmp388));
|
|
if (!device_is_ready(baro))
|
|
{
|
|
LOG_ERR("Device %s not ready", baro->name);
|
|
return 0;
|
|
}
|
|
|
|
accel = DEVICE_DT_GET(DT_NODELABEL(bmx055_accel));
|
|
if (!device_is_ready(accel))
|
|
{
|
|
LOG_ERR("Device %s not ready", accel->name);
|
|
return 0;
|
|
}
|
|
LOG_INF("Initialized %s", accel->name);
|
|
|
|
gyro = DEVICE_DT_GET(DT_NODELABEL(bmx055_gyro));
|
|
if (!device_is_ready(gyro))
|
|
{
|
|
LOG_ERR("Device %s not ready", gyro->name);
|
|
return 0;
|
|
}
|
|
LOG_INF("Initialized %s", gyro->name);
|
|
|
|
mag = DEVICE_DT_GET(DT_NODELABEL(bmx055_mag));
|
|
if (!device_is_ready(mag))
|
|
{
|
|
LOG_ERR("Device %s not ready", mag->name);
|
|
return 0;
|
|
}
|
|
LOG_INF("Initialized %s", mag->name);
|
|
|
|
lis = DEVICE_DT_GET(DT_NODELABEL(lis2dh));
|
|
if (!device_is_ready(lis))
|
|
{
|
|
LOG_ERR("Device %s not ready", lis->name);
|
|
return 0;
|
|
}
|
|
LOG_INF("Initialized %s", lis->name);
|
|
|
|
hdc = DEVICE_DT_GET(DT_NODELABEL(hdc1080));
|
|
if (!device_is_ready(hdc))
|
|
{
|
|
LOG_ERR("Device %s not ready", hdc->name);
|
|
return 0;
|
|
}
|
|
|
|
while (1)
|
|
{
|
|
if (sensor_sample_fetch(ina) < 0)
|
|
{
|
|
LOG_ERR("Could not fetch sample");
|
|
}
|
|
if (sensor_channel_get(ina, SENSOR_CHAN_VOLTAGE, &voltage) < 0)
|
|
{
|
|
LOG_ERR("Could not get voltage");
|
|
}
|
|
if (sensor_channel_get(ina, SENSOR_CHAN_CURRENT, ¤t) < 0)
|
|
{
|
|
LOG_ERR("Could not get current");
|
|
}
|
|
|
|
if (sensor_sample_fetch(baro) < 0)
|
|
{
|
|
LOG_ERR("Could not fetch sample");
|
|
}
|
|
if (sensor_channel_get(baro, SENSOR_CHAN_PRESS, &pressure) < 0)
|
|
{
|
|
LOG_ERR("Could not get pressure");
|
|
}
|
|
if (sensor_channel_get(baro, SENSOR_CHAN_AMBIENT_TEMP, &temperature) < 0)
|
|
{
|
|
LOG_ERR("Could not get temperature");
|
|
}
|
|
|
|
if (sensor_sample_fetch(accel) < 0)
|
|
{
|
|
LOG_ERR("Could not fetch sample from accel");
|
|
}
|
|
if (sensor_channel_get(accel, SENSOR_CHAN_ACCEL_X, &accel_x) < 0)
|
|
{
|
|
LOG_ERR("Could not get acceleration");
|
|
}
|
|
if (sensor_channel_get(accel, SENSOR_CHAN_ACCEL_Y, &accel_y) < 0)
|
|
{
|
|
LOG_ERR("Could not get acceleration");
|
|
}
|
|
if (sensor_channel_get(accel, SENSOR_CHAN_ACCEL_Z, &accel_z) < 0)
|
|
{
|
|
LOG_ERR("Could not get acceleration");
|
|
}
|
|
|
|
if (sensor_sample_fetch(gyro) < 0)
|
|
{
|
|
LOG_ERR("Could not fetch sample from gyro");
|
|
}
|
|
if (sensor_channel_get(gyro, SENSOR_CHAN_GYRO_X, &gyro_x) < 0)
|
|
{
|
|
LOG_ERR("Could not get gyro");
|
|
}
|
|
if (sensor_channel_get(gyro, SENSOR_CHAN_GYRO_Y, &gyro_y) < 0)
|
|
{
|
|
LOG_ERR("Could not get gyro");
|
|
}
|
|
if (sensor_channel_get(gyro, SENSOR_CHAN_GYRO_Z, &gyro_z) < 0)
|
|
{
|
|
LOG_ERR("Could not get gyro");
|
|
}
|
|
|
|
if (sensor_sample_fetch(mag) < 0)
|
|
{
|
|
LOG_ERR("Could not fetch sample from mag");
|
|
}
|
|
if (sensor_channel_get(mag, SENSOR_CHAN_MAGN_X, &mag_x) < 0)
|
|
{
|
|
LOG_ERR("Could not get mag");
|
|
}
|
|
if (sensor_channel_get(mag, SENSOR_CHAN_MAGN_Y, &mag_y) < 0)
|
|
{
|
|
LOG_ERR("Could not get mag");
|
|
}
|
|
if (sensor_channel_get(mag, SENSOR_CHAN_MAGN_Z, &mag_z) < 0)
|
|
{
|
|
LOG_ERR("Could not get mag");
|
|
}
|
|
|
|
if (sensor_sample_fetch(lis) < 0)
|
|
{
|
|
LOG_ERR("Could not fetch sample");
|
|
}
|
|
if (sensor_channel_get(lis, SENSOR_CHAN_ACCEL_Z, &accel_z_ref) < 0)
|
|
{
|
|
LOG_ERR("Could not get acceleration");
|
|
}
|
|
|
|
if (sensor_sample_fetch(hdc) < 0)
|
|
{
|
|
LOG_ERR("Could not fetch sample");
|
|
}
|
|
if (sensor_channel_get(hdc, SENSOR_CHAN_AMBIENT_TEMP, &temperature) < 0)
|
|
{
|
|
LOG_ERR("Could not get sample");
|
|
}
|
|
if (sensor_channel_get(hdc, SENSOR_CHAN_HUMIDITY, &humidity) < 0)
|
|
{
|
|
LOG_ERR("Could not get sample");
|
|
}
|
|
|
|
sprintf(str_v, "V :%7.5f", voltage.val1 + voltage.val2 * 1e-6);
|
|
sprintf(str_i, "I :%7.5f", current.val1 + current.val2 * 1e-6);
|
|
sprintf(str_p, "P :%7.4f", pressure.val1 + pressure.val2 * 1e-6);
|
|
sprintf(str_t, "T :%7.4f", temperature.val1 + temperature.val2 * 1e-6);
|
|
sprintf(str_ax, "AX:%+7.3f", accel_x.val1 + accel_x.val2 * 1e-6);
|
|
sprintf(str_ay, "AY:%+7.3f", accel_y.val1 + accel_y.val2 * 1e-6);
|
|
sprintf(str_az, "AZ:%+7.3f", accel_z.val1 + accel_z.val2 * 1e-6);
|
|
sprintf(str_gx, "GX:%+7.3f", gyro_x.val1 + gyro_x.val2 * 1e-6);
|
|
sprintf(str_gy, "GY:%+7.3f", gyro_y.val1 + gyro_y.val2 * 1e-6);
|
|
sprintf(str_gz, "GZ:%+7.3f", gyro_z.val1 + gyro_z.val2 * 1e-6);
|
|
sprintf(str_mx, "MX:%+7.3f", mag_x.val1 + mag_x.val2 * 1e-6);
|
|
sprintf(str_my, "MY:%+7.3f", mag_y.val1 + mag_y.val2 * 1e-6);
|
|
sprintf(str_mz, "MZ:%+7.3f", mag_z.val1 + mag_z.val2 * 1e-6);
|
|
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);
|
|
|
|
// LOG_INF("%s\t%s\t%s\t%s", str_v, str_i, str_p, str_t);
|
|
|
|
cfb_framebuffer_clear(display, false);
|
|
if (cfb_print(display, str_mx, 0, 0))
|
|
{
|
|
LOG_ERR("Failed to print a string");
|
|
}
|
|
if (cfb_print(display, str_gy, 0, 16))
|
|
{
|
|
LOG_ERR("Failed to print a string");
|
|
}
|
|
if (cfb_print(display, str_gz, 0, 16 * 2))
|
|
{
|
|
LOG_ERR("Failed to print a string");
|
|
}
|
|
if (cfb_print(display, str_ax, 0, 16 * 3))
|
|
{
|
|
LOG_ERR("Failed to print a string");
|
|
}
|
|
|
|
cfb_framebuffer_finalize(display);
|
|
#if defined(CONFIG_ARCH_POSIX)
|
|
k_sleep(K_MSEC(20));
|
|
#endif
|
|
}
|
|
|
|
return 0;
|
|
}
|