From 603efb8a088f4aa2924920096b5a9fe73354184c Mon Sep 17 00:00:00 2001 From: Alexey Zholtikov Date: Mon, 6 Mar 2023 02:07:22 +0300 Subject: [PATCH] Version 1.1 Added saving device ID to EEPROM. --- platformio.ini | 48 ++++++++++++++++++------------------------------ src/main.cpp | 41 ++++++++++++++++++++++++++++------------- 2 files changed, 46 insertions(+), 43 deletions(-) diff --git a/platformio.ini b/platformio.ini index 9389c57..49705c4 100644 --- a/platformio.ini +++ b/platformio.ini @@ -6,9 +6,6 @@ board_build.f_cpu = 8000000L framework = arduino upload_protocol = usbasp build_flags = - -D OPEN=1 - -D CLOSE=0 - -D OPEN_CLOSE=7 -D ID=$UNIX_TIME board_fuses.hfuse = 0xDE board_fuses.lfuse = 0xE2 @@ -17,8 +14,9 @@ upload_flags = -Pusb -e lib_deps = - feilipu/FreeRTOS@^10.5.0-0 - nrf24/RF24@^1.4.5 + https://github.com/aZholtikov/ZHConfig + https://github.com/feilipu/Arduino_FreeRTOS_Library + https://github.com/nRF24/RF24 [env:ATmega168P] platform = atmelavr @@ -28,9 +26,6 @@ board_build.f_cpu = 8000000L framework = arduino upload_protocol = usbasp build_flags = - -D OPEN=1 - -D CLOSE=0 - -D OPEN_CLOSE=7 -D ID=$UNIX_TIME board_fuses.hfuse = 0xDE board_fuses.lfuse = 0xE2 @@ -39,8 +34,9 @@ upload_flags = -Pusb -e lib_deps = - feilipu/FreeRTOS@^10.5.0-0 - nrf24/RF24@^1.4.5 + https://github.com/aZholtikov/ZHConfig + https://github.com/feilipu/Arduino_FreeRTOS_Library + https://github.com/nRF24/RF24 [env:ATmega168PB] platform = atmelavr @@ -50,9 +46,6 @@ board_build.f_cpu = 8000000L framework = arduino upload_protocol = usbasp build_flags = - -D OPEN=1 - -D CLOSE=0 - -D OPEN_CLOSE=7 -D ID=$UNIX_TIME board_fuses.hfuse = 0xDE board_fuses.lfuse = 0xE2 @@ -61,8 +54,9 @@ upload_flags = -Pusb -e lib_deps = - feilipu/FreeRTOS@^10.5.0-0 - nrf24/RF24@^1.4.5 + https://github.com/aZholtikov/ZHConfig + https://github.com/feilipu/Arduino_FreeRTOS_Library + https://github.com/nRF24/RF24 [env:ATmega328] platform = atmelavr @@ -72,9 +66,6 @@ board_build.f_cpu = 8000000L framework = arduino upload_protocol = usbasp build_flags = - -D OPEN=1 - -D CLOSE=0 - -D OPEN_CLOSE=7 -D ID=$UNIX_TIME board_fuses.hfuse = 0xDE board_fuses.lfuse = 0xE2 @@ -83,8 +74,9 @@ upload_flags = -Pusb -e lib_deps = - feilipu/FreeRTOS@^10.5.0-0 - nrf24/RF24@^1.4.5 + https://github.com/aZholtikov/ZHConfig + https://github.com/feilipu/Arduino_FreeRTOS_Library + https://github.com/nRF24/RF24 [env:ATmega328P] platform = atmelavr @@ -94,9 +86,6 @@ board_build.f_cpu = 8000000L framework = arduino upload_protocol = usbasp build_flags = - -D OPEN=1 - -D CLOSE=0 - -D OPEN_CLOSE=7 -D ID=$UNIX_TIME board_fuses.hfuse = 0xDE board_fuses.lfuse = 0xE2 @@ -105,8 +94,9 @@ upload_flags = -Pusb -e lib_deps = - feilipu/FreeRTOS@^10.5.0-0 - nrf24/RF24@^1.4.5 + https://github.com/aZholtikov/ZHConfig + https://github.com/feilipu/Arduino_FreeRTOS_Library + https://github.com/nRF24/RF24 [env:ATmega328PB] platform = atmelavr @@ -116,9 +106,6 @@ board_build.f_cpu = 8000000L framework = arduino upload_protocol = usbasp build_flags = - -D OPEN=1 - -D CLOSE=0 - -D OPEN_CLOSE=7 -D ID=$UNIX_TIME board_fuses.hfuse = 0xDE board_fuses.lfuse = 0xE2 @@ -127,5 +114,6 @@ upload_flags = -Pusb -e lib_deps = - feilipu/FreeRTOS@^10.5.0-0 - nrf24/RF24@^1.4.5 \ No newline at end of file + https://github.com/aZholtikov/ZHConfig + https://github.com/feilipu/Arduino_FreeRTOS_Library + https://github.com/nRF24/RF24 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 0344310..463cf16 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,20 +3,15 @@ #include "RF24.h" #include "avr/interrupt.h" #include "semphr.h" - -typedef struct -{ - int16_t sensor_id{0}; - int16_t sensor_type{0}; - int16_t value_1{0}; - int16_t value_2{0}; - int16_t value_3{0}; - int16_t value_4{0}; - int16_t value_5{0}; -} transmitted_data_t; +#include "EEPROM.h" +#include "ZHConfig.h" void sendButtonStatus(void *pvParameters); float getBatteryLevelCharge(void); +void loadConfig(void); +void saveConfig(void); + +int16_t id{abs((int16_t)ID)}; RF24 radio(9, 10); SemaphoreHandle_t buttonSemaphore; @@ -56,13 +51,13 @@ void sendButtonStatus(void *pvParameters) for (;;) { xSemaphoreTake(buttonSemaphore, portMAX_DELAY); - transmitted_data_t sensor{abs((int16_t)ID), OPEN_CLOSE}; + rf_transmitted_data_t sensor{id, RFST_OPEN_CLOSE}; sensor.value_1 = getBatteryLevelCharge() * 100; sensor.value_2 = digitalRead(PD2) ? OPEN : CLOSE; // Normally closed. // sensor.value_2 = digitalRead(PD2) ? CLOSE : OPEN; // Normally open. radio.powerUp(); radio.flush_tx(); - radio.write(&sensor, sizeof(transmitted_data_t)); + radio.write(&sensor, sizeof(rf_transmitted_data_t)); radio.powerDown(); sei(); } @@ -82,6 +77,26 @@ float getBatteryLevelCharge() return value; } +void loadConfig() +{ + cli(); + if (EEPROM.read(511) == 254) + EEPROM.get(0, id); + else + saveConfig(); + delay(50); + sei(); +} + +void saveConfig() +{ + cli(); + EEPROM.write(511, 254); + EEPROM.put(0, id); + delay(50); + sei(); +} + ISR(INT0_vect) { cli();