Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

matrix.c 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. NeXT non-ADB Keyboard USB Converter
  3. Copyright 2013, Benjamin Gould ([email protected])
  4. Based on:
  5. TMK firmware code Copyright 2011,2012 Jun WAKO <[email protected]>
  6. Arduino code by "Ladyada" Limor Fried (http://ladyada.net/, http://adafruit.com/), released under BSD license
  7. Timing reference thanks to http://m0115.web.fc2.com/ (dead link), http://cfile7.uf.tistory.com/image/14448E464F410BF22380BB
  8. Pinouts thanks to http://www.68k.org/~degs/nextkeyboard.html
  9. Keycodes from http://ftp.netbsd.org/pub/NetBSD/NetBSD-release-6/src/sys/arch/next68k/dev/
  10. This software is licensed with a Modified BSD License.
  11. All of this is supposed to be Free Software, Open Source, DFSG-free,
  12. GPL-compatible, and OK to use in both free and proprietary applications.
  13. Additions and corrections to this file are welcome.
  14. Redistribution and use in source and binary forms, with or without
  15. modification, are permitted provided that the following conditions are met:
  16. * Redistributions of source code must retain the above copyright
  17. notice, this list of conditions and the following disclaimer.
  18. * Redistributions in binary form must reproduce the above copyright
  19. notice, this list of conditions and the following disclaimer in
  20. the documentation and/or other materials provided with the
  21. distribution.
  22. * Neither the name of the copyright holders nor the names of
  23. contributors may be used to endorse or promote products derived
  24. from this software without specific prior written permission.
  25. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  29. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include <stdint.h>
  38. #include <stdbool.h>
  39. #include <avr/io.h>
  40. #include <util/delay.h>
  41. #include "print.h"
  42. #include "util.h"
  43. #include "serial.h"
  44. #include "matrix.h"
  45. #include "debug.h"
  46. #include "matrix.h"
  47. #include "next_kbd.h"
  48. static void matrix_make(uint8_t code);
  49. static void matrix_break(uint8_t code);
  50. static uint8_t matrix[MATRIX_ROWS];
  51. #define ROW(code) ((code>>3)&0xF)
  52. #define COL(code) (code&0x07)
  53. static bool is_modified = false;
  54. #ifndef NEXT_KBD_LED1_ON
  55. #define NEXT_KBD_LED1_ON
  56. #endif
  57. #ifndef NEXT_KBD_LED1_OFF
  58. #define NEXT_KBD_LED1_OFF
  59. #endif
  60. #define NEXT_KBD_PWR_READ (NEXT_KBD_PWR_PIN&(1<<NEXT_KBD_PWR_BIT))
  61. static bool power_state = false;
  62. /* intialize matrix for scanning. should be called once. */
  63. void matrix_init(void)
  64. {
  65. #ifdef DEBUG_ON_INIT
  66. debug_enable = true;
  67. #endif
  68. // I've found that the matrix likes a little while for things to
  69. // settle down before it gets started. Not sure why :)
  70. _delay_ms(250);
  71. dprintf("[ Intializing NeXT keyboard ]\n");
  72. NEXT_KBD_LED1_DDR |= (1<<NEXT_KBD_LED1_BIT); // LED pin to output
  73. NEXT_KBD_LED1_ON;
  74. NEXT_KBD_PWR_DDR &= ~(1<<NEXT_KBD_PWR_BIT); // Power Button pin to input
  75. NEXT_KBD_PWR_PIN |= (1<<NEXT_KBD_PWR_BIT); // KBD_PWR pull up
  76. power_state = NEXT_KBD_PWR_READ ? false : true;
  77. dprintf("Initial power button state: %b\n", power_state);
  78. next_kbd_init();
  79. // initialize matrix state: all keys off
  80. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  81. #ifdef NEXT_KBD_INIT_FLASH_LEDS
  82. dprintf("flashing LEDs:");
  83. // flash the LEDs after initialization
  84. bool leds_on = true;
  85. for (uint8_t i = 0; i <= 6; i++)
  86. {
  87. leds_on = leds_on ? false : true;
  88. dprintf(" %b", leds_on);
  89. next_kbd_set_leds(leds_on, leds_on);
  90. _delay_ms(250);
  91. }
  92. dprintf("\n");
  93. #endif
  94. dprintf("[ NeXT keyboard initialized ]\n");
  95. return;
  96. }
  97. #define NEXT_KBD_KEYCODE(response) (uint8_t)((response&0xFF)>>1)
  98. #define NEXT_KBD_PRESSED_KEYCODE(response) (uint8_t)(((response)&0xF00)==0x400)
  99. #define NEXT_KBD_PRESSED(response, mask) (uint8_t)(((response)&mask)>0)
  100. #define NEXT_KBD_PRESSED_CONTROL(response) NEXT_KBD_PRESSED(response,0x01000)
  101. #define NEXT_KBD_PRESSED_SHIFT_LEFT(response) NEXT_KBD_PRESSED(response,0x02000)
  102. #define NEXT_KBD_PRESSED_SHIFT_RGHT(response) NEXT_KBD_PRESSED(response,0x04000)
  103. #define NEXT_KBD_PRESSED_CMD_LEFT(response) NEXT_KBD_PRESSED(response,0x08000)
  104. #define NEXT_KBD_PRESSED_CMD_RGHT(response) NEXT_KBD_PRESSED(response,0x10000)
  105. #define NEXT_KBD_PRESSED_ALT_LEFT(response) NEXT_KBD_PRESSED(response,0x20000)
  106. #define NEXT_KBD_PRESSED_ALT_RGHT(response) NEXT_KBD_PRESSED(response,0x40000)
  107. #define NEXT_KBD_MAKE_OR_BREAK(key, code) \
  108. do { \
  109. if (NEXT_KBD_PRESSED_##key(resp) > 0) \
  110. matrix_make(code); \
  111. else \
  112. matrix_break(code); \
  113. } while (0);
  114. #define NEXT_KBD_PWR_KEYCODE 0x58
  115. /* scan all key states on matrix */
  116. uint8_t matrix_scan(void)
  117. {
  118. _delay_ms(5);
  119. //next_kbd_set_leds(false, false);
  120. NEXT_KBD_LED1_OFF;
  121. is_modified = false;
  122. if (!NEXT_KBD_PWR_READ) {
  123. matrix_make(NEXT_KBD_PWR_KEYCODE);
  124. power_state = 1;
  125. if (is_modified)
  126. {
  127. dprintf("Power state 1\n");
  128. }
  129. } else {
  130. matrix_break(NEXT_KBD_PWR_KEYCODE);
  131. power_state = 0;
  132. if (is_modified)
  133. {
  134. dprintf("Power state 0\n");
  135. }
  136. }
  137. uint32_t resp = (next_kbd_recv());
  138. if (!resp || resp == NEXT_KBD_KMBUS_IDLE)
  139. {
  140. return 0;
  141. }
  142. NEXT_KBD_LED1_ON;
  143. #ifdef NEXT_KBD_SHIFT_FLASH_LEDS
  144. next_kbd_set_leds(
  145. NEXT_KBD_PRESSED_SHIFT_LEFT(resp) ? true : false,
  146. NEXT_KBD_PRESSED_SHIFT_RGHT(resp) ? true : false
  147. );
  148. #endif
  149. dprintf("[ r=%04lX keycode=%02X pressed=%X CTRL=%X SHIFT_LEFT=%X SHIFT_RGHT=%X CMD_LEFT=%X CMD_RGHT=%X ALT_LEFT=%X ALT_RGHT=%X ]\n", \
  150. resp, \
  151. NEXT_KBD_KEYCODE(resp), \
  152. NEXT_KBD_PRESSED_KEYCODE(resp), \
  153. NEXT_KBD_PRESSED_CONTROL(resp), \
  154. NEXT_KBD_PRESSED_SHIFT_LEFT(resp), \
  155. NEXT_KBD_PRESSED_SHIFT_RGHT(resp), \
  156. NEXT_KBD_PRESSED_CMD_LEFT(resp), \
  157. NEXT_KBD_PRESSED_CMD_RGHT(resp), \
  158. NEXT_KBD_PRESSED_ALT_LEFT(resp), \
  159. NEXT_KBD_PRESSED_ALT_RGHT(resp) \
  160. );
  161. // Modifier keys don't return keycode; have to check the upper bits
  162. NEXT_KBD_MAKE_OR_BREAK(ALT_RGHT, 0x51);
  163. NEXT_KBD_MAKE_OR_BREAK(ALT_LEFT, 0x52);
  164. NEXT_KBD_MAKE_OR_BREAK(CMD_RGHT, 0x53);
  165. NEXT_KBD_MAKE_OR_BREAK(CMD_LEFT, 0x54);
  166. NEXT_KBD_MAKE_OR_BREAK(SHIFT_RGHT, 0x55);
  167. NEXT_KBD_MAKE_OR_BREAK(SHIFT_LEFT, 0x56);
  168. NEXT_KBD_MAKE_OR_BREAK(CONTROL, 0x57);
  169. NEXT_KBD_MAKE_OR_BREAK(KEYCODE, NEXT_KBD_KEYCODE(resp));
  170. return 1;
  171. }
  172. /* matrix state on row */
  173. inline
  174. uint8_t matrix_get_row(uint8_t row)
  175. {
  176. return matrix[row];
  177. }
  178. inline
  179. static void matrix_make(uint8_t code)
  180. {
  181. if (!matrix_is_on(ROW(code), COL(code))) {
  182. matrix[ROW(code)] |= 1<<COL(code);
  183. is_modified = true;
  184. }
  185. }
  186. inline
  187. static void matrix_break(uint8_t code)
  188. {
  189. if (matrix_is_on(ROW(code), COL(code))) {
  190. matrix[ROW(code)] &= ~(1<<COL(code));
  191. is_modified = true;
  192. }
  193. }