trade.cpp 13 KB

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