Files
bagdewitch/src/main.c
2026-02-08 00:04:14 +00:00

125 lines
3.2 KiB
C

#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 "input.h"
#include "menu.h"
#include "ble.h"
#include "cdc.h"
#include "wang.h"
#include "platform.h"
#include "screen.h"
enum usb_control_resp bl_handler(enum usb_control_state state) {
if ((usb_control_request.bmRequestType & 0x7f) != 0x43) return USB_CONTROL_RESP_PASS;
if (usb_control_request.bRequest != 0x69)
return USB_CONTROL_RESP_STALL;
if (state == USB_CONTROL_FINISHED) asm volatile("j 0x00");
return USB_CONTROL_RESP_ACK;
}
static int button_was_pressed[BUTTON_COUNT] = {0};
static int button_hold[BUTTON_COUNT] = {0};
extern struct screen badge_screen;
int main()
{
SetSysClock(CLK_SOURCE_PLL_60MHz);
button_init();
display_init();
WWDG_SetCounter(0x7F);
WWDG_ResetCfg(ENABLE);
TMR0_TimerInit((FREQ_SYS / 16000) / 2);
TMR0_ITCfg(ENABLE, TMR0_3_IT_CYC_END);
PFIC_EnableIRQ(TMR0_IRQn);
TMR1_TimerInit((FREQ_SYS / 1000));
TMR1_ITCfg(ENABLE, TMR0_3_IT_CYC_END);
TMR3_TimerInit(FREQ_SYS / 200);
TMR3_ITCfg(ENABLE, TMR0_3_IT_CYC_END);
PFIC_EnableIRQ(TMR3_IRQn);
wang_init();
usb_register_handler(bl_handler);
usb_init();
ble_init();
screen_push(&badge_screen);
// set SEVONPEND
PFIC->SCTLR |= (1 << 4);
while (1) {
WWDG_SetCounter(0x7F);
static int btldr_timer = 0;
if (button_pressed[1] && button_pressed[0]) {
btldr_timer++;
if (btldr_timer > 1000) asm volatile("j 0x00");
} else {
btldr_timer = 0;
}
struct screen_event ev = {
SE_TICK,
0
};
current_screen->event_handler(ev);
for (int i = 0; i < BUTTON_COUNT; i++) {
int is_pressed = button_pressed[i];
if (button_was_pressed[i] && is_pressed) {
button_hold[i]++;
if (button_hold[i] == 500) {
ev.type = SE_BUTTON_HOLD;
ev.data = i;
current_screen->event_handler(ev);
} else if (button_hold[i] > 500) {
button_hold[i] = 501;
}
} else if (is_pressed && !button_was_pressed[i]) {
ev.type = SE_BUTTON_PRESS;
ev.data = i;
current_screen->event_handler(ev);
button_was_pressed[i] = 1;
button_hold[i] = 0;
} else if (!is_pressed && button_was_pressed[i]) {
ev.type = button_hold[i] >= 500 ? SE_BUTTON_RELEASE_HOLD : SE_BUTTON_RELEASE_SHORT;
ev.data = i;
current_screen->event_handler(ev);
button_was_pressed[i] = 0;
}
}
if (screen_dirty) {
screen_dirty = 0;
current_screen->draw_handler(display_screen);
display_flip();
}
TMOS_SystemProcess();
cdc_tick();
hid_tick();
while (!TMR1_GetITFlag(TMR0_3_IT_CYC_END)) {
__SEV();
__asm__ volatile("wfi");
}
TMR1_ClearITFlag(TMR0_3_IT_CYC_END);
}
}