trade.cpp 13 KB

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