coleco.c 12 KB

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