trade.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. #include <furi_hal_light.h>
  2. #include <gui/view.h>
  3. #include <pokemon_icons.h>
  4. #include "../pokemon_app.h"
  5. #define GAME_BOY_CLK gpio_ext_pb2
  6. #define GAME_BOY_SI gpio_ext_pc3
  7. #define GAME_BOY_SO gpio_ext_pb3
  8. #define DELAY_MICROSECONDS 15
  9. #define PKMN_BLANK 0x00
  10. #define ITEM_1_HIGHLIGHTED 0xD0
  11. #define ITEM_2_HIGHLIGHTED 0xD1
  12. #define ITEM_3_HIGHLIGHTED 0xD2
  13. #define ITEM_1_SELECTED 0xD4
  14. #define ITEM_2_SELECTED 0xD5
  15. #define ITEM_3_SELECTED 0xD6
  16. #define PKMN_MASTER 0x01
  17. #define PKMN_SLAVE 0x02
  18. #define PKMN_CONNECTED 0x60
  19. #define PKMN_WAIT 0x7F
  20. #define PKMN_ACTION 0x60
  21. #define PKMN_TRADE_CENTRE ITEM_1_SELECTED
  22. #define PKMN_COLOSSEUM ITEM_2_SELECTED
  23. #define PKMN_BREAK_LINK ITEM_3_SELECTED
  24. #define TRADE_CENTRE_WAIT 0xFD
  25. typedef unsigned char byte;
  26. typedef enum { NOT_CONNECTED, CONNECTED, TRADE_CENTRE, COLOSSEUM } connection_state_t;
  27. typedef enum {
  28. INIT,
  29. READY_TO_GO,
  30. SEEN_FIRST_WAIT,
  31. SENDING_RANDOM_DATA,
  32. WAITING_TO_SEND_DATA,
  33. START_SENDING_DATA,
  34. SENDING_DATA,
  35. DATA_SENT,
  36. SENDING_PATCH_DATA,
  37. TRADE_PENDING,
  38. TRADE_CONFIRMATION,
  39. DONE
  40. } trade_centre_state_t;
  41. /* TODO: Convert all of these to be maintained in a struct in the Trade context */
  42. uint8_t out_data = 0;
  43. uint8_t in_data = 0;
  44. uint8_t shift = 0;
  45. uint32_t time = 0;
  46. volatile int counter = 0;
  47. volatile bool procesing = true;
  48. volatile connection_state_t connection_state = NOT_CONNECTED;
  49. volatile trade_centre_state_t trade_centre_state = INIT;
  50. unsigned char INPUT_BLOCK[405];
  51. void screen_gameboy_connect(Canvas* const canvas) {
  52. canvas_draw_frame(canvas, 0, 0, 128, 64);
  53. canvas_draw_icon(canvas, 1, 21, &I_Connect_me_62x31);
  54. canvas_draw_icon(canvas, 0, 53, &I_Background_128x11);
  55. canvas_draw_icon(canvas, 80, 0, &I_game_boy);
  56. canvas_draw_icon(canvas, 8, 2, &I_Space_65x18);
  57. canvas_draw_str(canvas, 18, 13, "Connect GB");
  58. }
  59. void screen_gameboy_connected(Canvas* const canvas) {
  60. canvas_draw_frame(canvas, 0, 0, 128, 64);
  61. canvas_draw_icon(canvas, 1, 21, &I_Connected_62x31);
  62. canvas_draw_icon(canvas, 0, 53, &I_Background_128x11);
  63. canvas_draw_icon(canvas, 80, 0, &I_game_boy);
  64. canvas_draw_icon(canvas, 8, 2, &I_Space_65x18);
  65. canvas_draw_str(canvas, 18, 13, "Connected!");
  66. }
  67. int time_in_seconds = 0;
  68. static void trade_draw_callback(Canvas* canvas, void* model) {
  69. const char* gameboy_status_text = NULL;
  70. PokemonFap* pokemon_fap = *(PokemonFap**)model;
  71. canvas_clear(canvas);
  72. if(!pokemon_fap->trading) {
  73. if(!pokemon_fap->connected) {
  74. furi_hal_light_set(LightGreen, 0x00);
  75. furi_hal_light_set(LightBlue, 0x00);
  76. furi_hal_light_set(LightRed, 0xff);
  77. screen_gameboy_connect(canvas);
  78. } else {
  79. furi_hal_light_set(LightGreen, 0xff);
  80. furi_hal_light_set(LightBlue, 0x00);
  81. furi_hal_light_set(LightRed, 0x00);
  82. screen_gameboy_connected(canvas);
  83. }
  84. } else {
  85. switch(pokemon_fap->gameboy_status) {
  86. case GAMEBOY_TRADING:
  87. furi_hal_light_set(LightGreen, 0x00);
  88. furi_hal_light_set(LightRed, 0x00);
  89. if(time_in_seconds % 2 == 1) {
  90. furi_hal_light_set(LightBlue, 0xff);
  91. canvas_draw_icon(canvas, 0, 0, &I_gb_step_1);
  92. } else {
  93. furi_hal_light_set(LightBlue, 0x00);
  94. canvas_draw_icon(canvas, 0, 0, &I_gb_step_2);
  95. }
  96. break;
  97. case GAMEBOY_READY:
  98. case GAMEBOY_WAITING:
  99. case GAMEBOY_SEND:
  100. canvas_draw_icon(
  101. canvas, 38, 11, pokemon_fap->pokemon_table[pokemon_fap->curr_pokemon].icon);
  102. break;
  103. default:
  104. // Default state added to eliminated enum warning
  105. break;
  106. }
  107. canvas_draw_icon(canvas, 0, 53, &I_Background_128x11);
  108. canvas_draw_frame(canvas, 0, 0, 128, 64);
  109. canvas_draw_icon(canvas, 24, 0, &I_Space_80x18);
  110. switch(pokemon_fap->gameboy_status) {
  111. case GAMEBOY_READY:
  112. gameboy_status_text = "READY";
  113. break;
  114. case GAMEBOY_WAITING:
  115. gameboy_status_text = "WAITING";
  116. break;
  117. case GAMEBOY_TRADE_READY:
  118. gameboy_status_text = "READY";
  119. break;
  120. case GAMEBOY_SEND:
  121. gameboy_status_text = "DEAL...";
  122. break;
  123. case GAMEBOY_PENDING:
  124. gameboy_status_text = "PENDING...";
  125. break;
  126. case GAMEBOY_TRADING:
  127. gameboy_status_text = "TRADING...";
  128. break;
  129. default:
  130. gameboy_status_text = "INITIAL";
  131. break;
  132. }
  133. canvas_draw_str(canvas, 48, 12, gameboy_status_text);
  134. canvas_draw_icon(canvas, 27, 1, &I_red_16x15);
  135. time_in_seconds = (int)DWT->CYCCNT / (72000000.0f / 4); // 250ms
  136. }
  137. }
  138. static bool trade_input_callback(InputEvent* event, void* context) {
  139. bool consumed = false;
  140. PokemonFap* pokemon_fap = (PokemonFap*)context;
  141. furi_assert(context);
  142. if(event->type == InputTypePress && event->key == InputKeyBack) {
  143. view_dispatcher_switch_to_view(pokemon_fap->view_dispatcher, AppViewSelectPokemon);
  144. consumed = true;
  145. }
  146. return consumed;
  147. }
  148. uint32_t micros() {
  149. return DWT->CYCCNT / 64;
  150. }
  151. /* Get the response byte from the link partner, updating the connection
  152. * state if needed.
  153. */
  154. byte getConnectResponse(byte in) {
  155. byte ret;
  156. switch(in) {
  157. case PKMN_CONNECTED:
  158. connection_state = CONNECTED;
  159. ret = PKMN_CONNECTED;
  160. break;
  161. case PKMN_MASTER:
  162. ret = PKMN_SLAVE;
  163. break;
  164. case PKMN_BLANK:
  165. ret = PKMN_BLANK;
  166. break;
  167. default:
  168. connection_state = NOT_CONNECTED;
  169. ret = PKMN_BREAK_LINK;
  170. break;
  171. }
  172. return ret;
  173. }
  174. /* Figure out what the pokemon game is requesting and move to that mode.
  175. */
  176. byte getMenuResponse(byte in) {
  177. /* TODO: Find out what this byte means */
  178. byte response = 0x00;
  179. switch(in) {
  180. case PKMN_CONNECTED:
  181. response = PKMN_CONNECTED;
  182. break;
  183. case PKMN_TRADE_CENTRE:
  184. connection_state = TRADE_CENTRE;
  185. break;
  186. case PKMN_COLOSSEUM:
  187. connection_state = COLOSSEUM;
  188. break;
  189. case PKMN_BREAK_LINK:
  190. case PKMN_MASTER:
  191. connection_state = NOT_CONNECTED;
  192. response = PKMN_BREAK_LINK;
  193. break;
  194. default:
  195. response = in;
  196. break;
  197. }
  198. return response;
  199. }
  200. byte getTradeCentreResponse(byte in, void* context) {
  201. PokemonFap* pokemon_fap = (PokemonFap*)context;
  202. uint8_t* trade_party_flat = (uint8_t*)pokemon_fap->trade_party;
  203. byte send = in;
  204. furi_assert(context);
  205. switch(trade_centre_state) {
  206. case INIT:
  207. // TODO: What does this value of in mean?
  208. if(in == 0x00) {
  209. // TODO: What does counter signify here?
  210. if(counter == 5) {
  211. trade_centre_state = READY_TO_GO;
  212. // CLICK EN LA MESA
  213. pokemon_fap->gameboy_status = GAMEBOY_READY;
  214. }
  215. counter++;
  216. }
  217. break;
  218. case READY_TO_GO:
  219. if((in & 0xF0) == 0xF0) trade_centre_state = SEEN_FIRST_WAIT;
  220. break;
  221. case SEEN_FIRST_WAIT:
  222. if((in & 0xF0) != 0xF0) {
  223. counter = 0;
  224. trade_centre_state = SENDING_RANDOM_DATA;
  225. }
  226. break;
  227. case SENDING_RANDOM_DATA:
  228. if((in & 0xF0) == 0xF0) {
  229. if(counter == 5) {
  230. trade_centre_state = WAITING_TO_SEND_DATA;
  231. pokemon_fap->gameboy_status = GAMEBOY_WAITING;
  232. }
  233. counter++;
  234. }
  235. break;
  236. case WAITING_TO_SEND_DATA:
  237. if((in & 0xF0) != 0xF0) {
  238. counter = 0;
  239. INPUT_BLOCK[counter] = in;
  240. send = trade_party_flat[counter];
  241. counter++;
  242. trade_centre_state = SENDING_DATA;
  243. }
  244. break;
  245. case SENDING_DATA:
  246. INPUT_BLOCK[counter] = in;
  247. send = trade_party_flat[counter];
  248. counter++;
  249. if(counter == 405) //TODO: replace with sizeof struct rather than static number
  250. trade_centre_state = SENDING_PATCH_DATA;
  251. break;
  252. case SENDING_PATCH_DATA:
  253. if(in == 0xFD) {
  254. counter = 0;
  255. send = 0xFD;
  256. } else {
  257. counter++;
  258. if(counter == 197) // TODO: What is this magic value?
  259. trade_centre_state = TRADE_PENDING;
  260. }
  261. break;
  262. case TRADE_PENDING:
  263. /* TODO: What are these states */
  264. if(in == 0x6F) {
  265. trade_centre_state = READY_TO_GO;
  266. send = 0x6F;
  267. pokemon_fap->gameboy_status = GAMEBOY_TRADE_READY;
  268. } else if((in & 0x60) == 0x60) {
  269. send = 0x60; // first pokemon
  270. pokemon_fap->gameboy_status = GAMEBOY_SEND;
  271. } else if(in == 0x00) {
  272. send = 0;
  273. trade_centre_state = TRADE_CONFIRMATION;
  274. }
  275. break;
  276. case TRADE_CONFIRMATION:
  277. if(in == 0x61) {
  278. trade_centre_state = TRADE_PENDING;
  279. pokemon_fap->gameboy_status = GAMEBOY_PENDING;
  280. } else if((in & 0x60) == 0x60) {
  281. trade_centre_state = DONE;
  282. }
  283. break;
  284. case DONE:
  285. if(in == 0x00) {
  286. send = 0;
  287. trade_centre_state = INIT;
  288. pokemon_fap->gameboy_status = GAMEBOY_TRADING;
  289. }
  290. break;
  291. default:
  292. // Do Nothing
  293. break;
  294. }
  295. return send;
  296. }
  297. void transferBit(void* context) {
  298. PokemonFap* pokemon_fap = (PokemonFap*)context;
  299. furi_assert(context);
  300. byte raw_data = furi_hal_gpio_read(&GAME_BOY_SI);
  301. in_data |= raw_data << (7 - shift);
  302. if(++shift > 7) {
  303. shift = 0;
  304. switch(connection_state) {
  305. case NOT_CONNECTED:
  306. pokemon_fap->connected = false;
  307. out_data = getConnectResponse(in_data);
  308. break;
  309. case CONNECTED:
  310. pokemon_fap->connected = true;
  311. out_data = getMenuResponse(in_data);
  312. break;
  313. case TRADE_CENTRE:
  314. out_data = getTradeCentreResponse(in_data, pokemon_fap);
  315. break;
  316. default:
  317. out_data = in_data;
  318. break;
  319. }
  320. in_data = 0; // TODO: I don't think this is necessary?
  321. }
  322. while(procesing && !furi_hal_gpio_read(&GAME_BOY_CLK))
  323. ;
  324. furi_hal_gpio_write(&GAME_BOY_SO, out_data & 0x80 ? true : false);
  325. furi_delay_us(
  326. DELAY_MICROSECONDS); // Wait 20-60us ... 120us max (in slave mode is not necessary)
  327. // TODO: The above comment doesn't make sense as DELAY_MICROSECONDS is defined as 15
  328. if(trade_centre_state == READY_TO_GO) pokemon_fap->trading = true;
  329. out_data = out_data << 1;
  330. }
  331. void input_clk_gameboy(void* context) {
  332. furi_assert(context);
  333. if(time > 0) {
  334. // if there is no response from the master in 120 microseconds, the counters are reset
  335. if(micros() - time > 120) {
  336. // IDLE & Reset
  337. in_data = 0;
  338. shift = 0;
  339. }
  340. }
  341. transferBit(context);
  342. time = micros();
  343. }
  344. void trade_enter_callback(void* context) {
  345. PokemonFap* pokemon_fap = (PokemonFap*)context;
  346. furi_assert(context);
  347. pokemon_fap->trading = false;
  348. pokemon_fap->connected = false;
  349. pokemon_fap->gameboy_status = GAMEBOY_INITIAL;
  350. pokemon_fap->trade_party->party_members[0] =
  351. pokemon_fap->pokemon_table[pokemon_fap->curr_pokemon].species;
  352. // B3 (Pin6) / SO (2)
  353. furi_hal_gpio_write(&GAME_BOY_SO, false);
  354. furi_hal_gpio_init(&GAME_BOY_SO, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
  355. // B2 (Pin5) / SI (3)
  356. furi_hal_gpio_write(&GAME_BOY_SI, false);
  357. furi_hal_gpio_init(&GAME_BOY_SI, GpioModeInput, GpioPullNo, GpioSpeedVeryHigh);
  358. // // C3 (Pin7) / CLK (5)
  359. furi_hal_gpio_init(
  360. &GAME_BOY_CLK,
  361. GpioModeInterruptRise,
  362. GpioPullNo,
  363. GpioSpeedVeryHigh); // <-- This line causes the "OK" to stop functioning when exiting the application, so a reboot of the Flipper Zero is required.
  364. furi_hal_gpio_remove_int_callback(&GAME_BOY_CLK);
  365. furi_hal_gpio_add_int_callback(&GAME_BOY_CLK, input_clk_gameboy, pokemon_fap);
  366. // furi_hal_gpio_disable_int_callback(&GAME_BOY_CLK);
  367. // furi_hal_gpio_remove_int_callback(&GAME_BOY_CLK);
  368. // Reset GPIO pins to default state
  369. // furi_hal_gpio_init(&GAME_BOY_CLK, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  370. }
  371. bool trade_custom_callback(uint32_t event, void* context) {
  372. UNUSED(event);
  373. PokemonFap* pokemon_fap = (PokemonFap*)context;
  374. furi_assert(context);
  375. view_dispatcher_send_custom_event(pokemon_fap->view_dispatcher, 0);
  376. return true;
  377. }
  378. void disconnect_pin(const GpioPin* pin) {
  379. furi_hal_gpio_init(pin, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  380. furi_hal_gpio_write(pin, true);
  381. }
  382. void trade_exit_callback(void* context) {
  383. furi_assert(context);
  384. procesing = false;
  385. furi_hal_light_set(LightGreen, 0x00);
  386. furi_hal_light_set(LightBlue, 0x00);
  387. furi_hal_light_set(LightRed, 0x00);
  388. }
  389. View* trade_alloc(PokemonFap* pokemon_fap) {
  390. View* view;
  391. view = view_alloc();
  392. procesing = true;
  393. view_set_context(view, pokemon_fap);
  394. view_allocate_model(view, ViewModelTypeLockFree, sizeof(PokemonFap**));
  395. with_view_model_cpp(
  396. view, PokemonFap**, model_fap, { *model_fap = pokemon_fap; }, false);
  397. view_set_draw_callback(view, trade_draw_callback);
  398. view_set_input_callback(view, trade_input_callback);
  399. view_set_enter_callback(view, trade_enter_callback);
  400. view_set_custom_callback(view, trade_custom_callback);
  401. view_set_exit_callback(view, trade_exit_callback);
  402. return view;
  403. }
  404. void trade_free(PokemonFap* pokemon_fap) {
  405. furi_assert(pokemon_fap);
  406. // Free resources
  407. procesing = false;
  408. furi_hal_gpio_remove_int_callback(&GAME_BOY_CLK);
  409. disconnect_pin(&GAME_BOY_CLK);
  410. view_free(pokemon_fap->trade_view);
  411. }