Add command to get battery information #38

This commit is contained in:
dxl
2023-08-22 12:05:48 +08:00
parent 448188af7a
commit 68a3e463f1
7 changed files with 45 additions and 1 deletions
+1
View File
@@ -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)
+11
View File
@@ -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)
+1 -1
View File
@@ -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.
+3
View File
@@ -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);
+1
View File
@@ -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)
//
// ******************************************************************
+19
View File
@@ -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}")
+9
View File
@@ -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__':