mirror of
https://github.com/whowechina/mai_pico.git
synced 2026-10-08 14:58:07 +03:00
139 lines
2.9 KiB
C
139 lines
2.9 KiB
C
/*
|
|
* Controller Main
|
|
* WHowe <github.com/whowechina>
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
|
|
#include "pico/stdio.h"
|
|
#include "pico/stdlib.h"
|
|
#include "bsp/board.h"
|
|
#include "pico/multicore.h"
|
|
#include "pico/bootrom.h"
|
|
|
|
#include "hardware/gpio.h"
|
|
#include "hardware/sync.h"
|
|
#include "hardware/structs/ioqspi.h"
|
|
#include "hardware/structs/sio.h"
|
|
|
|
#include "tusb.h"
|
|
#include "usb_descriptors.h"
|
|
|
|
#include "board_defs.h"
|
|
|
|
#include "touch.h"
|
|
#include "button.h"
|
|
#include "rgb.h"
|
|
|
|
#include "save.h"
|
|
#include "config.h"
|
|
#include "cli.h"
|
|
#include "commands.h"
|
|
#include "io.h"
|
|
#include "hid.h"
|
|
|
|
static void run_lights()
|
|
{
|
|
uint64_t now = time_us_64();
|
|
if (io_last_io_time() != 0 && now - io_last_io_time() < 60000000) {
|
|
return;
|
|
}
|
|
|
|
uint16_t buttons = button_read();
|
|
for (int i = 0; i < 8; i++) {
|
|
uint32_t color = mai_cfg->color.key_off;
|
|
if (buttons & (1 << i)) {
|
|
color = mai_cfg->color.key_on;
|
|
}
|
|
rgb_set_button_color(i, color);
|
|
}
|
|
}
|
|
|
|
static mutex_t core1_io_lock;
|
|
static void core1_loop()
|
|
{
|
|
while (1) {
|
|
if (mutex_try_enter(&core1_io_lock, NULL)) {
|
|
run_lights();
|
|
rgb_update();
|
|
mutex_exit(&core1_io_lock);
|
|
}
|
|
cli_fps_count(1);
|
|
sleep_ms(1);
|
|
}
|
|
}
|
|
|
|
static void core0_loop()
|
|
{
|
|
static uint64_t next_frame = 0;
|
|
|
|
while(1) {
|
|
tud_task();
|
|
io_update();
|
|
|
|
cli_run();
|
|
save_loop();
|
|
cli_fps_count(0);
|
|
|
|
sleep_until(next_frame);
|
|
next_frame = time_us_64() + 1000; // 1KHz
|
|
|
|
touch_update();
|
|
button_update();
|
|
|
|
hid_update();
|
|
}
|
|
}
|
|
|
|
void init()
|
|
{
|
|
sleep_ms(50);
|
|
set_sys_clock_khz(150000, true);
|
|
board_init();
|
|
|
|
tusb_init();
|
|
stdio_init_all();
|
|
|
|
config_init();
|
|
mutex_init(&core1_io_lock);
|
|
|
|
save_init(board_id_32() ^ 0xcafe1111, &core1_io_lock);
|
|
|
|
touch_init();
|
|
button_init();
|
|
rgb_init();
|
|
|
|
cli_init("mai_pico>", "\n << Mai Pico Controller >>\n"
|
|
" https://github.com/whowechina\n\n");
|
|
commands_init();
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
init();
|
|
multicore_launch_core1(core1_loop);
|
|
core0_loop();
|
|
return 0;
|
|
}
|
|
|
|
// Invoked when received GET_REPORT control request
|
|
// Application must fill buffer report's content and return its length.
|
|
// Return zero will cause the stack to STALL request
|
|
uint16_t tud_hid_get_report_cb(uint8_t itf, uint8_t report_id,
|
|
hid_report_type_t report_type, uint8_t *buffer,
|
|
uint16_t reqlen)
|
|
{
|
|
printf("Get from USB %d-%d\n", report_id, report_type);
|
|
return 0;
|
|
}
|
|
|
|
// Invoked when received SET_REPORT control request or
|
|
// received data on OUT endpoint ( Report ID = 0, Type = 0 )
|
|
void tud_hid_set_report_cb(uint8_t itf, uint8_t report_id,
|
|
hid_report_type_t report_type, uint8_t const *buffer,
|
|
uint16_t bufsize)
|
|
{
|
|
}
|