Initial commit
This commit is contained in:
136
src/main.c
Normal file
136
src/main.c
Normal file
@@ -0,0 +1,136 @@
|
||||
#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 "button.h"
|
||||
#include "menu.h"
|
||||
#include "ble.h"
|
||||
#include "cdc.h"
|
||||
#include "wang.h"
|
||||
|
||||
int anim_render(uint16_t *fb, int index, int frame);
|
||||
|
||||
enum usb_control_resp bl_handler(enum usb_control_state state) {
|
||||
if (usb_control_request.bmRequestType != 0x60) return USB_CONTROL_RESP_PASS;
|
||||
|
||||
if (usb_control_request.bRequest != 0x69)
|
||||
return USB_CONTROL_RESP_STALL;
|
||||
|
||||
asm volatile("j 0x00");
|
||||
return USB_CONTROL_RESP_ACK;
|
||||
}
|
||||
|
||||
|
||||
int btn1_was_pressed = 1;
|
||||
int btn2_was_pressed = 0;
|
||||
int btn1_hold = 0;
|
||||
int btn2_hold = 0;
|
||||
|
||||
int image_index = 0;
|
||||
|
||||
int frame = 0;
|
||||
int subframe = 0;
|
||||
|
||||
int main_handler(void) {
|
||||
|
||||
#define TIMER(btn0, btn1, v, count) if (button_pressed[0] != btn0 || button_pressed[1] != btn1) { v = 0; } else if (v < count) { v += 1; } else
|
||||
static int menu_timer = 0;
|
||||
TIMER(1, 0, menu_timer, 500) {
|
||||
display_brightness = (display_brightness + 15) % 16;
|
||||
menu_timer = 0;
|
||||
menu_switch();
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (button_pressed[0] && !btn1_was_pressed) {
|
||||
display_brightness = (display_brightness + 1) % 16;
|
||||
}
|
||||
|
||||
if (button_pressed[1] && !btn2_was_pressed) {
|
||||
if (flash_header_valid) {
|
||||
image_index++;
|
||||
if (!flash_header.widths[image_index]) image_index = 0;
|
||||
frame = 0;
|
||||
subframe = 99999;
|
||||
}
|
||||
display_flip();
|
||||
}
|
||||
|
||||
subframe++;
|
||||
if (subframe > 32 && flash_header_valid) {
|
||||
// approximately 60fps
|
||||
subframe = 0;
|
||||
|
||||
uint16_t fb[44];
|
||||
if (anim_render(fb, image_index, frame)) {
|
||||
frame = 0;
|
||||
} else {
|
||||
frame++;
|
||||
}
|
||||
display_make_buf_all(fb, display_screen);
|
||||
display_flip();
|
||||
}
|
||||
|
||||
btn2_was_pressed = button_pressed[1];
|
||||
btn1_was_pressed = button_pressed[0];
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
TMR3_TimerInit(FREQ_SYS / 200);
|
||||
TMR3_ITCfg(ENABLE, TMR0_3_IT_CYC_END);
|
||||
PFIC_EnableIRQ(TMR3_IRQn);
|
||||
|
||||
usb_register_handler(bl_handler);
|
||||
usb_init();
|
||||
|
||||
ble_init();
|
||||
|
||||
wang_init();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
static int cur_handler = 0;
|
||||
if (cur_handler) {
|
||||
cur_handler = menu_handler();
|
||||
if (!cur_handler) {
|
||||
btn2_was_pressed = button_pressed[1];
|
||||
btn1_was_pressed = button_pressed[0];
|
||||
subframe = 99999;
|
||||
}
|
||||
} else
|
||||
cur_handler = main_handler();
|
||||
|
||||
TMOS_SystemProcess();
|
||||
cdc_tick();
|
||||
hid_tick();
|
||||
DelayMs(1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user