Kinda works with game

This commit is contained in:
whowechina 2024-09-16 18:14:29 +08:00
parent 1aca164816
commit ec39e8f8fa
5 changed files with 170 additions and 97 deletions

View File

@ -29,10 +29,10 @@ static struct {
uint16_t out_low;
uint16_t out_high;
} key_defs[] = {
{ 0, 120, 250, 100, 270 },
{ 1, 120, 250, 100, 270 },
{ 0, 400, 500, 380, 520 },
{ 1, 400, 500, 380, 520 }
{ 0, 50, 200, 30, 220 },
{ 1, 50, 200, 30, 220 },
{ 0, 300, 400, 280, 430 },
{ 1, 300, 400, 280, 430 }
};
#define AIRKEY_NUM (count_of(key_defs))
@ -71,9 +71,11 @@ static bool readings[AIRKEY_NUM];
static void tof_read()
{
printf("\n");
for (int i = 0; i < TOF_NUM; i++) {
vl53l0x_use(i);
tof_dist[i] = readRangeContinuousMillimeters(i);
printf(" %4d", tof_dist[i]);
}
}

View File

@ -9,6 +9,8 @@
#include "gimbal.h"
#include "airkey.h"
#include "config.h"
#include "light.h"
#include "hid.h"
struct __attribute__((packed)) {
@ -26,48 +28,72 @@ struct __attribute__((packed)) {
uint8_t keymap[15];
} hid_nkro;
const static struct {
uint8_t group;
uint8_t bit;
} button_to_io4_map[] = {
{ 0, 0 }, { 0, 5 }, { 0, 4 }, // Left ABC
{ 0, 1 }, { 1, 0 }, { 0, 15 }, // Right ABC
{ 1, 14 }, { 0, 13 }, // AUX 12
}, wad_left = { 1, 15 }, wad_right = { 0, 14 };
static void gen_hid_analogs()
{
hid_joy.adcs[0] = gimbal_read() << 8;
}
static void gen_hid_buttons()
{
const static struct {
uint8_t group;
uint8_t bit;
} button_to_io4_map[] = {
{ 0, 0 }, { 0, 5 }, { 0, 4 }, // Left ABC
{ 0, 1 }, { 1, 0 }, { 0, 15 }, // Right ABC
{ 1, 14 }, { 0, 13 }, // AUX 12
}, wad_left = { 1, 15 }, wad_right = { 0, 14 };
uint16_t buttons = button_read();
hid_joy.buttons[0] = 0;
hid_joy.buttons[1] = 0;
for (int i = 0; i < button_num(); i++) {
uint8_t group = button_to_io4_map[i].group;
uint8_t bit = button_to_io4_map[i].bit;
if (buttons & (1 << i)) {
if (!airkey_get(3)) {
hid_joy.buttons[group] |= (1 << bit);
}
}
}
if (!airkey_get(0)) {
hid_joy.buttons[wad_left.group] |= (1 << wad_left.bit);
}
if (!airkey_get(1)) {
hid_joy.buttons[wad_right.group] |= (1 << wad_right.bit);
}
}
static void gen_hid_coins()
{
static uint8_t last_gimbal = 0;
uint8_t gimbal = gimbal_read();
static int dec_count = 0;
if (airkey_get(3)) {
if (gimbal < last_gimbal) {
dec_count++;
} else if (gimbal > last_gimbal) {
dec_count = 0;
}
if (dec_count > 100) {
dec_count = 0;
hid_joy.chutes[0] += 0x100;
}
}
last_gimbal = gimbal;
}
static void report_usb_hid()
{
if (tud_hid_ready()) {
if (geki_cfg->hid.joy || geki_runtime.key_stuck) {
hid_joy.adcs[0] = (gimbal_read() - 128) << 8;
static uint16_t last_buttons = 0;
uint16_t buttons = button_read();
hid_joy.buttons[0] = 0;
hid_joy.buttons[1] = 0;
for (int i = 0; i < button_num(); i++) {
uint8_t group = button_to_io4_map[i].group;
uint8_t bit = button_to_io4_map[i].bit;
if (buttons & (1 << i)) {
hid_joy.buttons[group] |= (1 << bit);
}
}
if (airkey_get(0)) {
hid_joy.buttons[wad_left.group] |= (1 << wad_left.bit);
}
if (airkey_get(1)) {
hid_joy.buttons[wad_right.group] |= (1 << wad_right.bit);
}
if ((last_buttons ^ buttons) & (1 << 11)) {
if (buttons & (1 << 11)) {
// just pressed coin button
hid_joy.chutes[0] += 0x100;
}
}
gen_hid_analogs();
gen_hid_buttons();
gen_hid_coins();
tud_hid_n_report(0, REPORT_ID_JOYSTICK, &hid_joy, sizeof(hid_joy));
last_buttons = buttons;
}
if (geki_cfg->hid.nkro && !geki_runtime.key_stuck) {
tud_hid_n_report(1, 0, &hid_nkro, sizeof(hid_nkro));
@ -108,6 +134,24 @@ typedef struct __attribute__((packed)) {
uint8_t payload[62];
} hid_output_t;
static void update_led(const uint8_t data[4])
{
const uint8_t led_bit[18] = { 30, 31, 28, 26, 27, 29, 23, 25, 24, 20, 22, 21, 17, 19, 18, 14, 16, 15 };
uint32_t leds = (data[0] << 24) | (data[1] << 16) |
(data[2] << 8) | data[3];
uint8_t rgbs[18];
for (uint8_t i = 0; i < 18; i++) {
rgbs[i] = ((leds >> led_bit[i]) & 1) ? 0xff : 0x00;
}
for (uint8_t i = 0; i < 6; i++) {
uint32_t color = rgb32(rgbs[i * 3 + 1], rgbs[i * 3 + 2], rgbs[i * 3], false);
light_set_main(i, color);
}
}
void hid_proc(const uint8_t *data, uint8_t len)
{
hid_output_t *output = (hid_output_t *)data;
@ -116,14 +160,17 @@ void hid_proc(const uint8_t *data, uint8_t len)
case 0x01: // Set Timeout
case 0x02: // Set Sampling Count
hid_joy.system_status = 0x30;
printf("USB set timeout/sampling\n");
break;
case 0x03: // Clear Board Status
hid_joy.chutes[0] = 0;
hid_joy.chutes[1] = 0;
hid_joy.system_status = 0x00;
printf("USB clear status\n");
break;
case 0x04: // Set General Output
// LED
update_led(output->payload);
break;
case 0x41: // I don't know what this is
break;
@ -131,5 +178,5 @@ void hid_proc(const uint8_t *data, uint8_t len)
printf("USB unknown cmd: %d\n", output->cmd);
break;
}
}
}
}

View File

@ -23,13 +23,6 @@
static uint32_t buf_rgb[37]; // left 3 + right 3 + button 4 * 7 + indicator 5
static bool bind[37] = { 0 };
#define _MAP_LED(x) _MAKE_MAPPER(x)
#define _MAKE_MAPPER(x) MAP_LED_##x
#define MAP_LED_RGB { c1 = r; c2 = g; c3 = b; }
#define MAP_LED_GRB { c1 = g; c2 = r; c3 = b; }
#define REMAP_BUTTON_RGB _MAP_LED(BUTTON_RGB_ORDER)
#define REMAP_TT_RGB _MAP_LED(TT_RGB_ORDER)
static inline uint32_t _rgb32(uint32_t c1, uint32_t c2, uint32_t c3, bool gamma_fix)
{
@ -44,7 +37,7 @@ static inline uint32_t _rgb32(uint32_t c1, uint32_t c2, uint32_t c3, bool gamma_
uint32_t rgb32(uint32_t r, uint32_t g, uint32_t b, bool gamma_fix)
{
#if BUTTON_RGB_ORDER == GRB
#if RGB_ORDER == GRB
return _rgb32(g, r, b, gamma_fix);
#else
return _rgb32(r, g, b, gamma_fix);
@ -119,6 +112,7 @@ void light_init()
static void light_effect()
{
return;
static uint32_t loop = 0;
loop++;
@ -153,6 +147,30 @@ void light_set(uint8_t index, uint32_t color)
bind[index] = true;
}
void light_set_main(uint8_t index, uint32_t color)
{
if (index < 3) {
light_set(index * 4 + 4, color);
light_set(index * 4 + 5, color);
light_set(index * 4 + 6, color);
light_set(index * 4 + 7, color);
} else if (index < 6) {
light_set(index * 4 + 9, color);
light_set(index * 4 + 10, color);
light_set(index * 4 + 11, color);
light_set(index * 4 + 12, color);
}
}
void light_set_aux(uint8_t index, uint32_t color)
{
if (index == 0) {
light_set(0, color);
} else if (index == 1) {
light_set(36, color);
}
}
void light_unset(uint8_t index)
{
if (index >= count_of(buf_rgb)) {

View File

@ -19,6 +19,9 @@ uint32_t rgb32(uint32_t r, uint32_t g, uint32_t b, bool gamma_fix);
uint32_t rgb32_from_hsv(uint8_t h, uint8_t s, uint8_t v);
uint32_t load_color(const rgb_hsv_t *color);
void light_set_main(uint8_t index, uint32_t color);
void light_set_aux(uint8_t index, uint32_t color);
void light_set(uint8_t index, uint32_t color);
void light_unset(uint8_t index);

View File

@ -41,63 +41,67 @@
static void run_lights()
{
int gimbal = gimbal_read();
gimbal = gimbal * 5 / 256;
int gimbal = 255 - gimbal_read();
uint16_t button = button_read();
gimbal = gimbal * 5 / 256;
for (int i = 0; i < 5; i++) {
light_set(16 + i, (i == gimbal) ? 0x00ff00 : 0);
}
light_set_aux(0, button & 0x40 ? rgb32(0x80, 0, 0, false) : 0);
light_set_aux(1, button & 0x80 ? 0x808080 : 0);
if (airkey_get(3)) {
uint32_t phase = (time_us_32() >> 16) % 3;
light_set(1, phase == 0 ? 0x808080 : 0);
light_set(2, phase == 1 ? 0x808080 : 0);
light_set(3, phase == 2 ? 0x808080 : 0);
light_set(33, phase == 0 ? 0x808080 : 0);
light_set(34, phase == 1 ? 0x808080 : 0);
light_set(35, phase == 2 ? 0x808080 : 0);
} else {
if (airkey_get(0)) {
light_set(1, 0x804000);
light_set(2, 0x804000);
light_set(3, 0x804000);
} else {
light_set(1, 0);
light_set(2, 0);
light_set(3, 0);
}
if (airkey_get(1)) {
light_set(33, 0x004080);
light_set(34, 0x004080);
light_set(35, 0x004080);
} else {
light_set(33, 0);
light_set(34, 0);
light_set(35, 0);
}
}
return;
uint32_t colors[6] = {0x400000, 0x004000, 0x000040,
0x400000, 0x004000, 0x000040 };
uint16_t button = button_read();
for (int i = 0; i < 6; i++) {
uint32_t color = colors[i];
if (button & (1 << i)) {
color = 0x808080;
}
int index = 4 + i * 4 + (i > 2 ? 5 : 0);
light_set(index, color);
light_set(index + 1, color);
light_set(index + 2, color);
light_set(index + 3, color);
}
if (button & 0x40) {
light_set(0, 0x808080);
} else {
light_set(0, 0);
}
if (button & 0x80) {
light_set(36, 0x808080);
} else {
light_set(36, 0);
}
if (airkey_get(0)) {
light_set(1, 0x804000);
light_set(2, 0x804000);
light_set(3, 0x804000);
} else {
light_set(1, 0);
light_set(2, 0);
light_set(3, 0);
}
if (airkey_get(1)) {
light_set(33, 0x004080);
light_set(34, 0x004080);
light_set(35, 0x004080);
} else {
light_set(33, 0);
light_set(34, 0);
light_set(35, 0);
light_set_main(i, color);
}
}
static void run_sound()
{
if (airkey_get(3)) {
sound_set(0, false);
sound_set(1, false);
return;
}
sound_set(0, airkey_get(0));
sound_set(1, airkey_get(1));
}
@ -159,22 +163,21 @@ static void core0_loop()
/* if certain key pressed when booting, enter update mode */
static void update_check()
{
const uint8_t pins[] = BUTTON_DEF; // keypad 00 and *
bool all_pressed = true;
for (int i = 0; i < 2; i++) {
uint8_t gpio = pins[sizeof(pins) - 2 + i];
const uint8_t pins[] = BUTTON_DEF;
int pressed = 0;
for (int i = 0; i < count_of(pins); i++) {
uint8_t gpio = pins[i];
gpio_init(gpio);
gpio_set_function(gpio, GPIO_FUNC_SIO);
gpio_set_dir(gpio, GPIO_IN);
gpio_pull_up(gpio);
sleep_ms(1);
if (gpio_get(gpio)) {
all_pressed = false;
break;
if (!gpio_get(gpio)) {
pressed++;
}
}
if (all_pressed) {
if (pressed >= 4) {
sleep_ms(100);
reset_usb_boot(0, 2);
return;