i2c: all function calls are consistent. fixed some MISRA errors.

This commit is contained in:
A.M. Rowsell 2026-06-30 00:31:20 -04:00
commit 958b2e8e00
7 changed files with 71 additions and 68 deletions

46
main.c
View file

@ -44,14 +44,6 @@
#define WIFI_PASSWORD "WIFI_PASSWORD"
#define MQTT_KEEP_ALIVE_SECONDS 60
// MQTT Topics
static const char temp_topic[] = "ADAFRUIT_IO_USERNAME/feeds/ensaht.temperature";
static const char humi_topic[] = "ADAFRUIT_IO_USERNAME/feeds/ensaht.humidity";
static const char aqi_topic[] = "ADAFRUIT_IO_USERNAME/feeds/ensaht.aqi";
static const char tvoc_topic[] = "ADAFRUIT_IO_USERNAME/feeds/ensaht.tvoc";
static const char etoh_topic[] = "ADAFRUIT_IO_USERNAME/feeds/ensaht.etoh";
static const char eco2_topic[] = "ADAFRUIT_IO_USERNAME/feeds/ensaht.eco2";
static void mqtt_connection_cb(mqtt_client_t *client, void *arg,
mqtt_connection_status_t status) {
LWIP_UNUSED_ARG(client);
@ -65,7 +57,7 @@ static float average_float(const float *samples, uint8_t count) {
for (uint8_t i = 0; i < count; i++) {
sum += samples[i];
}
return sum / count;
return sum / (float)count;
}
static double average_double(const double *samples, uint8_t count) {
@ -73,7 +65,7 @@ static double average_double(const double *samples, uint8_t count) {
for (uint8_t i = 0; i < count; i++) {
sum += samples[i];
}
return sum / count;
return sum / (double)count;
}
static uint16_t average_uint16(const uint16_t *samples, uint8_t count) {
@ -81,10 +73,18 @@ static uint16_t average_uint16(const uint16_t *samples, uint8_t count) {
for (uint8_t i = 0; i < count; i++) {
sum += samples[i];
}
return (uint16_t)((sum + (uint32_t)count / 2) / (uint32_t)count);
return (uint16_t)((sum + (uint32_t)count / 2UL) / (uint32_t)count);
}
int main() {
int main(void) {
// MQTT Topics
static const char temp_topic[] = "ADAFRUIT_IO_USERNAME/feeds/ensaht.temperature";
static const char humi_topic[] = "ADAFRUIT_IO_USERNAME/feeds/ensaht.humidity";
static const char aqi_topic[] = "ADAFRUIT_IO_USERNAME/feeds/ensaht.aqi";
static const char tvoc_topic[] = "ADAFRUIT_IO_USERNAME/feeds/ensaht.tvoc";
static const char etoh_topic[] = "ADAFRUIT_IO_USERNAME/feeds/ensaht.etoh";
static const char eco2_topic[] = "ADAFRUIT_IO_USERNAME/feeds/ensaht.eco2";
static const char prs_topic[] = "ADAFRUIT_IO_USERNAME/feeds/ensaht.pressure";
uint16_t tvoc, eco2, etoh, avg_tvoc, avg_eco2, avg_etoh;
uint8_t aqi, validData;
double t, h, avg_t, avg_h, avg_bmp_t, avg_bmp_p;
@ -93,8 +93,8 @@ int main() {
float bmpt_avg[AVERAGES];
float bmpp_avg[AVERAGES];
char payload[32];
float bmpT;
float bmpP;
float bmpT = 0.0;
float bmpP = 0.0;
// MQTT setup
ensaht_mqtt_t mqtt;
struct mqtt_connect_client_info_t mqtt_client_info = {
@ -139,7 +139,7 @@ int main() {
if (mqtt_err != ERR_OK) {
printf("MQTT connect start failed: %d\n", mqtt_err);
}
i2c_init(i2c_default, 100 * 1000); // start i2c at 100khz
i2c_init(i2c_default, 400 * 1000); // start i2c0 at 400khz
gpio_set_function(PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C);
gpio_set_function(PICO_DEFAULT_I2C_SDA_PIN, GPIO_FUNC_I2C);
gpio_pull_up(PICO_DEFAULT_I2C_SCL_PIN);
@ -159,10 +159,10 @@ int main() {
printf("\x1b[2J"); // clear screen
uint8_t i = 0;
uint8_t sample_count = 0;
activateENS();
activateENS(i2c_default);
while (1) {
// cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
getAHTData(&t, &h);
getAHTData(i2c_default, &t, &h);
getBMPData(i2c1, &bmpT, &bmpP);
t_avg[i] = t;
h_avg[i] = h;
@ -177,8 +177,9 @@ int main() {
avg_bmp_p = average_float(bmpp_avg, sample_count);
printf("\x1b[H\x1b[JTemperature %0.2f C Humidity %0.2f%%\n", avg_t, avg_h);
printf("BMP Temperature %0.2f C Pressure %0.2f kPa\n", avg_bmp_t,
avg_bmp_p / 100.0);
validData = getENSData(&avg_bmp_t, &avg_h, &aqi, &tvoc, &eco2, &etoh);
avg_bmp_p / 1000.0);
validData =
getENSData(i2c_default, &avg_bmp_t, &avg_h, &aqi, &tvoc, &eco2, &etoh);
tvoc_avg[i] = tvoc;
eco2_avg[i] = eco2;
etoh_avg[i] = etoh;
@ -194,10 +195,10 @@ int main() {
i++;
if (i == AVERAGES)
i = 0;
snprintf(payload, sizeof(payload), "%.2f", avg_t);
snprintf(payload, sizeof(payload), "%.4f", avg_bmp_t);
ensaht_mqtt_publish_client(&mqtt, temp_topic, payload, strlen(payload),
NULL, NULL);
snprintf(payload, sizeof(payload), "%.2f", avg_h);
snprintf(payload, sizeof(payload), "%.4f", avg_h);
ensaht_mqtt_publish_client(&mqtt, humi_topic, payload, strlen(payload),
NULL, NULL);
snprintf(payload, sizeof(payload), "%d", avg_tvoc);
@ -212,6 +213,9 @@ int main() {
snprintf(payload, sizeof(payload), "%d", aqi);
ensaht_mqtt_publish_client(&mqtt, aqi_topic, payload, strlen(payload), NULL,
NULL);
snprintf(payload, sizeof(payload), "%.4f", avg_bmp_p / 1000.0);
ensaht_mqtt_publish_client(&mqtt, prs_topic, payload, strlen(payload), NULL,
NULL);
sleep_ms(30000);
}