coleco.c 12 KB

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