Permalink
Browse files

make keyboard work, also include keycode display in 2nd boot

  • Loading branch information...
makapuf committed Sep 13, 2014
1 parent c96efc4 commit 3a983ffb45378b33b057f5f1939f70c48defc824
Showing with 146 additions and 40 deletions.
  1. +31 −3 2nd_boot/2nd_boot.c
  2. +1 −3 2nd_boot/Makefile
  3. +22 −10 lib/bitbox.h
  4. +2 −1 lib/bitbox.mk
  5. +62 −20 lib/evt_queue.c
  6. +28 −3 lib/usbh_hid_keybd.c
View
@@ -202,9 +202,6 @@ int read_icon(const char *filename)
char *HEX_Digits;
void game_init() {
- // CB->VTOR=SRAM1_BASE;
- // HACK, FIXME with a proper init of the interrupt table
-
flash_init();
window(2,2,78,4);
@@ -233,11 +230,38 @@ void game_init() {
}
+void display_first_event(void)
+{
+ // display first event, put it back
+ struct event e;
+ e=event_get();
+ switch(e.type)
+ {
+ case evt_keyboard_press :
+ print_at(50,10,"KB pressed ");
+ vram_char[10][61]="0123456789ABCDEF"[e.kbd.key>>4];
+ vram_char[10][62]="0123456789ABCDEF"[e.kbd.key&0xf];
+ break;
+
+ case evt_keyboard_release :
+ print_at(50,10,"KB released ");
+ vram_char[10][63]="0123456789ABCDEF"[e.kbd.key>>4];
+ vram_char[10][64]="0123456789ABCDEF"[e.kbd.key&0xf];
+ break;
+ }
+ if (e.type) event_push(e); // put it back
+}
+
int selected;
int x =5,y=10 , dir_x=1, dir_y=1;
char old_val=' ';
void game_frame()
{
+ display_first_event();
+
+ // interpret keyboard as gamepad & discard all other events
+ kbd_emulate_gamepad();
+
// handle input
if (GAMEPAD_PRESSED(0,down) && vga_frame%4==0)
selected +=1;
@@ -249,6 +273,8 @@ void game_frame()
// XXX try to read game icon if selected is different
+
+ // Start flashing ?
if (flash_done() && (GAMEPAD_PRESSED(0,start) || GAMEPAD_PRESSED(0,A)))
{
print_at(MSG_X,MSG_Y,"Goldorak GO ! Flashing ");
@@ -290,6 +316,8 @@ void game_frame()
vram_char[y][x] = '\x02';
}
+
+
flash_frame(); // at the end to let it finish
}
View
@@ -5,9 +5,7 @@ GAME_C_FILES = $(NAME).c font.c bg.c flashit.c
include lib/bitbox.mk
#comment to build to flash (for testing with out bootloader 1)
-TO_SRAM=1
-
-ifdef TO_SRAM
+ifdef FALSE
# NOTE that this program is loaded to SRAM1 (see bitbox memories), thus data is mapped accordingly & load stlink also !
LINKER_SCRIPT=Linker_2Bootloader.ld
View
@@ -59,13 +59,6 @@ enum device_enum {
device_gamepad
};
-enum buttons_enum {
- but_A=1,but_B,but_X,but_Y,but_L,but_R,but_select,but_start,
- // Those values are alternate names for mouse.
- but_left=but_A,but_right=but_B,but_middle=but_X
-};
-
-
enum evt_type { // type (a,b,c data)
no_event=0x00, // (error / empty queue)
evt_device_change, // device : port, device type, subtype. Set device type to 0 to mean unconnect
@@ -116,12 +109,16 @@ enum gamepad_buttons_enum {
gamepad_up = 1<<8,
gamepad_down =1<<9,
gamepad_left = 1<<10,
- gamepad_right=1<<12,
+ gamepad_right=1<<11,
+
+ mousebut_left=gamepad_A,
+ mousebut_right=gamepad_B,
+ mousebut_middle=gamepad_X
};
#define GAMEPAD_PRESSED(id , key) (gamepad_buttons[id] & (gamepad_##key))
-struct event {
+struct event { //should be coded in 32bits
uint8_t type;
union {
struct {
@@ -147,7 +144,7 @@ struct event {
uint8_t data[3];
};
-} ;
+} __attribute__ ((__packed__));
// -- state. defined in usb_devices.c
extern volatile enum device_enum device_type[2]; // currently plugged device
@@ -171,6 +168,21 @@ struct event event_get();
char kbd_map(struct event kbd_e);
+/* This emulates the gamepad with a keyboard.
+ * fetches all keyboard events,
+ * discarding all others (not optimal)
+ * mapping:
+
+ Space : Select, 2C
+ Enter : Start, 28
+ UDLR arrows : D-pad 52, 51, 50, 4F
+ D : A button, 07
+ F : B button, 09
+ E : X button, 08
+ R : Y button, 15
+ Left/Right CTRL (L/R shoulders)
+ */
+void kbd_emulate_gamepad (void);
// --- misc
void die(int where, int cause); // blink leds
View
@@ -20,7 +20,8 @@ CC = arm-none-eabi-gcc
LD = arm-none-eabi-gcc
OBJCOPY = arm-none-eabi-objcopy
-DEFINES = -DARM_MATH_CM4 -DOVERCLOCK -DAUDIO -DPROFILE
+DEFINES = -DARM_MATH_CM4 -DOVERCLOCK -DPROFILE
+#-DAUDIO
# USB defines
ifdef NO_USB
View
@@ -69,26 +69,6 @@ void event_clear()
evt_in=evt_out=&evt_queue[0];
}
-#ifdef TEST
-static void event_test()
-{
- // test vectors
- const int add[] = {0,2,3,10,6 ,4,0,16,0 ,16,16,16,16,16,16,0};
- const int get[] = {2,0,6,0 ,16,0,4,0 ,16,0,0,0,0,0,0,16};
-
- event_clear();
- for (int i=0;i<sizeof(add)/sizeof(int);i++)
- {
- printf("round %d:+%d-%d:",i,add[i],get[i]);
- for (int j=0;j<add[i];j++) event_push((event){.raw=i});
- for (int j=0;j<get[i];j++) printf("%2d ",event_get(i));
- //printf("in: %p out:%p start:%p end:%p",evt_in, evt_out, QUEUE_START,QUEUE_END);
- if (event_full()) printf(" - now full ");
- if (event_empty()) printf("- now empty ");
- printf("\n");
- }
-}
-#endif
// keymap : list of ranges of elements, non shifted and shifted
// see http://www.usb.org/developers/devclass_docs/Hut1_11.pdf, page 7
@@ -111,8 +91,69 @@ char kbd_map(struct event e)
return 0; // non printable char
}
+
+/* This emulates the gamepad with a keyboard.
+ * fetches all keyboard events,
+ * discarding all others (not optimal)
+ * mapping:
+
+ Space : Select, 2C
+ Enter : Start, 28
+ UDLR arrows : D-pad 52, 51, 50, 4F
+ D : A button, 07
+ F : B button, 09
+ E : X button, 08
+ R : Y button, 15
+ Left/Right CTRL (L/R shoulders)
+ */
+
+void kbd_emulate_gamepad (void)
+{
+ // kbd codes in order of gamepad buttons
+ static const uint8_t kbd_gamepad[] = {0x07, 0x09, 0x08, 0x15, 0xE0, 0xE4, 0x2c, 0x28, 0x52, 0x51, 0x50, 0x4f };
+ //
+ struct event e;
+ do {
+ e=event_get();
+ for (int i=0;i<12;i++)
+ {
+ if (kbd_gamepad[i]==e.kbd.key)
+ switch (e.type)
+ {
+ case evt_keyboard_press :
+ gamepad_buttons[0] |= (1<<i);
+ break;
+
+ case evt_keyboard_release :
+ gamepad_buttons[0] &= ~(1<<i);
+ break;
+ }
+ }
+ } while (e.type);
+}
+
#ifdef TEST
#include <stdio.h>
+
+static void event_test()
+{
+ // test vectors
+ const int add[] = {0,2,3,10,6 ,4,0,16,0 ,16,16,16,16,16,16,0};
+ const int get[] = {2,0,6,0 ,16,0,4,0 ,16,0,0,0,0,0,0,16};
+
+ event_clear();
+ for (int i=0;i<sizeof(add)/sizeof(int);i++)
+ {
+ printf("round %d:+%d-%d:",i,add[i],get[i]);
+ for (int j=0;j<add[i];j++) event_push((event){.raw=i});
+ for (int j=0;j<get[i];j++) printf("%2d ",event_get(i));
+ //printf("in: %p out:%p start:%p end:%p",evt_in, evt_out, QUEUE_START,QUEUE_END);
+ if (event_full()) printf(" - now full ");
+ if (event_empty()) printf("- now empty ");
+ printf("\n");
+ }
+}
+
int main()
{
printf("%d event, %d queue \n",sizeof(event),sizeof(evt_queue) );
@@ -123,3 +164,4 @@ int main()
#endif
+
View
@@ -47,12 +47,17 @@ static void KEYBRD_Init (uint8_t coreID, uint16_t vid, uint16_t pid)
static void KEYBRD_Decode(uint8_t coreID, uint8_t *pbuf)
{
static uint8_t nb_last;
- static uint8_t keys_last[KBR_MAX_NBR_PRESSED];
+ static uint8_t keys_last[KBR_MAX_NBR_PRESSED]; // list of 0-nb_last codes of last pressed keys
+ static uint8_t mod_last;
+
int j;
struct event e;
+ // keyboard boot protocol : modifiers bits(Ctrl, Shift, Alt, Win L then R), reserved, char[6]
+
// calculates New-Last & send keypresses
for (int i = 2; i < 2 + KBR_MAX_NBR_PRESSED; i++) {
+ if (pbuf[i]==0) continue; // break?
// tests for errors (code 1,2 or 3)
if ((unsigned)(pbuf[i]-1) <= 2)
return;
@@ -70,8 +75,8 @@ static void KEYBRD_Decode(uint8_t coreID, uint8_t *pbuf)
e.kbd.key=pbuf[i];
event_push(e);
}
-
}
+
// now calculates old-new and send key released events.
for (int i=0;i<nb_last;i++)
@@ -84,16 +89,36 @@ static void KEYBRD_Decode(uint8_t coreID, uint8_t *pbuf)
{
e.type=evt_keyboard_release;
e.kbd.mod=pbuf[0];
- e.kbd.key=pbuf[i];
+ e.kbd.key=keys_last[i];
event_push(e);
}
}
+ // special case : modifier keys as keypresses
+ if (pbuf[0] != mod_last)
+ for (int i=0;i<8;i++) {
+ if ((pbuf[0] & ~mod_last) & (1<<i)) // new ones
+ {
+ e.type=evt_keyboard_press;
+ e.kbd.key=0xE0 + i; // codes are in the same order as bits
+ event_push(e);
+ }
+
+ if ((mod_last & ~pbuf[0]) & (1<<i)) // released ones
+ {
+ e.type=evt_keyboard_release;
+ e.kbd.key=0xE0 + i; // codes are in the same order as bits
+ event_push(e);
+ }
+ }
+
+
// fills old keys with current
nb_last=0;
for (int i=0;i<KBR_MAX_NBR_PRESSED;i++) {
if (pbuf[2+i])
keys_last[nb_last++]=pbuf[2+i];
}
+ mod_last=pbuf[0];
}

0 comments on commit 3a983ff

Please sign in to comment.