Skip to content

Commit

Permalink
Added "Screen Off" option + small optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
pvvx committed Mar 22, 2023
1 parent edeb377 commit d1d2c32
Show file tree
Hide file tree
Showing 22 changed files with 157 additions and 82 deletions.
Binary file modified ATC_v43.bin
Binary file not shown.
Binary file modified BTH_v43.bin
Binary file not shown.
Binary file modified CGDK2_v43.bin
Binary file not shown.
Binary file modified CGG1M_v43.bin
Binary file not shown.
Binary file modified CGG1_v43.bin
Binary file not shown.
Binary file modified MHO_C401N_v43.bin
Binary file not shown.
Binary file modified MHO_C401_v43.bin
Binary file not shown.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ In case you want to go back to the original firmware, you can download them here
| 4.0 | Improved stability in connected mode. I2C bus CLK and PullUp correction.|
| 4.1 | Changed "connection latency" for [stability in connected](https://github.com/pvvx/ATC_MiThermometer/issues/265#issuecomment-1431495494).|
| 4.2 | Option Increasing Communication Distance: Bluetooth 5.0 LE Long Range (Advertising Extensions: primary and secondary Coded PHY S=8, Connectable). Added support [MHO-C401 (2022.11)](https://pvvx.github.io/MHO_C401N) and [MJWSD05MMC](https://pvvx.github.io/MJWSD05MMC). Added function key: Connect |
| 4.3 | Increasing the degree of averaging (x64) of the sent values over the battery. |
| 4.3 | Increasing the degree of averaging (x64) of the sent values over the battery. Added "Screen Off" option. |

## Applications

Expand Down
79 changes: 40 additions & 39 deletions src/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,13 @@ void set_hw_version(void) {
#endif
}

// go deep-sleep
void go_sleep(uint32_t tik) {
cpu_sleep_wakeup(DEEPSLEEP_MODE, PM_WAKEUP_TIMER,
clock_time() + tik);
while(1);
}

__attribute__((optimize("-Os")))
void test_config(void) {
if (cfg.flg2.longrange)
Expand Down Expand Up @@ -341,9 +348,7 @@ void low_vbat(void) {
while(task_lcd()) pm_wait_ms(10);
#endif
#endif
cpu_sleep_wakeup(DEEPSLEEP_MODE, PM_WAKEUP_TIMER,
clock_time() + 120 * CLOCK_16M_SYS_TIMER_CLK_1S); // go deep-sleep 2 minutes
while(1);
go_sleep(120 * CLOCK_16M_SYS_TIMER_CLK_1S); // go deep-sleep 2 minutes
}

_attribute_ram_code_
Expand Down Expand Up @@ -402,8 +407,6 @@ void WakeupLowPowerCb(int par) {
_attribute_ram_code_
static void suspend_exit_cb(u8 e, u8 *p, int n) {
(void) e; (void) p; (void) n;
if (timer_measure_cb)
init_i2c();
rf_set_power_level_index(cfg.rf_tx_power);
}

Expand Down Expand Up @@ -470,14 +473,14 @@ static void start_tst_battery(void) {
uint16_t avr_mv = get_battery_mv();
measured_data.battery_mv = avr_mv;
if (avr_mv < MIN_VBAT_MV) { // 2.2V
#if (DEVICE_TYPE == DEVICE_LYWSD03MMC) || (DEVICE_TYPE == DEVICE_CGDK2) || (DEVICE_TYPE == DEVICE_MJWSD05MMC)
// Set sleep power < 1 uA
#if (DEVICE_TYPE == DEVICE_LYWSD03MMC)
// Set LYWSD03MMC sleep < 1 uA
init_i2c();
send_i2c_word(0x70 << 1, 0x98b0); // SHTC3 go SLEEP: Sleep command of the sensor
#endif
send_i2c_byte(0x3E << 1, 0xEA); // BU9792AFUV reset
#endif
cpu_sleep_wakeup(DEEPSLEEP_MODE, PM_WAKEUP_TIMER,
clock_time() + 120 * CLOCK_16M_SYS_TIMER_CLK_1S); // go deep-sleep 2 minutes
go_sleep(120 * CLOCK_16M_SYS_TIMER_CLK_1S); // go deep-sleep 2 minutes
}
measured_data.average_battery_mv = avr_mv;
for(i = 0; i < BAT_AVERAGE_COUNT; i++)
Expand Down Expand Up @@ -526,10 +529,7 @@ void set_default_cfg(void) {
cfg.flg.lp_measures = 1;
flash_write_cfg(&cfg, EEP_ID_CFG, sizeof(cfg));
SHOW_REBOOT_SCREEN();
cpu_sleep_wakeup(DEEPSLEEP_MODE, PM_WAKEUP_TIMER,
clock_time() + 2*CLOCK_16M_SYS_TIMER_CLK_1S); // go deep-sleep 2 sec
while(1)
start_reboot();
go_sleep(2*CLOCK_16M_SYS_TIMER_CLK_1S); // go deep-sleep 2 sec
}
#endif // GPIO_KEY2 || USE_WK_RDS_COUNTER

Expand Down Expand Up @@ -614,8 +614,7 @@ void user_init_normal(void) {//this will get executed one time after power up
SHOW_REBOOT_SCREEN();
#endif // POWERUP_SCREEN
// RTC wakes up after powering on > 1 second.
cpu_sleep_wakeup(DEEPSLEEP_MODE, PM_WAKEUP_TIMER,
clock_time() + 1500*CLOCK_16M_SYS_TIMER_CLK_1MS); // go deep-sleep 1.5 sec
go_sleep(1500*CLOCK_16M_SYS_TIMER_CLK_1MS); // go deep-sleep 1.5 sec
#endif // SHOW_REBOOT_SCREEN || (USE_RTC)
#endif // POWERUP_SCREEN || (USE_RTC) || (BLE_EXT_ADV)
}
Expand Down Expand Up @@ -792,7 +791,7 @@ void main_loop(void) {
#endif
} else {
bls_pm_setAppWakeupLowPower(0, 0); // clear callback
if ((blc_ll_getCurrentState() & BLS_LINK_STATE_CONN) && blc_ll_getTxFifoNumber() < 9) {
if (ble_connected && blc_ll_getTxFifoNumber() < 9) {
// connect, TxFifo ready
if (end_measure & 1) {
end_measure &= ~1;
Expand Down Expand Up @@ -833,33 +832,35 @@ void main_loop(void) {
tim_measure = new;
start_measure = 1;
}
if(!cfg.flg2.screen_off) {
#if (USE_EPD)
if ((!stage_lcd) && (new - lcd_flg.tim_last_chow >= lcd_flg.min_step_time_update_lcd)) {
lcd_flg.tim_last_chow = new;
lcd_flg.show_stage++;
if(lcd_flg.update_next_measure) {
lcd_flg.update = end_measure & 2;
end_measure &= ~2;
} else
lcd_flg.update = 1;
}
if ((!stage_lcd) && (new - lcd_flg.tim_last_chow >= lcd_flg.min_step_time_update_lcd)) {
lcd_flg.tim_last_chow = new;
lcd_flg.show_stage++;
if(lcd_flg.update_next_measure) {
lcd_flg.update = end_measure & 2;
end_measure &= ~2;
} else
lcd_flg.update = 1;
}
#elif (DEVICE_TYPE != DEVICE_MJWSD05MMC)
if (new - lcd_flg.tim_last_chow >= lcd_flg.min_step_time_update_lcd) {
lcd_flg.tim_last_chow = new;
lcd_flg.show_stage++;
if(lcd_flg.update_next_measure) {
lcd_flg.update = end_measure & 2;
end_measure &= ~2;
} else
lcd_flg.update = 1;
}
if (new - lcd_flg.tim_last_chow >= lcd_flg.min_step_time_update_lcd) {
lcd_flg.tim_last_chow = new;
lcd_flg.show_stage++;
if(lcd_flg.update_next_measure) {
lcd_flg.update = end_measure & 2;
end_measure &= ~2;
} else
lcd_flg.update = 1;
}
#endif
if (lcd_flg.update) {
lcd_flg.update = 0;
if (!lcd_flg.b.ext_data_buf) { // LCD show external data ? No
lcd();
if (lcd_flg.update) {
lcd_flg.update = 0;
if (!lcd_flg.b.ext_data_buf) { // LCD show external data ? No
lcd();
}
update_lcd();
}
update_lcd();
}
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,9 @@ enum {
} ADV_TYPE_ENUM;
#define ADV_TYPE_DEFAULT ADV_TYPE_PVVX

#if (DEVICE_TYPE != DEVICE_MJWSD05MMC)
#define MASK_FLG2_REBOOT 0xe0
#define MASK_FLG2_EXT_ADV 0xc0
#else
#define MASK_FLG2_REBOOT 0x60
#endif
#define MASK_FLG2_SCR_OFF 0x80

typedef struct __attribute__((packed)) _cfg_t {
struct __attribute__((packed)) {
uint8_t advertising_type : 2; // 0 - atc1441, 1 - Custom (pvvx), 2 - Mi, 3 - HA_BLE
Expand Down Expand Up @@ -102,7 +99,7 @@ typedef struct __attribute__((packed)) _cfg_t {
uint8_t adv_flags : 1; // advertising add flags
uint8_t bt5phy : 1; // support BT5.0 All PHY
uint8_t longrange : 1; // advertising in LongRange mode (сбрасывается после отключения питания)
uint8_t reserved : 1;
uint8_t screen_off : 1; // screen off, v4.3+
} flg2;
int8_t temp_offset; // Set temp offset, -12,5 - +12,5 °C (-125..125)
int8_t humi_offset; // Set humi offset, -12,5 - +12,5 % (-125..125)
Expand Down
1 change: 0 additions & 1 deletion src/app_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ extern "C" {
#define DIY_ADC_TO_TH 0

#define USE_MIHOME_SERVICE 0 // = 1 MiHome service compatibility (missing in current version! Set = 0!)
#define USE_NEW_OTA 0 // = 1 keeping the old firmware, erasing the region when updating (test version only!)
#define UART_PRINT_DEBUG_ENABLE 0 // =1 use u_printf() (PA7/SWS), source: SDK/components/application/print/u_printf.c

#if DEVICE_TYPE == DEVICE_MHO_C401
Expand Down
10 changes: 3 additions & 7 deletions src/ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ RAM adv_buf_t adv_buf;
RAM uint8_t ota_is_working; // OTA_STAGES: =1 ota enabled, =2 - ota wait, = 0xff flag ext.ota

void app_enter_ota_mode(void) {
#if USE_NEW_OTA
bls_ota_clearNewFwDataArea();
#endif
#if USE_EXT_OTA
if(ota_is_working != OTA_EXTENDED)
#endif
Expand Down Expand Up @@ -245,10 +242,12 @@ _attribute_ram_code_ int RxTxWrite(void * p) {
return 0;
}

/* moved app.c: suspend_exit_cb
_attribute_ram_code_ void user_set_rf_power(u8 e, u8 *p, int n) {
(void) e; (void) p; (void) n;
rf_set_power_level_index(cfg.rf_tx_power);
}
*/

#if (BLE_EXT_ADV)
/* cfg.rf_tx_power -> ext.advertising TX power */
Expand Down Expand Up @@ -511,7 +510,6 @@ __attribute__((optimize("-Os"))) void init_ble(void) {
blc_ll_initAdvertising_module(mac_public); // adv module: must for BLE slave,
blc_ll_initConnection_module(); // connection module must for BLE slave/master
blc_ll_initSlaveRole_module(); // slave module: must for BLE slave,
blc_ll_initPowerManagement_module(); //pm module: optional
#if (BLE_EXT_ADV)
adv_buf.ext_adv_init = 0;
#endif
Expand Down Expand Up @@ -604,7 +602,7 @@ __attribute__((optimize("-Os"))) void init_ble(void) {

///////////////////// USER application initialization ///////////////////
rf_set_power_level_index(cfg.rf_tx_power);
bls_app_registerEventCallback(BLT_EV_FLAG_SUSPEND_EXIT, &user_set_rf_power);
// bls_app_registerEventCallback(BLT_EV_FLAG_SUSPEND_EXIT, &user_set_rf_power); // -> app.c: suspend_exit_cb
bls_app_registerEventCallback(BLT_EV_FLAG_CONNECT, &ble_connect_callback);
bls_app_registerEventCallback(BLT_EV_FLAG_TERMINATE, &ble_disconnect_callback);

Expand All @@ -614,9 +612,7 @@ __attribute__((optimize("-Os"))) void init_ble(void) {
blc_pm_setDeepsleepRetentionThreshold(40, 18);
blc_pm_setDeepsleepRetentionEarlyWakeupTiming(240);
blc_pm_setDeepsleepRetentionType(DEEPSLEEP_MODE_RET_SRAM_LOW32K);
#if USE_NEW_OTA == 0
bls_ota_clearNewFwDataArea();
#endif
bls_ota_registerStartCmdCb(app_enter_ota_mode);
blc_l2cap_registerConnUpdateRspCb(app_conn_param_update_response);
bls_set_advertise_prepare(app_advertise_prepare_handler); // TODO: not work if EXTENDED_ADVERTISING
Expand Down
20 changes: 13 additions & 7 deletions src/cmd_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ void cmd_parser(void * p) {
}
ble_send_ext();
} else if (cmd == CMD_ID_CFG || cmd == CMD_ID_CFG_NS) { // Get/set config
if (--len > sizeof(cfg)) len = sizeof(cfg);
u8 tmp = ((volatile u8 *)&cfg.flg2)[0];
if (--len > sizeof(cfg)) len = sizeof(cfg);
if (len) {
memcpy(&cfg, &req->dat[1], len);
#if (DEVICE_TYPE == DEVICE_MJWSD05MMC)
Expand All @@ -297,23 +297,29 @@ void cmd_parser(void * p) {
#endif
}
test_config();
// set_hw_version();
tmp ^= ((volatile u8 *)&cfg.flg2)[0];
if(tmp & MASK_FLG2_SCR_OFF)
init_lcd();
ev_adv_timeout(0, 0, 0);
if (cmd != CMD_ID_CFG_NS) { // Get/set config (not save to Flash)
flash_write_cfg(&cfg, EEP_ID_CFG, sizeof(cfg));
if((tmp ^ ((volatile u8 *)&cfg.flg2)[0]) & MASK_FLG2_REBOOT) { // (cfg.flg2.bt5phy || cfg.flg2.ext_adv)
if(tmp & MASK_FLG2_REBOOT) { // (cfg.flg2.bt5phy || cfg.flg2.ext_adv)
ble_connected |= BIT(CONNECTED_FLG_RESET_OF_DISCONNECT); // reset device on disconnect
}
flash_write_cfg(&cfg, EEP_ID_CFG, sizeof(cfg));
}
ble_send_cfg();
} else if (cmd == CMD_ID_CFG_DEF) { // Set default config
if((((u8 *)&cfg.flg2)[0] ^ ((u8 *)&def_cfg.flg2)[0]) & MASK_FLG2_REBOOT) { // (cfg.flg2.bt5phy || cfg.flg2.ext_adv)
ble_connected |= BIT(CONNECTED_FLG_RESET_OF_DISCONNECT); // reset device on disconnect
}
u8 tmp = ((volatile u8 *)&cfg.flg2)[0];
memcpy(&cfg, &def_cfg, sizeof(cfg));
test_config();
if (!cfg.hw_cfg.shtc3) // sensor SHT4x ?
cfg.flg.lp_measures = 1;
tmp ^= ((volatile u8 *)&cfg.flg2)[0];
if(tmp & MASK_FLG2_REBOOT) { // (cfg.flg2.bt5phy || cfg.flg2.ext_adv)
ble_connected |= BIT(CONNECTED_FLG_RESET_OF_DISCONNECT); // reset device on disconnect
}
if(tmp & MASK_FLG2_SCR_OFF)
init_lcd();
ev_adv_timeout(0, 0, 0);
flash_write_cfg(&cfg, EEP_ID_CFG, sizeof(cfg));
ble_send_cfg();
Expand Down
8 changes: 8 additions & 0 deletions src/epd_cgg1.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,10 @@ void show_batt_cgg1(void) {
}

_attribute_ram_code_ void update_lcd(void){
if(cfg.flg2.screen_off) {
stage_lcd = 0;
return;
}
if (!stage_lcd) {
if (memcmp(display_cmp_buff, display_buff, sizeof(display_buff))) {
memcpy(display_cmp_buff, display_buff, sizeof(display_buff));
Expand All @@ -349,6 +353,10 @@ _attribute_ram_code_ void update_lcd(void){
}

_attribute_ram_code_ __attribute__((optimize("-Os"))) int task_lcd(void) {
if(cfg.flg2.screen_off) {
stage_lcd = 0;
return stage_lcd;
}
if (gpio_read(EPD_BUSY)) {
switch (stage_lcd) {
case 1: // Update/Init lcd, stage 1
Expand Down
8 changes: 8 additions & 0 deletions src/epd_cgg1n.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,10 @@ _attribute_ram_code_ static void transmit_blk(uint8_t cd, const uint8_t * pdata,
}

_attribute_ram_code_ void update_lcd(void){
if(cfg.flg2.screen_off) {
stage_lcd = 0;
return;
}
if (!stage_lcd) {
if (memcmp(display_cmp_buff, display_buff, sizeof(display_buff))) {
memcpy(display_cmp_buff, display_buff, sizeof(display_buff));
Expand Down Expand Up @@ -381,6 +385,10 @@ void init_lcd(void) {
}

_attribute_ram_code_ __attribute__((optimize("-Os"))) int task_lcd(void) {
if(cfg.flg2.screen_off) {
stage_lcd = 0;
return stage_lcd;
}
if (gpio_read(EPD_BUSY)) {
switch (stage_lcd) {
case 1: // Update/Init, stage 1
Expand Down
8 changes: 8 additions & 0 deletions src/epd_mho_c401.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,10 @@ void init_lcd(void) {
}

_attribute_ram_code_ void update_lcd(void){
if(cfg.flg2.screen_off) {
stage_lcd = 0;
return;
}
if (!stage_lcd) {
if (memcmp(display_cmp_buff, display_buff, sizeof(display_buff))) {
memcpy(display_cmp_buff, display_buff, sizeof(display_buff));
Expand All @@ -327,6 +331,10 @@ _attribute_ram_code_ void update_lcd(void){
}

_attribute_ram_code_ __attribute__((optimize("-Os"))) int task_lcd(void) {
if(cfg.flg2.screen_off) {
stage_lcd = 0;
return stage_lcd;
}
if (gpio_read(EPD_BUSY)) {
switch (stage_lcd) {
case 1: // Update/Init lcd, stage 1
Expand Down
8 changes: 8 additions & 0 deletions src/epd_mho_c401n.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,10 @@ void init_lcd(void) {

_attribute_ram_code_
void update_lcd(void){
if(cfg.flg2.screen_off) {
stage_lcd = 0;
return;
}
if (!stage_lcd) {
if (memcmp(display_cmp_buff, display_buff, sizeof(display_buff))) {
memcpy(display_cmp_buff, display_buff, sizeof(display_buff));
Expand All @@ -368,6 +372,10 @@ void update_lcd(void){

_attribute_ram_code_
__attribute__((optimize("-Os"))) int task_lcd(void) {
if(cfg.flg2.screen_off) {
stage_lcd = 0;
return stage_lcd;
}
if (gpio_read(EPD_BUSY)) {
switch (stage_lcd) {
case 1: // Update/Init, stage 1
Expand Down
4 changes: 4 additions & 0 deletions src/lcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ RAM uint8_t display_buff[LCD_BUF_SIZE], display_cmp_buff[LCD_BUF_SIZE];
#if (!USE_EPD)
_attribute_ram_code_
void update_lcd(void){
if(cfg.flg2.screen_off)
return;
if (memcmp(&display_cmp_buff, &display_buff, sizeof(display_buff))) {
send_to_lcd();
memcpy(&display_cmp_buff, &display_buff, sizeof(display_buff));
Expand All @@ -50,6 +52,8 @@ uint8_t is_comfort(int16_t t, uint16_t h) {
_attribute_ram_code_
__attribute__((optimize("-Os")))
void lcd(void) {
if(cfg.flg2.screen_off)
return;
bool set_small_number_and_bat = true;

#if defined(GPIO_KEY2) || USE_WK_RDS_COUNTER
Expand Down
Loading

0 comments on commit d1d2c32

Please sign in to comment.