7 Commits

8 changed files with 304 additions and 110 deletions

9
dependencies.lock Normal file
View File

@@ -0,0 +1,9 @@
dependencies:
idf:
component_hash: null
source:
type: idf
version: 4.4.4
manifest_hash: dcf4d39b94252de130019eadceb989d72b0dbc26b552cfdcbb50f6da531d2b92
target: esp32c3
version: 1.0.0

View File

@@ -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" "app_nvs.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 ".")

View File

@@ -2,6 +2,7 @@
#include <stdlib.h>
#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,

19
main/app_nvs.c Normal file
View File

@@ -0,0 +1,19 @@
#pragma once
#include <stdio.h>
#include "esp_system.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "nvs.h"
#include "nvs_flash.h"
void app_nvs_init() {
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES ||
err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
err = nvs_flash_init();
}
ESP_ERROR_CHECK(err);
}

27
main/hw-ms03.c Normal file
View File

@@ -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);
}

View File

@@ -9,9 +9,12 @@
#pragma once
#include "driver/rmt.h"
#include "esp_log.h"
#include "esp_system.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "led_strip.h"
#include "nvs.h"
#include "nvs_flash.h"
#include "sdkconfig.h"
static const char *LIGHT_TAG = "DisplayAmbientLight_Light";
@@ -36,6 +39,10 @@ light_mode_t light_mode;
float display_ambient_light_brightness = 1;
uint8_t display_ambient_lighting_level = 255;
float led_strip_red_calibration = 1.0;
float led_strip_green_calibration = 1.0;
float led_strip_blue_calibration = 1.0;
void led_strip_fade_in_light_level(void *pvParameter) {
float target = (float)display_ambient_lighting_level / 255.0;
float step_length = (target - display_ambient_light_brightness) / 40.0;
@@ -55,6 +62,68 @@ void led_strip_set_brightness(uint8_t level) {
xTaskCreate(led_strip_fade_in_light_level, "LED_STRIP_FADE_IN_LIGHT_LEVEL",
4096, NULL, 1, NULL);
nvs_handle_t nvs_handle;
esp_err_t err = nvs_open("storage", NVS_READWRITE, &nvs_handle);
err = nvs_set_u8(nvs_handle, "brightness", level);
if (err != ESP_OK) {
ESP_LOGW(LIGHT_TAG, "Error (%s) saving light level!\n",
esp_err_to_name(err));
nvs_close(nvs_handle);
return;
}
err = nvs_commit(nvs_handle);
if (err != ESP_OK) {
ESP_LOGW(LIGHT_TAG, "Error (%s) saving light level!\n",
esp_err_to_name(err));
}
nvs_close(nvs_handle);
}
void led_strip_set_color_calibration(float red, float green, float blue) {
led_strip_red_calibration = red;
led_strip_green_calibration = green;
led_strip_blue_calibration = blue;
nvs_handle_t local_nvs_handle;
esp_err_t err = nvs_open("storage", NVS_READWRITE, &local_nvs_handle);
if (err != ESP_OK) {
ESP_LOGW(LIGHT_TAG, "Error (%s) opening NVS handle!\n",
esp_err_to_name(err));
return;
}
err = nvs_set_u8(local_nvs_handle, "calibration_r", (uint32_t)(red * 255));
if (err != ESP_OK) {
nvs_close(local_nvs_handle);
ESP_LOGW(LIGHT_TAG, "Error (%s) write calibration_r failed!",
esp_err_to_name(err));
return;
}
err = nvs_set_u8(local_nvs_handle, "calibration_g", (uint8_t)(green * 255));
if (err != ESP_OK) {
nvs_close(local_nvs_handle);
ESP_LOGW(LIGHT_TAG, "Error (%s) calibration_g failed!",
esp_err_to_name(err));
return;
}
err = nvs_set_u8(local_nvs_handle, "calibration_b", (uint8_t)(blue * 255));
if (err != ESP_OK) {
nvs_close(local_nvs_handle);
ESP_LOGW(LIGHT_TAG, "Error (%s) calibration_b failed!",
esp_err_to_name(err));
return;
}
err = nvs_commit(local_nvs_handle);
if (err != ESP_OK) {
ESP_LOGW(LIGHT_TAG, "Error (%s) save led_strip_red_calibration failed!",
esp_err_to_name(err));
}
nvs_close(local_nvs_handle);
}
/**
@@ -157,21 +226,64 @@ void light_for_init() {
ESP_LOGI(LIGHT_TAG, "light_for_init");
ESP_ERROR_CHECK(light_led_strip->clear(light_led_strip, 100));
uint32_t red = 0, green = 0, blue = 0;
int8_t i = 0;
nvs_handle local_nvs_handle;
esp_err_t err = nvs_open("storage", NVS_READWRITE, &local_nvs_handle);
if (err != ESP_OK) {
ESP_LOGW(LIGHT_TAG, "Error (%s) opening NVS handle!", esp_err_to_name(err));
}
uint8_t r = 255, g = 255, b = 255;
uint8_t brightness = 200;
err = nvs_get_u8(local_nvs_handle, "calibration_r", &r);
if (err != ESP_OK) {
ESP_LOGW(LIGHT_TAG, "Error (%s) reading calibration_r!",
esp_err_to_name(err));
}
err = nvs_get_u8(local_nvs_handle, "calibration_g", &g);
if (err != ESP_OK) {
ESP_LOGW(LIGHT_TAG, "Error (%s) reading calibration_g!",
esp_err_to_name(err));
}
err = nvs_get_u8(local_nvs_handle, "calibration_b", &b);
if (err != ESP_OK) {
ESP_LOGW(LIGHT_TAG, "Error (%s) reading calibration_b!",
esp_err_to_name(err));
}
err = nvs_get_u8(local_nvs_handle, "brightness", &brightness);
if (err != ESP_OK) {
ESP_LOGW(LIGHT_TAG, "Error (%s) reading brightness!", esp_err_to_name(err));
}
nvs_close(local_nvs_handle);
// set brightness
led_strip_set_brightness(brightness);
// play init light
float r_f = (float)r / 255.0;
float g_f = (float)g / 255.0;
float b_f = (float)b / 255.0;
uint8_t init_r, init_g, init_b;
do {
for (; i < 100; i++) {
for (uint8_t i = 0; i < 50; i++) {
init_r = (uint8_t)(r_f * (float)i);
init_g = (uint8_t)(g_f * (float)i);
init_b = (uint8_t)(b_f * (float)i);
for (int j = 0; j < STRIP_LED_NUMBER; j++) {
led_strip_hsv2rgb(0, 0, i, &red, &green, &blue);
ESP_ERROR_CHECK(
light_led_strip->set_pixel(light_led_strip, j, red, green, blue));
ESP_ERROR_CHECK(light_led_strip->set_pixel(light_led_strip, j, init_r,
init_g, init_b));
}
ESP_ERROR_CHECK(light_led_strip->refresh(light_led_strip, 100));
vTaskDelay(pdMS_TO_TICKS(10));
vTaskDelay(pdMS_TO_TICKS(20));
}
vTaskDelay(pdMS_TO_TICKS(100));
} while (light_mode == light_mode_init);
led_strip_set_color_calibration((float)r / 255.0, (float)g / 255.0,
(float)b / 255.0);
}
void light_for_connecting_wifi() {
@@ -206,9 +318,11 @@ void light_for_idle() {
// Build RGB values
led_strip_hsv2rgb(hue, 50, 30, &red, &green, &blue);
red = red * display_ambient_light_brightness;
green = green * display_ambient_light_brightness;
blue = blue * display_ambient_light_brightness;
red = red * display_ambient_light_brightness * led_strip_red_calibration;
green = green * display_ambient_light_brightness *
led_strip_green_calibration;
blue =
blue * display_ambient_light_brightness * led_strip_blue_calibration;
// Write RGB values to strip driver
ESP_ERROR_CHECK(
light_led_strip->set_pixel(light_led_strip, j, red, green, blue));
@@ -273,18 +387,40 @@ void light_init_strip() {
void light_play_colors(uint16_t len, uint8_t *buffer) {
light_mode = light_mode_desktop_sending_colors;
uint16_t black_count = 0; // count of black pixels. r/g/b <= 10
for (uint16_t led_index = 0, buffer_cursor = 0;
led_index < STRIP_LED_NUMBER && buffer_cursor < len;
led_index++, buffer_cursor += 3) {
uint8_t r = (uint8_t)((float)buffer[buffer_cursor] *
display_ambient_light_brightness),
display_ambient_light_brightness *
led_strip_red_calibration),
g = (uint8_t)((float)buffer[buffer_cursor + 1] *
display_ambient_light_brightness),
display_ambient_light_brightness *
led_strip_green_calibration),
b = (uint8_t)((float)buffer[buffer_cursor + 2] *
display_ambient_light_brightness);
display_ambient_light_brightness *
led_strip_blue_calibration);
if (r <= 7 && g <= 7 && b <= 7) {
black_count++;
}
ESP_ERROR_CHECK(
light_led_strip->set_pixel(light_led_strip, led_index, r, g, b));
}
if (black_count > STRIP_LED_NUMBER / 5 * 4) {
uint8_t r = (uint8_t)((float)50 * display_ambient_light_brightness *
led_strip_red_calibration),
g = (uint8_t)((float)40 * display_ambient_light_brightness *
led_strip_green_calibration),
b = (uint8_t)((float)20 * display_ambient_light_brightness *
led_strip_blue_calibration);
for (uint16_t led_index = 0; led_index < STRIP_LED_NUMBER; led_index++) {
ESP_ERROR_CHECK(
light_led_strip->set_pixel(light_led_strip, led_index, r, g, b));
}
} else {
}
ESP_ERROR_CHECK(light_led_strip->refresh(light_led_strip, 100));
vTaskDelay(pdMS_TO_TICKS(10));
}

View File

@@ -1,9 +1,11 @@
#include "apds_9960.c"
#include "app_nvs.c"
#include "ci_03t.c"
#include "embedded_display.c"
#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"
@@ -16,6 +18,7 @@
static const char *TAG = "DisplayAmbientLight";
void app_main(void) {
app_nvs_init();
light_init_strip();
gpio_install_isr_service(0);
@@ -24,28 +27,29 @@ void app_main(void) {
init_display();
display_print8_str(0, 0, "Ambient Light");
// ci_03t_init();
// 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);
}
}

View File

@@ -40,6 +40,8 @@
#define MQTT_KEY_DESKTOP_ONLINE MQTT_DESKTOP_KEY_PREFIX MQTT_ONLINE_SUFFIX
#define MQTT_KEY_DESKTOP_COLORS MQTT_DESKTOP_KEY_PREFIX MQTT_COLORS_SUFFIX
#define MQTT_KEY_DESKTOP_ALL MQTT_DESKTOP_KEY_PREFIX MQTT_ALL_SUFFIX
#define MQTT_KEY_DESKTOP_COLOR_CALIBRATION \
MQTT_DESKTOP_KEY_PREFIX "color-calibration"
#define MQTT_KEY_BOARD_CMD MQTT_BOARD_KEY_PREFIX MQTT_CMD_SUFFIX
#define MQTT_KEY_DESKTOP_DISPLAY_0_BRIGHTNESS \
@@ -124,6 +126,11 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base,
.value = (uint16_t)(event->data[0] << 8 | event->data[1]),
};
xQueueSend(mqtt_cmd_event, &mqtt_event, NULL);
} else if (strncmp(event->topic, MQTT_KEY_DESKTOP_COLOR_CALIBRATION,
event->topic_len) == 0) {
led_strip_set_color_calibration((float)event->data[0] / 255.0,
(float)event->data[1] / 255.0,
(float)event->data[2] / 255.0);
} else {
printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
printf("DATA=%.*s\r\n", event->data_len, event->data);