trade.cpp 13 KB

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