coleco.c 12 KB

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