diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index e6cd9ff..5db0748 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,2 +1,2 @@ -idf_component_register(SRCS "apds_9960.c" "pca9555.c" "i2c.c" "asr_pro.c" "ci_03t.c" "ui_input.c" "ambient_light.c" "temperature.c" "embedded_display.c" "mqtt.c" "main.c" "wifi.c" "light.c" "mqtt.c" +idf_component_register(SRCS "hw-ms03.c" "apds_9960.c" "pca9555.c" "i2c.c" "asr_pro.c" "ci_03t.c" "ui_input.c" "ambient_light.c" "temperature.c" "embedded_display.c" "mqtt.c" "main.c" "wifi.c" "light.c" "mqtt.c" INCLUDE_DIRS ".") \ No newline at end of file diff --git a/main/apds_9960.c b/main/apds_9960.c index 2020b96..1bc4144 100644 --- a/main/apds_9960.c +++ b/main/apds_9960.c @@ -2,6 +2,7 @@ #include #include "common.h" +#include "driver/gpio.h" #include "driver/i2c.h" #include "embedded_display.c" #include "esp_log.h" @@ -122,7 +123,8 @@ // 50 mA LED, Reserved, 2x PGAIN, 4x ALS/Cain GAIN #define APDS_9960_CONTROL_VALUE 0b00001010 // Enable Gesture, Proximity, ALS, Power -#define APDS_9960_ENABLE_VALUE 0b01000111 +// X, GEN, PIEN, AIEN, WEN, PEN, AEN, PON +#define APDS_9960_ENABLE_VALUE 0b01100111 #define APDS_9960_TAG "APDS-9960" @@ -265,71 +267,44 @@ void apds_9960_fetch(void* arg) { } } - // display_fill_rect(0, 0, 128, 8, 0x00); + // clear + error = apds_9960_read_word(APDS_9960_REG_CDATAL, &clear_raw); + if (error != ESP_OK) { + ESP_LOGW(APDS_9960_TAG, "read failed. %d", error); + } else { + sprintf(clear_str, "C:% 5d", clear_raw); + display_print8_str(64, 6, clear_str); + ESP_LOGD(APDS_9960_TAG, "Clear: %d", clear_raw); + } + // red + error = apds_9960_read_word(APDS_9960_REG_RDATAL, &red_raw); + if (error != ESP_OK) { + ESP_LOGW(APDS_9960_TAG, "read failed. %d", error); + } else { + sprintf(red_str, "R:% 5d", red_raw); + display_print8_str(0, 4, red_str); + ESP_LOGD(APDS_9960_TAG, "Red: %d", red_raw); + } + // green + error = apds_9960_read_word(APDS_9960_REG_GDATAL, &green_raw); + if (error != ESP_OK) { + ESP_LOGW(APDS_9960_TAG, "read failed. %d", error); + } else { + sprintf(green_str, "G:% 5d", green_raw); + display_print8_str(64, 4, green_str); + ESP_LOGD(APDS_9960_TAG, "Green: %d", green_raw); + } + // blue + error = apds_9960_read_word(APDS_9960_REG_BDATAL, &blue_raw); + if (error != ESP_OK) { + ESP_LOGW(APDS_9960_TAG, "read failed. %d", error); + } else { + sprintf(blue_str, "B:% 5d", blue_raw); + display_print8_str(0, 6, blue_str); + ESP_LOGD(APDS_9960_TAG, "Blue: %d", blue_raw); + } - // interrupt = gpio_get_level(APDS_9960_INT_GPIO); - - // // Status - // error = apds_9960_read_byte(APDS_9960_REG_STATUS, &status_raw); - // if (error != ESP_OK) { - // ESP_LOGW(APDS_9960_TAG, "read status failed. %x", error); - // } else { - // sprintf(status_str, "%d :%d%d%d%d %d%d%d%d", interrupt, - // (status_raw >> 7) & 1, (status_raw >> 6) & 1, - // (status_raw >> 5) & 1, (status_raw >> 4) & 1, - // (status_raw >> 3) & 1, (status_raw >> 2) & 1, - // (status_raw >> 1) & 1, status_raw & 1); - // display_print8_str(0, 0, status_str); - // ESP_LOGD(APDS_9960_TAG, "Status: %2x", status_raw); - // } - - // error = apds_9960_read_byte(APDS_9960_REG_GFLVL, &byte_buffer); - // if (error != ESP_OK) { - // ESP_LOGW(APDS_9960_TAG, "read APDS_9960_REG_GFLVL failed. %d", error); - // } - // if (byte_buffer == 0) { - // sprintf(status_str, "LVL: %4d", byte_buffer); - // display_print8_str(0, 4, status_str); - // } - - // // clear - // error = apds_9960_read_word(APDS_9960_REG_CDATAL, &clear_raw); - // if (error != ESP_OK) { - // ESP_LOGW(APDS_9960_TAG, "read failed. %d", error); - // } else { - // sprintf(clear_str, "C:% 5d", clear_raw); - // display_print8_str(64, 6, clear_str); - // ESP_LOGD(APDS_9960_TAG, "Clear: %d", clear_raw); - // } - // // red - // error = apds_9960_read_word(APDS_9960_REG_RDATAL, &red_raw); - // if (error != ESP_OK) { - // ESP_LOGW(APDS_9960_TAG, "read failed. %d", error); - // } else { - // sprintf(red_str, "R:% 5d", red_raw); - // display_print8_str(0, 4, red_str); - // ESP_LOGD(APDS_9960_TAG, "Red: %d", red_raw); - // } - // // green - // error = apds_9960_read_word(APDS_9960_REG_GDATAL, &green_raw); - // if (error != ESP_OK) { - // ESP_LOGW(APDS_9960_TAG, "read failed. %d", error); - // } else { - // sprintf(green_str, "G:% 5d", green_raw); - // display_print8_str(64, 4, green_str); - // ESP_LOGD(APDS_9960_TAG, "Green: %d", green_raw); - // } - // // blue - // error = apds_9960_read_word(APDS_9960_REG_BDATAL, &blue_raw); - // if (error != ESP_OK) { - // ESP_LOGW(APDS_9960_TAG, "read failed. %d", error); - // } else { - // sprintf(blue_str, "B:% 5d", blue_raw); - // display_print8_str(0, 6, blue_str); - // ESP_LOGD(APDS_9960_TAG, "Blue: %d", blue_raw); - // } - - vTaskDelay(pdMS_TO_TICKS(100)); + vTaskDelay(pdMS_TO_TICKS(1000)); } display_fill_rect(0, 2, 128, 8, 0x00); } @@ -386,6 +361,26 @@ void apds_9960_read_gesture() { uint8_t last_2_index = byte_buffer - 2; + // head 2 + for (int i = 0; i < 2; i++) { + u = gesture_values_raw_arr[i] & 0xff; + d = gesture_values_raw_arr[i] >> 8 & 0xff; + l = gesture_values_raw_arr[i] >> 16 & 0xff; + r = gesture_values_raw_arr[i] >> 24 & 0xff; + before_ud += (u - d) * 100 / u + d; + before_lr += (l - r) * 100 / l + r; + } + + // last 2 + for (int i = last_2_index; i < byte_buffer; i++) { + u = gesture_values_raw_arr[i] & 0xff; + d = gesture_values_raw_arr[i] >> 8 & 0xff; + l = gesture_values_raw_arr[i] >> 16 & 0xff; + r = gesture_values_raw_arr[i] >> 24 & 0xff; + after_ud += (u - d) * 100 / u + d; + after_lr += (l - r) * 100 / l + r; + } + for (int i = 0; i < byte_buffer; i++) { if (i < 2) { u = gesture_values_raw_arr[i] & 0xff; @@ -394,8 +389,6 @@ void apds_9960_read_gesture() { r = gesture_values_raw_arr[i] >> 24 & 0xff; before_ud += (u - d) * 100 / u + d; before_lr += (l - r) * 100 / l + r; - printf("Gesture %08x %03d %03d %03d %03d\n", gesture_values_raw_arr[i], - u, d, l, r); } else if (i >= last_2_index) { u = gesture_values_raw_arr[i] & 0xff; d = gesture_values_raw_arr[i] >> 8 & 0xff; @@ -403,24 +396,22 @@ void apds_9960_read_gesture() { r = gesture_values_raw_arr[i] >> 24 & 0xff; after_ud += (u - d) * 100 / u + d; after_lr += (l - r) * 100 / l + r; - printf("Gesture %08x %03d %03d %03d %03d\n", gesture_values_raw_arr[i], - u, d, l, r); } } printf("Δud: %d, Δlr: %d \n", after_ud - before_ud, after_lr - before_lr); - display_fill_rect(0, 4, 128, 8, 0x00); + display_fill_rect(0, 0, 128, 2, 0x00); if (abs(after_ud - before_ud) * 2 > abs(after_lr - before_lr)) { if (after_ud - before_ud < -80) { - display_print8_str(0, 4, "Gesture: up"); + display_print8_str(0, 0, "Gesture: up"); } else if (after_ud - before_ud > 80) { - display_print8_str(0, 4, "Gesture: down"); + display_print8_str(0, 0, "Gesture: down"); } } else { if (after_lr - before_lr < -120) { - display_print8_str(0, 4, "Gesture: left"); + display_print8_str(0, 0, "Gesture: left"); } else if (after_lr - before_lr > 120) { - display_print8_str(0, 4, "Gesture: right"); + display_print8_str(0, 0, "Gesture: right"); } } // display_print8_str(0, 0, gesture_str); @@ -524,7 +515,8 @@ void apds_9960_init() { // // enable sleep after interrupt // ESP_ERROR_CHECK(apds_9960_write(APDS_9960_REG_CONFIG3, 0x10)); - ESP_ERROR_CHECK(apds_9960_write(APDS_9960_REG_ENABLE, 0b01101101)); + ESP_ERROR_CHECK( + apds_9960_write(APDS_9960_REG_ENABLE, APDS_9960_ENABLE_VALUE)); apds_9960_int_evt_queue = xQueueCreate(10, NULL); xTaskCreate(apds_9960_int_evt_handler, "apds_9960_gesture_fetch", 2048, NULL, diff --git a/main/hw-ms03.c b/main/hw-ms03.c new file mode 100644 index 0000000..ef70a48 --- /dev/null +++ b/main/hw-ms03.c @@ -0,0 +1,27 @@ +#pragma once +#include "driver/gpio.h" +#include "freeRTOS/FreeRTOS.h" + +#define HW_MS03_INT_GPIO 6 +#define BEEP_GPIO 7 + +void hw_ms03_int_handler(void *arg) { + gpio_set_level(BEEP_GPIO, gpio_get_level(HW_MS03_INT_GPIO)); +} + +void hw_ms03_init() { + gpio_config_t io_conf = {}; + + io_conf.mode = GPIO_MODE_INPUT; + io_conf.pull_up_en = 0; + io_conf.pull_down_en = 1; + io_conf.intr_type = GPIO_INTR_ANYEDGE; + io_conf.pin_bit_mask = 1ULL << HW_MS03_INT_GPIO; + gpio_config(&io_conf); + io_conf.mode = GPIO_MODE_OUTPUT; + io_conf.pin_bit_mask = 1ULL << BEEP_GPIO; + io_conf.pull_down_en = 0; + gpio_config(&io_conf); + + gpio_isr_handler_add(HW_MS03_INT_GPIO, hw_ms03_int_handler, NULL); +} \ No newline at end of file diff --git a/main/main.c b/main/main.c index b3dd370..8467e05 100644 --- a/main/main.c +++ b/main/main.c @@ -4,6 +4,7 @@ #include "esp_log.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" +#include "hw-ms03.c" #include "i2c.c" #include "light.c" #include "mqtt.c" @@ -24,28 +25,29 @@ void app_main(void) { init_display(); display_print8_str(0, 0, "Ambient Light"); + // hw_ms03_init(); // ci_03t_init(); apds_9960_init(); apds_9960_auto_fetch(); - // auto_fetch_temperature(); - // pca9555_init(); - // ui_input_init(); - // xTaskCreate(mqtt_publish_ui_input, "mqtt_publish_ui_input", 2048, NULL, 10, - // NULL); - // vTaskDelay(pdMS_TO_TICKS(10)); + auto_fetch_temperature(); + pca9555_init(); + ui_input_init(); + xTaskCreate(mqtt_publish_ui_input, "mqtt_publish_ui_input", 2048, NULL, 10, + NULL); + vTaskDelay(pdMS_TO_TICKS(10)); light_play(light_mode_connection_wifi); - // if (connect_wifi()) { - // light_play(light_mode_idle); - // } - // vTaskDelay(pdMS_TO_TICKS(1000)); - // mqtt_app_start(); - // if (waiting_for_mqtt_connected()) { - // light_play(light_mode_mqtt_connected); - // } - // if (waiting_for_desktop_online()) { - // light_play(light_mode_desktop_online); - // } - // while (waiting_and_get_colors()) { - // light_play_colors(NUMBER_OF_LEDS * 3, mqtt_colors_buffer); - // } + if (connect_wifi()) { + light_play(light_mode_idle); + } + vTaskDelay(pdMS_TO_TICKS(1000)); + mqtt_app_start(); + if (waiting_for_mqtt_connected()) { + light_play(light_mode_mqtt_connected); + } + if (waiting_for_desktop_online()) { + light_play(light_mode_desktop_online); + } + while (waiting_and_get_colors()) { + light_play_colors(NUMBER_OF_LEDS * 3, mqtt_colors_buffer); + } }