Skip to content

Commit

Permalink
WIP: battery indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
kienvo committed Sep 11, 2024
1 parent 68e4ce4 commit ec30c5a
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,61 @@ static void debug_init()
UART1_BaudRateCfg(921600);
}

uint16_t adcBuff[40];
static int read_batt()
{
int ret = 0;
/* adc 1 - pa5 */
PRINT("\n2.Single channel sampling...\n");
GPIOA_ModeCfg(GPIO_Pin_5, GPIO_ModeIN_Floating);
ADC_ExtSingleChSampInit(SampleFreq_3_2, ADC_PGA_0);

int16_t RoughCalib_Value = ADC_DataCalib_Rough(); // 用于计算ADC内部偏差,记录到全局变量 RoughCalib_Value中
PRINT("RoughCalib_Value =%d \n", RoughCalib_Value);

ADC_ChannelCfg(1);

int ratio = 4096 / 100;

for(int i = 0; i < 20; i++) {
adcBuff[i] = ADC_ExcutSingleConver() + RoughCalib_Value; // 连续采样20次
ret += adcBuff[i] / ratio;
}
for(int i = 0; i < 20; i++) {
PRINT("%d \n", adcBuff[i]); // 注意:由于ADC内部偏差的存在,当采样电压在所选增益范围极限附近的时候,可能会出现数据溢出的现象
}

return (ret / 20);
}

static int is_charging()
{
GPIOA_ModeCfg(GPIO_Pin_0, GPIO_ModeIN_PU);
return GPIOA_ReadPortPin(GPIO_Pin_0) == 0;
}

static void disp_bat_stt(int bat_percent, int col, int row)
{
xbm2fb(&bat_xbm, fb, col, row);
bat_percent /= 10;
for (int i=1; i <= bat_percent; i++) {
fb[col + i] = fb[col];
}
}

static void disp_charging()
{
while (1) {
read_batt();
disp_bat_stt(read_batt(), 7, 2);

if (is_charging())
ani_xbm_next_frame(&coffee_cup_xbm, fb, 25, 0);

DelayMs(300);
}
}

int main()
{
SetSysClock(CLK_SOURCE_PLL_60MHz);
Expand All @@ -327,6 +382,8 @@ int main()
PFIC_EnableIRQ(TMR0_IRQn);

bmlist_init(LED_COLS * 4);

disp_charging();

play_splash(&splash, 0, 0);

Expand Down
5 changes: 5 additions & 0 deletions src/res/bat-icon.xbm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#define bat_icon_width 13
#define bat_icon_height 7
static unsigned char bat_icon_bits[] = {
0xff, 0x0f, 0x01, 0x08, 0x01, 0x18, 0x01, 0x18, 0x01, 0x18, 0x01, 0x08,
0xff, 0x0f };
7 changes: 7 additions & 0 deletions src/res/coffee-cup.xbm
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#define coffee_cup_width 10
#define coffee_cup_height 22
static unsigned char coffee_cup_bits[] = {
0x88, 0x00, 0x44, 0x00, 0x48, 0x00, 0x00, 0x00, 0xfe, 0x00, 0x82, 0x03,
0x82, 0x02, 0x82, 0x02, 0x82, 0x01, 0x7c, 0x00, 0x00, 0x00, 0x10, 0x01,
0x88, 0x00, 0x48, 0x00, 0x00, 0x00, 0xfe, 0x00, 0x82, 0x03, 0x82, 0x02,
0x82, 0x02, 0x82, 0x01, 0x7c, 0x00, 0x00, 0x00 };
16 changes: 16 additions & 0 deletions src/resource.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "res/bluetooth.xbm"
#include "res/foss-asia-2.xbm"
#include "res/bat-icon.xbm"
#include "res/coffee-cup.xbm"

xbm_t bluetooth = {
.bits = bluetooth_bits,
Expand All @@ -15,4 +17,18 @@ xbm_t splash = {
.w = foss_asia_2_width,
.h = foss_asia_2_height,
.fh = 11
};

xbm_t bat_xbm = {
.bits = bat_icon_bits,
.w = bat_icon_width,
.h = bat_icon_height,
.fh = 11
};

xbm_t coffee_cup_xbm = {
.bits = coffee_cup_bits,
.w = coffee_cup_width,
.h = coffee_cup_height,
.fh = 11
};
2 changes: 2 additions & 0 deletions src/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@

extern xbm_t bluetooth;
extern xbm_t splash;
extern xbm_t bat_xbm;
extern xbm_t coffee_cup_xbm;

#endif /* __RES_H__ */

0 comments on commit ec30c5a

Please sign in to comment.