coleco.c 13 KB

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