358 lines
8.4 KiB
C
358 lines
8.4 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 *dev;
|
|
uint16_t x_res;
|
|
uint16_t y_res;
|
|
uint16_t rows;
|
|
uint8_t ppt;
|
|
uint8_t font_width;
|
|
uint8_t font_height;
|
|
|
|
int err;
|
|
|
|
printk("Starting Mellifera version %s...\n", APP_VERSION_STRING);
|
|
// printk("Board: %s\n", BOARD);
|
|
|
|
LOG_INF("Starting Beacon Demo");
|
|
|
|
// Initialize the Bluetooth Subsystem
|
|
err = bt_enable(bt_ready);
|
|
if (err)
|
|
{
|
|
LOG_ERR("Bluetooth init failed (err %d)", err);
|
|
}
|
|
|
|
dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_display));
|
|
if (!device_is_ready(dev))
|
|
{
|
|
LOG_ERR("Device %s not ready", dev->name);
|
|
return 0;
|
|
}
|
|
|
|
if (display_set_pixel_format(dev, PIXEL_FORMAT_MONO10) != 0)
|
|
{
|
|
if (display_set_pixel_format(dev, PIXEL_FORMAT_MONO01) != 0)
|
|
{
|
|
LOG_ERR("Failed to set required pixel format");
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
LOG_INF("Initialized %s", dev->name);
|
|
|
|
if (cfb_framebuffer_init(dev))
|
|
{
|
|
LOG_ERR("Framebuffer initialization failed!");
|
|
return 0;
|
|
}
|
|
|
|
cfb_framebuffer_clear(dev, true);
|
|
|
|
display_blanking_off(dev);
|
|
|
|
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);
|
|
|
|
for (int idx = 0; idx < 42; idx++)
|
|
{
|
|
if (cfb_get_font_size(dev, 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(dev, 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));
|
|
|
|
cfb_set_kerning(dev, 3);
|
|
|
|
const struct device *ina, *bmp, *bmx, *lis, *hdc;
|
|
|
|
ina = DEVICE_DT_GET(DT_NODELABEL(ina231));
|
|
if (!device_is_ready(ina))
|
|
{
|
|
LOG_ERR("Device %s not ready", ina->name);
|
|
return 0;
|
|
}
|
|
|
|
bmp = DEVICE_DT_GET(DT_NODELABEL(bmp388));
|
|
if (!device_is_ready(bmp))
|
|
{
|
|
LOG_ERR("Device %s not ready", bmp->name);
|
|
return 0;
|
|
}
|
|
|
|
bmx = DEVICE_DT_GET(DT_NODELABEL(bmx055));
|
|
if (!device_is_ready(bmx))
|
|
{
|
|
LOG_ERR("Device %s not ready", bmx->name);
|
|
return 0;
|
|
}
|
|
LOG_INF("Initialized %s", bmx->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)
|
|
{
|
|
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)
|
|
{
|
|
LOG_ERR("Could not fetch sample (%d)", ret);
|
|
return 0;
|
|
}
|
|
ret = sensor_channel_get(ina, SENSOR_CHAN_VOLTAGE, &voltage);
|
|
if (ret < 0)
|
|
{
|
|
LOG_ERR("Could not get voltage (%d)", ret);
|
|
return 0;
|
|
}
|
|
ret = sensor_channel_get(ina, SENSOR_CHAN_CURRENT, ¤t);
|
|
if (ret < 0)
|
|
{
|
|
LOG_ERR("Could not get current (%d)", ret);
|
|
return 0;
|
|
}
|
|
|
|
ret = sensor_sample_fetch(bmp);
|
|
if (ret < 0)
|
|
{
|
|
LOG_ERR("Could not fetch sample (%d)", ret);
|
|
return 0;
|
|
}
|
|
ret = sensor_channel_get(bmp, SENSOR_CHAN_PRESS, &pressure);
|
|
if (ret < 0)
|
|
{
|
|
LOG_ERR("Could not get pressure (%d)", ret);
|
|
return 0;
|
|
}
|
|
// ret = sensor_channel_get(bmp, SENSOR_CHAN_AMBIENT_TEMP, &temperature);
|
|
// if (ret < 0)
|
|
// {
|
|
// LOG_ERR("Could not get temperature (%d)", ret);
|
|
// return 0;
|
|
// }
|
|
|
|
ret = sensor_sample_fetch(bmx);
|
|
if (ret < 0)
|
|
{
|
|
LOG_ERR("Could not fetch sample (%d)", ret);
|
|
return 0;
|
|
}
|
|
ret = sensor_channel_get(bmx, SENSOR_CHAN_DIE_TEMP, &temperature);
|
|
if (ret < 0)
|
|
{
|
|
LOG_ERR("Could not get temperature (%d)", ret);
|
|
return 0;
|
|
}
|
|
ret = sensor_channel_get(bmx, SENSOR_CHAN_ACCEL_X, &accel_x);
|
|
if (ret < 0)
|
|
{
|
|
LOG_ERR("Could not get acceleration (%d)", ret);
|
|
return 0;
|
|
}
|
|
ret = sensor_channel_get(bmx, SENSOR_CHAN_ACCEL_Y, &accel_y);
|
|
if (ret < 0)
|
|
{
|
|
LOG_ERR("Could not get acceleration (%d)", ret);
|
|
return 0;
|
|
}
|
|
ret = sensor_channel_get(bmx, SENSOR_CHAN_ACCEL_Z, &accel_z);
|
|
if (ret < 0)
|
|
{
|
|
LOG_ERR("Could not get acceleration (%d)", ret);
|
|
return 0;
|
|
}
|
|
|
|
ret = sensor_sample_fetch(lis);
|
|
if (ret < 0)
|
|
{
|
|
LOG_ERR("Could not fetch sample (%d)", ret);
|
|
return 0;
|
|
}
|
|
ret = sensor_channel_get(lis, SENSOR_CHAN_ACCEL_Z, &accel_z_ref);
|
|
if (ret < 0)
|
|
{
|
|
LOG_ERR("Could not get acceleration (%d)", ret);
|
|
return 0;
|
|
}
|
|
|
|
ret = sensor_sample_fetch(hdc);
|
|
if (ret < 0)
|
|
{
|
|
LOG_ERR("Could not fetch sample (%d)", ret);
|
|
return 0;
|
|
}
|
|
ret = sensor_channel_get(hdc, SENSOR_CHAN_AMBIENT_TEMP, &temperature);
|
|
if (ret < 0)
|
|
{
|
|
LOG_ERR("Could not get sample (%d)", ret);
|
|
return 0;
|
|
}
|
|
ret = sensor_channel_get(hdc, SENSOR_CHAN_HUMIDITY, &humidity);
|
|
if (ret < 0)
|
|
{
|
|
LOG_ERR("Could not get sample (%d)", ret);
|
|
return 0;
|
|
}
|
|
|
|
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, "X :%+7.3f", accel_x.val1 + accel_x.val2 * 1e-6);
|
|
sprintf(str_ay, "Y :%+7.3f", accel_y.val1 + accel_y.val2 * 1e-6);
|
|
sprintf(str_az, "Z :%+7.3f", accel_z.val1 + accel_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);
|
|
|
|
// printf("%s\t%s\t%s\t%s\n", str_v, str_i, str_p, str_t);
|
|
|
|
cfb_framebuffer_clear(dev, false);
|
|
if (cfb_print(dev, str_t, 0, 0))
|
|
{
|
|
printf("Failed to print a string\n");
|
|
continue;
|
|
}
|
|
if (cfb_print(dev, str_h, 0, 16))
|
|
{
|
|
printf("Failed to print a string\n");
|
|
continue;
|
|
}
|
|
// if (cfb_print(dev, str_az, 0, 16 * 2))
|
|
// {
|
|
// printf("Failed to print a string\n");
|
|
// continue;
|
|
// }
|
|
// if (cfb_print(dev, str_az_ref, 0, 16 * 3))
|
|
// {
|
|
// printf("Failed to print a string\n");
|
|
// continue;
|
|
// }
|
|
|
|
cfb_framebuffer_finalize(dev);
|
|
#if defined(CONFIG_ARCH_POSIX)
|
|
k_sleep(K_MSEC(20));
|
|
#endif
|
|
}
|
|
|
|
return 0;
|
|
}
|