diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b03fa6..7024238 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file. This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log... ## [unreleased][unreleased] + - Added `hw battery` to get battery informartion (@xianglin1998) - Added `hw slot delete` to delete HF or LF out of a HF+LF slot (@augustozanellato) - Changed CLI prompt autocompletion, saved history and internal cmd registration (@szymex73) - Fixed SDK NFC IRQ handler busy loop (@doegox) diff --git a/firmware/application/src/app_cmd.c b/firmware/application/src/app_cmd.c index 3bb43eb..c8aa5ac 100644 --- a/firmware/application/src/app_cmd.c +++ b/firmware/application/src/app_cmd.c @@ -114,6 +114,15 @@ data_frame_tx_t* cmd_processor_get_animation_mode(uint16_t cmd, uint16_t status, return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 1, (uint8_t *)(&animation_mode)); } +data_frame_tx_t* cmd_processor_get_battery_info(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { + uint8_t resp[3] = { 0x00 }; + // set voltage + num_to_bytes(batt_lvl_in_milli_volts, 2, resp); + // set percentage + resp[2] = percentage_batt_lvl; + return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, sizeof(resp), resp); +} + #if defined(PROJECT_CHAMELEON_ULTRA) data_frame_tx_t* cmd_processor_14a_scan(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -755,6 +764,8 @@ static cmd_data_map_t m_data_cmd_map[] = { { DATA_CMD_SET_ANIMATION_MODE, NULL, cmd_processor_set_animation_mode, NULL }, { DATA_CMD_GET_ANIMATION_MODE, NULL, cmd_processor_get_animation_mode, NULL }, { DATA_CMD_GET_GIT_VERSION, NULL, cmd_processor_get_git_version, NULL }, + { DATA_CMD_GET_BATTERY_INFO, NULL, cmd_processor_get_battery_info, NULL }, + #if defined(PROJECT_CHAMELEON_ULTRA) diff --git a/firmware/application/src/ble_main.c b/firmware/application/src/ble_main.c index c51d87c..0b34cf0 100644 --- a/firmware/application/src/ble_main.c +++ b/firmware/application/src/ble_main.c @@ -575,7 +575,7 @@ void saadc_event_handler(nrf_drv_saadc_evt_t const * p_event) APP_ERROR_CHECK(err_code); batt_lvl_in_milli_volts = ADC_RESULT_IN_MILLI_VOLTS(adc_result) + 100; - NRF_LOG_INFO("batt_lvl_in_milli_volts: %d", batt_lvl_in_milli_volts); + // NRF_LOG_INFO("batt_lvl_in_milli_volts: %d", batt_lvl_in_milli_volts); percentage_batt_lvl = BATVOL2PERCENT(batt_lvl_in_milli_volts); // if battery service is notification enable, we can send msg to device. diff --git a/firmware/application/src/ble_main.h b/firmware/application/src/ble_main.h index 875eebf..5182c7b 100644 --- a/firmware/application/src/ble_main.h +++ b/firmware/application/src/ble_main.h @@ -6,6 +6,9 @@ #include "ble_bas.h" +extern uint16_t batt_lvl_in_milli_volts; +extern uint8_t percentage_batt_lvl; + void ble_slave_init(void); void advertising_start(void); void nus_data_reponse(uint8_t *p_data, uint16_t length); diff --git a/firmware/application/src/data_cmd.h b/firmware/application/src/data_cmd.h index 72ed13f..aff644b 100644 --- a/firmware/application/src/data_cmd.h +++ b/firmware/application/src/data_cmd.h @@ -30,6 +30,7 @@ #define DATA_CMD_WIPE_FDS (1020) #define DATA_CMD_GET_ENABLED_SLOTS (1023) #define DATA_CMD_DELETE_SLOT_SENSE_TYPE (1024) +#define DATA_CMD_GET_BATTERY_INFO (1025) // // ****************************************************************** diff --git a/software/script/chameleon_cli_unit.py b/software/script/chameleon_cli_unit.py index d4f7808..e8a1fa1 100644 --- a/software/script/chameleon_cli_unit.py +++ b/software/script/chameleon_cli_unit.py @@ -1265,3 +1265,22 @@ class HWFactoryReset(DeviceRequiredUnit): print(" - A Serial Error below is normal, please ignore it") else: print(" - Reset failed!") + + +@hw.command('battery', 'Get battery information, voltage and level.') +class HWBatteryInfo(DeviceRequiredUnit): + # How much remaining battery is considered low? + BATTERY_LOW_LEVEL = 30 + + def args_parser(self) -> ArgumentParserNoExit: + return None + + def on_exec(self, args: argparse.Namespace): + resp = self.cmd.battery_informartion() + voltage = int.from_bytes(resp.data[:2], 'big') + percentage = resp.data[2] + print(" - Battery informartion:") + print(f" voltage -> {voltage}mV") + print(f" percentage -> {percentage}%") + if percentage < HWBatteryInfo.BATTERY_LOW_LEVEL: + print(f"{colorama.Fore.RED}[!] Low battery, please charge.{colorama.Style.RESET_ALL}") diff --git a/software/script/chameleon_cmd.py b/software/script/chameleon_cmd.py index 1f1cf3f..b8ea4a1 100644 --- a/software/script/chameleon_cmd.py +++ b/software/script/chameleon_cmd.py @@ -37,6 +37,8 @@ DATA_CMD_WIPE_FDS = 1020 DATA_CMD_GET_ENABLED_SLOTS = 1023 DATA_CMD_DELETE_SLOT_SENSE_TYPE = 1024 +DATA_CMD_GET_BATTERY_INFO = 1025 + DATA_CMD_SCAN_14A_TAG = 2000 DATA_CMD_MF1_SUPPORT_DETECT = 2001 DATA_CMD_MF1_NT_LEVEL_DETECT = 2002 @@ -671,6 +673,13 @@ class ChameleonCMD: Reset to factory settings """ return self.device.send_cmd_sync(DATA_CMD_WIPE_FDS, 0x00) + + @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + def battery_informartion(self): + """ + Get battery info + """ + return self.device.send_cmd_sync(DATA_CMD_GET_BATTERY_INFO, 0x00) if __name__ == '__main__':