|
@@ -29,6 +29,7 @@
|
|
|
|
|
|
#include "bsp/board.h"
|
|
|
#include "hardware/gpio.h"
|
|
|
+#include "hardware/adc.h"
|
|
|
#include "tusb.h"
|
|
|
|
|
|
#include "usb_descriptors.h"
|
|
@@ -48,18 +49,25 @@ enum {
|
|
|
BLINK_SUSPENDED = 2500,
|
|
|
};
|
|
|
|
|
|
-#define J_UP 14
|
|
|
-#define J_DOWN 12
|
|
|
-#define J_RIGHT 15
|
|
|
-#define J_LEFT 13
|
|
|
-#define J_FIRE1 16
|
|
|
-#define J_FIRE2 17
|
|
|
-#define J_FIRE3 19
|
|
|
-#define J_FIRE4 21
|
|
|
-#define J_FIRE5 22
|
|
|
-#define J_FIRE6 23
|
|
|
-#define J_START 0
|
|
|
-#define J_SELECT 4
|
|
|
+#define J_UP 21
|
|
|
+#define J_DOWN 20
|
|
|
+#define J_LEFT 19
|
|
|
+#define J_RIGHT 18
|
|
|
+#define J_FIRE1 8 /* south */
|
|
|
+#define J_FIRE2 7 /* north */
|
|
|
+#define J_FIRE3 10 /* east */
|
|
|
+#define J_FIRE4 6 /* west */
|
|
|
+#define J_FIRE5 12 /* TL */
|
|
|
+#define J_FIRE6 13 /* TR */
|
|
|
+#define J_FIRE7 4 /* TL2 */
|
|
|
+#define J_FIRE8 9 /* TR2 */
|
|
|
+#define J_START 5
|
|
|
+#define J_SELECT 11
|
|
|
+
|
|
|
+#define POT 27 /* Sensitivity / mode change */
|
|
|
+#define POT_ADC 1 /* ADC1 */
|
|
|
+
|
|
|
+#define LED_DIGITAL 28
|
|
|
|
|
|
static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
|
|
|
|
|
@@ -131,63 +139,88 @@ static void send_hid_report(uint8_t report_id, uint32_t btn)
|
|
|
{
|
|
|
// use to avoid send multiple consecutive zero report for keyboard
|
|
|
static bool has_gamepad_key = false;
|
|
|
+ static int32_t prog_x = 0, prog_y = 0;
|
|
|
+ uint32_t pot_val = 0;
|
|
|
+ uint32_t gpio_mask = gpio_get_all();
|
|
|
+
|
|
|
|
|
|
hid_gamepad_report_t report =
|
|
|
{
|
|
|
- .x = 1, .y = 0, .z = 0, .rz = 0, .rx = 0, .ry = 0,
|
|
|
+ .x = 0, .y = 0, .z = 0, .rz = 0, .rx = 0, .ry = 0,
|
|
|
.hat = 0, .buttons = 0
|
|
|
};
|
|
|
|
|
|
- if (gpio_get(J_UP) == 0) {
|
|
|
- report.y = -127;
|
|
|
- } else if (gpio_get(J_DOWN) == 0) {
|
|
|
- report.y = 127;
|
|
|
+ if ((gpio_mask & (1<<J_UP)) == 0) {
|
|
|
+ report.y = -1;
|
|
|
+ } else if ((gpio_mask & (1<<J_DOWN)) == 0) {
|
|
|
+ report.y = 1;
|
|
|
+ }
|
|
|
+ if ((gpio_mask & (1<<J_RIGHT)) == 0) {
|
|
|
+ report.x = 1;
|
|
|
+ } else if ((gpio_mask & (1<<J_LEFT)) == 0) {
|
|
|
+ report.x = -1;
|
|
|
}
|
|
|
- if (gpio_get(J_RIGHT) == 0) {
|
|
|
- report.x = 127;
|
|
|
- } else if (gpio_get(J_LEFT) == 0) {
|
|
|
- report.x = -127;
|
|
|
+
|
|
|
+ adc_select_input(POT_ADC);
|
|
|
+ pot_val = adc_read();
|
|
|
+
|
|
|
+ if (pot_val < 4000) {
|
|
|
+ if (pot_val > 32) {
|
|
|
+ /* Tune intensity */
|
|
|
+ report.x *= (pot_val >> 5);
|
|
|
+ report.y *= (pot_val >> 5);
|
|
|
+ }
|
|
|
+ gpio_put(LED_DIGITAL, 0);
|
|
|
+ } else {
|
|
|
+ /* Digital mode */
|
|
|
+ report.x *= 127;
|
|
|
+ report.y *= 127;
|
|
|
+ gpio_put(LED_DIGITAL, 1);
|
|
|
}
|
|
|
|
|
|
report.buttons = 0;
|
|
|
- if (gpio_get(J_FIRE1) == 0) {
|
|
|
+ if ((gpio_mask & (1<<J_FIRE1)) == 0) {
|
|
|
report.buttons |= GAMEPAD_BUTTON_SOUTH;
|
|
|
}
|
|
|
- if (gpio_get(J_FIRE2) == 0) {
|
|
|
+ if ((gpio_mask & (1<<J_FIRE2)) == 0) {
|
|
|
report.buttons |= GAMEPAD_BUTTON_NORTH;
|
|
|
}
|
|
|
- if (gpio_get(J_FIRE3) == 0) {
|
|
|
+ if ((gpio_mask & (1<<J_FIRE3)) == 0) {
|
|
|
report.buttons |= GAMEPAD_BUTTON_EAST;
|
|
|
}
|
|
|
- if (gpio_get(J_FIRE4) == 0) {
|
|
|
+ if ((gpio_mask & (1<<J_FIRE4)) == 0) {
|
|
|
report.buttons |= GAMEPAD_BUTTON_WEST;
|
|
|
}
|
|
|
- if (gpio_get(J_FIRE5) == 0) {
|
|
|
- report.buttons |= GAMEPAD_BUTTON_Z;
|
|
|
- }
|
|
|
- if (gpio_get(J_FIRE6) == 0) {
|
|
|
+ if ((gpio_mask & (1<<J_FIRE5)) == 0) {
|
|
|
report.buttons |= GAMEPAD_BUTTON_TL;
|
|
|
}
|
|
|
- if (gpio_get(J_START) == 0) {
|
|
|
+ if ((gpio_mask & (1<<J_FIRE6)) == 0) {
|
|
|
+ report.buttons |= GAMEPAD_BUTTON_TR;
|
|
|
+ }
|
|
|
+ if ((gpio_mask & (1<<J_FIRE7)) == 0) {
|
|
|
+ report.buttons |= GAMEPAD_BUTTON_TL2;
|
|
|
+ }
|
|
|
+ if ((gpio_mask & (1<<J_FIRE8)) == 0) {
|
|
|
+ report.buttons |= GAMEPAD_BUTTON_TR2;
|
|
|
+ }
|
|
|
+ if ((gpio_mask & (1<<J_START)) == 0) {
|
|
|
report.buttons |= GAMEPAD_BUTTON_START;
|
|
|
}
|
|
|
- if (gpio_get(J_SELECT) == 0) {
|
|
|
+ if ((gpio_mask & (1<<J_SELECT)) == 0) {
|
|
|
report.buttons |= GAMEPAD_BUTTON_SELECT;
|
|
|
}
|
|
|
tud_hid_report(REPORT_ID_GAMEPAD, &report, sizeof(report));
|
|
|
}
|
|
|
break;
|
|
|
-
|
|
|
- default: break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-// Every 10ms, we will sent 1 report for each HID profile (keyboard, mouse etc ..)
|
|
|
+// Every 5ms, we will sent 1 report for each HID profile (keyboard, mouse etc ..)
|
|
|
// tud_hid_report_complete_cb() is used to send the next report after previous one is complete
|
|
|
void hid_task(void)
|
|
|
{
|
|
|
// Poll every 10ms
|
|
|
- const uint32_t interval_ms = 10;
|
|
|
+ const uint32_t interval_ms = 5;
|
|
|
static uint32_t start_ms = 0;
|
|
|
static int gpio_initialized = 0;
|
|
|
|
|
@@ -202,8 +235,14 @@ void hid_task(void)
|
|
|
gpio_init(J_FIRE2);
|
|
|
gpio_init(J_FIRE3);
|
|
|
gpio_init(J_FIRE4);
|
|
|
+ gpio_init(J_FIRE5);
|
|
|
+ gpio_init(J_FIRE6);
|
|
|
+ gpio_init(J_FIRE7);
|
|
|
+ gpio_init(J_FIRE8);
|
|
|
gpio_init(J_START);
|
|
|
gpio_init(J_SELECT);
|
|
|
+ gpio_init(POT);
|
|
|
+ gpio_init(LED_DIGITAL);
|
|
|
|
|
|
gpio_set_dir(J_UP, GPIO_IN);
|
|
|
gpio_set_dir(J_DOWN, GPIO_IN);
|
|
@@ -213,9 +252,18 @@ void hid_task(void)
|
|
|
gpio_set_dir(J_FIRE2, GPIO_IN);
|
|
|
gpio_set_dir(J_FIRE3, GPIO_IN);
|
|
|
gpio_set_dir(J_FIRE4, GPIO_IN);
|
|
|
+ gpio_set_dir(J_FIRE5, GPIO_IN);
|
|
|
+ gpio_set_dir(J_FIRE6, GPIO_IN);
|
|
|
+ gpio_set_dir(J_FIRE7, GPIO_IN);
|
|
|
+ gpio_set_dir(J_FIRE8, GPIO_IN);
|
|
|
gpio_set_dir(J_START, GPIO_IN);
|
|
|
gpio_set_dir(J_SELECT, GPIO_IN);
|
|
|
|
|
|
+ gpio_set_dir(LED_DIGITAL, GPIO_OUT);
|
|
|
+
|
|
|
+ adc_init();
|
|
|
+ adc_gpio_init(POT);
|
|
|
+
|
|
|
gpio_pull_up(J_UP);
|
|
|
gpio_pull_up(J_DOWN);
|
|
|
gpio_pull_up(J_LEFT);
|
|
@@ -224,14 +272,17 @@ void hid_task(void)
|
|
|
gpio_pull_up(J_FIRE2);
|
|
|
gpio_pull_up(J_FIRE3);
|
|
|
gpio_pull_up(J_FIRE4);
|
|
|
+ gpio_pull_up(J_FIRE5);
|
|
|
+ gpio_pull_up(J_FIRE6);
|
|
|
+ gpio_pull_up(J_FIRE7);
|
|
|
+ gpio_pull_up(J_FIRE8);
|
|
|
gpio_pull_up(J_START);
|
|
|
gpio_pull_up(J_SELECT);
|
|
|
gpio_initialized++;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
if ( board_millis() - start_ms < interval_ms) return; // not enough time
|
|
|
- start_ms += interval_ms;
|
|
|
+ start_ms += interval_ms;
|
|
|
|
|
|
// Remote wakeup
|
|
|
if ( tud_suspended())
|