Initial commit
This commit is contained in:
145
src/menu.c
Normal file
145
src/menu.c
Normal file
@@ -0,0 +1,145 @@
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "CH58x_common.h"
|
||||
#include "CH58x_sys.h"
|
||||
#include "CH58xBLE_LIB.h"
|
||||
#include "usb/core.h"
|
||||
#include "led.h"
|
||||
#include "ble.h"
|
||||
#include "button.h"
|
||||
|
||||
#include "img/menu.xbm"
|
||||
|
||||
static void boop()
|
||||
{
|
||||
WWDG_ResetCfg(DISABLE);
|
||||
PFIC_DisableIRQ(TMR0_IRQn);
|
||||
PFIC_DisableIRQ(TMR3_IRQn);
|
||||
|
||||
// Stop wasting energy
|
||||
GPIOA_ModeCfg(GPIO_Pin_All, GPIO_ModeIN_Floating);
|
||||
GPIOB_ModeCfg(GPIO_Pin_All, GPIO_ModeIN_Floating);
|
||||
|
||||
GPIOA_ModeCfg(GPIO_Pin_1, GPIO_ModeIN_PD);
|
||||
while (GPIOA_ReadPortPin(GPIO_Pin_1)) DelayMs(1);
|
||||
|
||||
DelayMs(100);
|
||||
// 0 to 1 (pulldown, so implies being _pressed_)
|
||||
// however, this doesn't work?
|
||||
GPIOA_ITModeCfg(GPIO_Pin_1, GPIO_ITMode_HighLevel);
|
||||
PFIC_EnableIRQ(GPIO_A_IRQn);
|
||||
// Wait for shit to settle
|
||||
while (GPIOA_ReadPortPin(GPIO_Pin_1)) DelayMs(1);
|
||||
PWR_PeriphWakeUpCfg(ENABLE, RB_SLP_GPIO_WAKE, Long_Delay);
|
||||
|
||||
/* Good bye */
|
||||
LowPower_Shutdown(0);
|
||||
}
|
||||
|
||||
static void render_xbm(unsigned char *bits, struct row_buf *b) {
|
||||
uint16_t fb[45] = {0};
|
||||
int index = 0;
|
||||
for (int j = 0; j < 11; j++) {
|
||||
index = j * 48;
|
||||
for (int i = 0; i < 44; i++) {
|
||||
int bit = bits[index / 8] & (1 << (index % 8));
|
||||
if (!bit) fb[i] |= (1 << j);
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
display_make_buf_all(fb, b);
|
||||
}
|
||||
|
||||
int menu_index = 0;
|
||||
|
||||
static int btn0_pressed = 0;
|
||||
static int btn1_pressed = 0;
|
||||
|
||||
void menu_render(void) {
|
||||
char buffer[(48 * 11) / 8];
|
||||
memcpy(buffer, menu_bits, sizeof(buffer));
|
||||
int invert_index = 1 + menu_index * 13;
|
||||
for (int i = invert_index; i < (invert_index + 9); i++) {
|
||||
for (int j = 1; j < 10; j++)
|
||||
buffer[(i / 8) + (j * 48 / 8)] ^= (1 << (i % 8));
|
||||
}
|
||||
|
||||
int batt_percent = ((get_battery_percentage() + 7) * 10) / 100;
|
||||
int charge = is_charging() || is_plugged();
|
||||
for (int i = 39; i < 43; i++) {
|
||||
for (int j = 0; j < 10; j++) {
|
||||
int index = 10 - j;
|
||||
if (j <= batt_percent) {
|
||||
buffer[(i / 8) + (index * 48 / 8)] &= ~(1 << (i % 8));
|
||||
} else if (charge && (j - 2) <= batt_percent) {
|
||||
if (i % 2 == j % 2)
|
||||
buffer[(i / 8) + (index * 48 / 8)] &= ~(1 << (i % 8));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!ble_on) {
|
||||
#define PIX(x, y) buffer[((x) / 8) + ((y) * 48 / 8)] |= (1 << ((x) % 8))
|
||||
for (int i = 0; i < 11; i += 2) {
|
||||
PIX(13 + i, 0);
|
||||
PIX(13 + i, 10);
|
||||
PIX(13, i);
|
||||
PIX(23, i);
|
||||
}
|
||||
}
|
||||
|
||||
render_xbm(buffer, display_screen);
|
||||
display_flip();
|
||||
}
|
||||
|
||||
void menu_switch(void) {
|
||||
btn0_pressed = button_pressed[0];
|
||||
btn1_pressed = button_pressed[1];
|
||||
menu_index = 0;
|
||||
|
||||
menu_render();
|
||||
}
|
||||
|
||||
int menu_handler(void) {
|
||||
static int btldr_timer = 0;
|
||||
static int render_ticks = 0;
|
||||
render_ticks++;
|
||||
|
||||
if (button_pressed[1] && button_pressed[0]) {
|
||||
btldr_timer++;
|
||||
if (btldr_timer > 2000) asm volatile("j 0x00");
|
||||
} else {
|
||||
btldr_timer = 0;
|
||||
}
|
||||
|
||||
if (button_pressed[0] && !btn0_pressed && !button_pressed[1]) {
|
||||
menu_index = (menu_index + 1) % 3;
|
||||
menu_render();
|
||||
render_ticks = 0;
|
||||
}
|
||||
|
||||
if (button_pressed[1] && !btn1_pressed && !button_pressed[0]) {
|
||||
if (menu_index == 0) {
|
||||
return 0;
|
||||
} else if (menu_index == 1) {
|
||||
ble_toggle();
|
||||
menu_render();
|
||||
render_ticks = 0;
|
||||
} else if (menu_index == 2) {
|
||||
boop();
|
||||
}
|
||||
}
|
||||
|
||||
if (render_ticks > 125) {
|
||||
render_ticks = 0;
|
||||
menu_render();
|
||||
}
|
||||
|
||||
btn0_pressed = button_pressed[0];
|
||||
btn1_pressed = button_pressed[1];
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user