coleco.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. #include <furi.h>
  2. #include <furi_hal_gpio.h>
  3. #include <furi_hal_power.h>
  4. #include <gui/gui.h>
  5. #include "coleco_icons.h"
  6. #define CODE_0 0x0A
  7. #define CODE_1 0x0D
  8. #define CODE_2 0x07
  9. #define CODE_3 0x0C
  10. #define CODE_4 0x02
  11. #define CODE_5 0x03
  12. #define CODE_6 0x0E
  13. #define CODE_7 0x05
  14. #define CODE_8 0x01
  15. #define CODE_9 0x0B
  16. #define CODE_H 0x06
  17. #define CODE_S 0x09
  18. #define CODE_N 0x0F
  19. const GpioPin* const pin_up = &gpio_ext_pa6;
  20. const GpioPin* const pin_down = &gpio_ext_pc0;
  21. const GpioPin* const pin_right = &gpio_ext_pb2;
  22. const GpioPin* const pin_left = &gpio_ext_pc3;
  23. const GpioPin* const pin_code0 = &gpio_ext_pa7;
  24. const GpioPin* const pin_code1 = &gpio_ext_pa4;
  25. const GpioPin* const pin_code2 = &gpio_ibutton;
  26. const GpioPin* const pin_code3 = &gpio_ext_pc1;
  27. const GpioPin* const pin_fire = &gpio_ext_pb3;
  28. const GpioPin* const pin_alt = &gpio_usart_tx;
  29. typedef enum {
  30. EventTypeTick,
  31. EventTypeKey,
  32. } EventType;
  33. typedef struct {
  34. EventType type;
  35. InputEvent input;
  36. } PluginEvent;
  37. typedef struct {
  38. bool dpad;
  39. int row;
  40. int column;
  41. FuriMutex* mutex;
  42. } Coleco;
  43. static void render_callback(Canvas* const canvas, void* context) {
  44. Coleco* coleco = (Coleco*)context;
  45. furi_mutex_acquire(coleco->mutex, FuriWaitForever);
  46. if(coleco->dpad) {
  47. canvas_draw_icon(canvas, 4, 16, &I_ColecoJoystick_sel_33x33);
  48. canvas_draw_icon(canvas, 27, 52, &I_ColecoFire_sel_18x9);
  49. } else {
  50. const bool hvr = coleco->row == 0 && coleco->column < 2;
  51. canvas_draw_icon(
  52. canvas, 4, 16, hvr ? &I_ColecoJoystick_hvr_33x33 : &I_ColecoJoystick_33x33);
  53. canvas_draw_icon(canvas, 27, 52, hvr ? &I_ColecoFire_hvr_18x9 : &I_ColecoFire_18x9);
  54. }
  55. canvas_draw_icon(
  56. canvas,
  57. 27,
  58. 4,
  59. (coleco->row == 0 && coleco->column == 2) ? &I_ColecoAlt_hvr_18x9 : &I_ColecoAlt_18x9);
  60. canvas_draw_icon(
  61. canvas,
  62. 49,
  63. 44,
  64. (coleco->row == 1 && coleco->column == 0) ? &I_Coleco1_hvr_17x17 : &I_Coleco1_17x17);
  65. canvas_draw_icon(
  66. canvas,
  67. 49,
  68. 24,
  69. (coleco->row == 1 && coleco->column == 1) ? &I_Coleco2_hvr_17x17 : &I_Coleco2_17x17);
  70. canvas_draw_icon(
  71. canvas,
  72. 49,
  73. 4,
  74. (coleco->row == 1 && coleco->column == 2) ? &I_Coleco3_hvr_17x17 : &I_Coleco3_17x17);
  75. canvas_draw_icon(
  76. canvas,
  77. 69,
  78. 44,
  79. (coleco->row == 2 && coleco->column == 0) ? &I_Coleco4_hvr_17x17 : &I_Coleco4_17x17);
  80. canvas_draw_icon(
  81. canvas,
  82. 69,
  83. 24,
  84. (coleco->row == 2 && coleco->column == 1) ? &I_Coleco5_hvr_17x17 : &I_Coleco5_17x17);
  85. canvas_draw_icon(
  86. canvas,
  87. 69,
  88. 4,
  89. (coleco->row == 2 && coleco->column == 2) ? &I_Coleco6_hvr_17x17 : &I_Coleco6_17x17);
  90. canvas_draw_icon(
  91. canvas,
  92. 89,
  93. 44,
  94. (coleco->row == 3 && coleco->column == 0) ? &I_Coleco7_hvr_17x17 : &I_Coleco7_17x17);
  95. canvas_draw_icon(
  96. canvas,
  97. 89,
  98. 24,
  99. (coleco->row == 3 && coleco->column == 1) ? &I_Coleco8_hvr_17x17 : &I_Coleco8_17x17);
  100. canvas_draw_icon(
  101. canvas,
  102. 89,
  103. 4,
  104. (coleco->row == 3 && coleco->column == 2) ? &I_Coleco9_hvr_17x17 : &I_Coleco9_17x17);
  105. canvas_draw_icon(
  106. canvas,
  107. 109,
  108. 44,
  109. (coleco->row == 4 && coleco->column == 0) ? &I_ColecoStar_hvr_17x17 : &I_ColecoStar_17x17);
  110. canvas_draw_icon(
  111. canvas,
  112. 109,
  113. 24,
  114. (coleco->row == 4 && coleco->column == 1) ? &I_Coleco0_hvr_17x17 : &I_Coleco0_17x17);
  115. canvas_draw_icon(
  116. canvas,
  117. 109,
  118. 4,
  119. (coleco->row == 4 && coleco->column == 2) ? &I_ColecoPound_hvr_17x17 :
  120. &I_ColecoPound_17x17);
  121. furi_mutex_release(coleco->mutex);
  122. }
  123. static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
  124. furi_assert(event_queue);
  125. PluginEvent event = {.type = EventTypeKey, .input = *input_event};
  126. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  127. }
  128. static void coleco_write_code(uint8_t code) {
  129. furi_hal_gpio_write(pin_code0, (code & 1));
  130. furi_hal_gpio_write(pin_code1, (code & 2));
  131. furi_hal_gpio_write(pin_code2, (code & 4));
  132. furi_hal_gpio_write(pin_code3, (code & 8));
  133. }
  134. static void coleco_gpio_init() {
  135. // configure output pins
  136. furi_hal_gpio_init(pin_up, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
  137. furi_hal_gpio_init(pin_down, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
  138. furi_hal_gpio_init(pin_right, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
  139. furi_hal_gpio_init(pin_left, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
  140. furi_hal_gpio_init(pin_code0, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
  141. furi_hal_gpio_init(pin_code1, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
  142. furi_hal_gpio_init(pin_code2, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
  143. furi_hal_gpio_init(pin_code3, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
  144. furi_hal_gpio_init(pin_fire, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
  145. furi_hal_gpio_init(pin_alt, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
  146. furi_hal_gpio_write(pin_up, true);
  147. furi_hal_gpio_write(pin_down, true);
  148. furi_hal_gpio_write(pin_right, true);
  149. furi_hal_gpio_write(pin_left, true);
  150. furi_hal_gpio_write(pin_fire, true);
  151. furi_hal_gpio_write(pin_alt, true);
  152. coleco_write_code(CODE_N);
  153. }
  154. static Coleco* coleco_alloc() {
  155. Coleco* coleco = malloc(sizeof(Coleco));
  156. coleco->dpad = false;
  157. coleco->row = 0;
  158. coleco->column = 1;
  159. coleco->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  160. if(!coleco->mutex) {
  161. FURI_LOG_E("Coleco", "cannot create mutex\r\n");
  162. free(coleco);
  163. return NULL;
  164. }
  165. return coleco;
  166. }
  167. static void coleco_free(Coleco* coleco) {
  168. furi_assert(coleco);
  169. furi_mutex_free(coleco->mutex);
  170. free(coleco);
  171. }
  172. int32_t coleco_app(void* p) {
  173. UNUSED(p);
  174. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
  175. Coleco* coleco = coleco_alloc();
  176. if(coleco == NULL) {
  177. return 255;
  178. }
  179. // set system callbacks
  180. ViewPort* view_port = view_port_alloc();
  181. view_port_draw_callback_set(view_port, render_callback, coleco);
  182. view_port_input_callback_set(view_port, input_callback, event_queue);
  183. // open GUI and register view_port
  184. Gui* gui = furi_record_open("gui");
  185. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  186. coleco_gpio_init();
  187. furi_hal_power_enable_otg();
  188. PluginEvent event;
  189. for(bool processing = true; processing;) {
  190. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  191. furi_mutex_acquire(coleco->mutex, FuriWaitForever);
  192. if(event_status == FuriStatusOk) {
  193. // press events
  194. if(event.type == EventTypeKey) {
  195. switch(event.input.key) {
  196. case InputKeyUp:
  197. if(coleco->dpad) {
  198. if(event.input.type == InputTypePress) {
  199. furi_hal_gpio_write(pin_up, false);
  200. } else if(event.input.type == InputTypeRelease) {
  201. furi_hal_gpio_write(pin_up, true);
  202. }
  203. } else {
  204. if(event.input.type == InputTypePress && coleco->column < 2) {
  205. coleco->column++;
  206. coleco_write_code(CODE_N);
  207. }
  208. }
  209. break;
  210. case InputKeyDown:
  211. if(coleco->dpad) {
  212. if(event.input.type == InputTypePress) {
  213. furi_hal_gpio_write(pin_down, false);
  214. } else if(event.input.type == InputTypeRelease) {
  215. furi_hal_gpio_write(pin_down, true);
  216. }
  217. } else {
  218. if(event.input.type == InputTypePress && coleco->column > 0) {
  219. coleco->column--;
  220. coleco_write_code(CODE_N);
  221. }
  222. }
  223. break;
  224. case InputKeyRight:
  225. if(coleco->dpad) {
  226. if(event.input.type == InputTypePress) {
  227. furi_hal_gpio_write(pin_right, false);
  228. } else if(event.input.type == InputTypeRelease) {
  229. furi_hal_gpio_write(pin_right, true);
  230. }
  231. } else {
  232. if(event.input.type == InputTypePress && coleco->row < 4) {
  233. coleco->row++;
  234. coleco_write_code(CODE_N);
  235. }
  236. }
  237. break;
  238. case InputKeyLeft:
  239. if(coleco->dpad) {
  240. if(event.input.type == InputTypePress) {
  241. furi_hal_gpio_write(pin_left, false);
  242. } else if(event.input.type == InputTypeRelease) {
  243. furi_hal_gpio_write(pin_left, true);
  244. }
  245. } else {
  246. if(event.input.type == InputTypePress && coleco->row > 0) {
  247. coleco->row--;
  248. coleco_write_code(CODE_N);
  249. }
  250. }
  251. break;
  252. case InputKeyOk:
  253. if(coleco->dpad) {
  254. if(event.input.type == InputTypePress) {
  255. furi_hal_gpio_write(pin_fire, false);
  256. } else if(event.input.type == InputTypeRelease) {
  257. furi_hal_gpio_write(pin_fire, true);
  258. }
  259. } else {
  260. if(event.input.type == InputTypePress) {
  261. if(coleco->row == 0) {
  262. if(coleco->column == 2) {
  263. furi_hal_gpio_write(pin_alt, false);
  264. } else {
  265. coleco->dpad = true;
  266. }
  267. } else if(coleco->row == 1) {
  268. if(coleco->column == 0) {
  269. coleco_write_code(CODE_1);
  270. } else if(coleco->column == 1) {
  271. coleco_write_code(CODE_2);
  272. } else {
  273. coleco_write_code(CODE_3);
  274. }
  275. } else if(coleco->row == 2) {
  276. if(coleco->column == 0) {
  277. coleco_write_code(CODE_4);
  278. } else if(coleco->column == 1) {
  279. coleco_write_code(CODE_5);
  280. } else {
  281. coleco_write_code(CODE_6);
  282. }
  283. } else if(coleco->row == 3) {
  284. if(coleco->column == 0) {
  285. coleco_write_code(CODE_7);
  286. } else if(coleco->column == 1) {
  287. coleco_write_code(CODE_8);
  288. } else {
  289. coleco_write_code(CODE_9);
  290. }
  291. } else if(coleco->row == 4) {
  292. if(coleco->column == 0) {
  293. coleco_write_code(CODE_S);
  294. } else if(coleco->column == 1) {
  295. coleco_write_code(CODE_0);
  296. } else {
  297. coleco_write_code(CODE_H);
  298. }
  299. }
  300. }
  301. if(event.input.type == InputTypeRelease) {
  302. furi_hal_gpio_write(pin_alt, true);
  303. coleco_write_code(CODE_N);
  304. }
  305. }
  306. break;
  307. case InputKeyBack:
  308. if(event.input.type == InputTypePress) {
  309. if(coleco->dpad) {
  310. coleco->dpad = false;
  311. } else {
  312. processing = false;
  313. }
  314. }
  315. break;
  316. default:
  317. break;
  318. }
  319. view_port_update(view_port);
  320. }
  321. }
  322. // else
  323. // {
  324. // FURI_LOG_D("Coleco", "FuriMessageQueue: event timeout");
  325. // }
  326. furi_mutex_release(coleco->mutex);
  327. }
  328. furi_hal_power_disable_otg();
  329. view_port_enabled_set(view_port, false);
  330. gui_remove_view_port(gui, view_port);
  331. furi_record_close("gui");
  332. view_port_free(view_port);
  333. furi_message_queue_free(event_queue);
  334. coleco_free(coleco);
  335. return 0;
  336. }