trade.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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_table[model->current_pokemon].icon);
  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. unsigned char convertCharToTagHex(char c) {
  274. switch (c) {
  275. case 'A': return A_;
  276. case 'B': return B_;
  277. case 'C': return C_;
  278. case 'D': return D_;
  279. case 'E': return E_;
  280. case 'F': return F_;
  281. case 'G': return G_;
  282. case 'H': return H_;
  283. case 'I': return I_;
  284. case 'J': return J_;
  285. case 'K': return K_;
  286. case 'L': return L_;
  287. case 'M': return M_;
  288. case 'N': return N_;
  289. case 'O': return O_;
  290. case 'P': return P_;
  291. case 'Q': return Q_;
  292. case 'R': return R_;
  293. case 'S': return S_;
  294. case 'T': return T_;
  295. case 'U': return U_;
  296. case 'V': return V_;
  297. case 'W': return W_;
  298. case 'X': return X_;
  299. case 'Y': return Y_;
  300. case 'Z': return Z_;
  301. case 'a': return A_;
  302. case 'b': return B_;
  303. case 'c': return C_;
  304. case 'd': return D_;
  305. case 'e': return E_;
  306. case 'f': return F_;
  307. case 'g': return G_;
  308. case 'h': return H_;
  309. case 'i': return I_;
  310. case 'j': return J_;
  311. case 'k': return K_;
  312. case 'l': return L_;
  313. case 'm': return M_;
  314. case 'n': return N_;
  315. case 'o': return O_;
  316. case 'p': return P_;
  317. case 'q': return Q_;
  318. case 'r': return R_;
  319. case 's': return S_;
  320. case 't': return T_;
  321. case 'u': return U_;
  322. case 'v': return V_;
  323. case 'w': return W_;
  324. case 'x': return X_;
  325. case 'y': return Y_;
  326. case 'z': return Z_;
  327. case ' ': return SPACE_;
  328. case '.': return PERIOD_;
  329. case '\'': return S_QUOTE_;
  330. case '♂': return MALE_;
  331. case '♀': return FEMALE_;
  332. default: return 0x00;
  333. }
  334. }
  335. void trade_enter_callback(void* context) {
  336. furi_assert(context);
  337. Trade* trade = (Trade*)context;
  338. with_view_model_cpp(
  339. trade->view,
  340. SelectPokemonModel*,
  341. model,
  342. {
  343. model->current_pokemon = trade->app->current_pokemon;
  344. model->pokemon_hex_code = trade->app->pokemon_hex_code;
  345. model->current_level = trade->app->current_level;
  346. model->current_move = trade->app->current_move;
  347. model->move1_hex_code = trade->app->move1_hex_code;
  348. model->move2_hex_code = trade->app->move2_hex_code;
  349. model->move3_hex_code = trade->app->move3_hex_code;
  350. model->move4_hex_code = trade->app->move4_hex_code;
  351. model->trading = false;
  352. model->connected = false;
  353. model->gameboy_status = GAMEBOY_INITIAL;
  354. },
  355. true);
  356. FURI_LOG_D(TAG, "[Trade] Current Pokemon: %d", trade->app->current_pokemon);
  357. // Set the Pokemon name
  358. unsigned char nickname[11];
  359. for (size_t i = 0; i < 11; ++i) {
  360. nickname[i] = 0x00;
  361. }
  362. for (size_t i = 0; i < strlen(pokemon_table[trade->app->current_pokemon].name); ++i) {
  363. nickname[i] = convertCharToTagHex(pokemon_table[trade->app->current_pokemon].name[i]);
  364. }
  365. for (size_t i = 0; i < 11; ++i) {
  366. if(nickname[i] == 0x00) {
  367. nickname[i] = 0x50;
  368. }
  369. break;
  370. }
  371. memcpy(DATA_BLOCK2.nickname[0].str, nickname, sizeof(nickname));
  372. FURI_LOG_D(TAG, "[Trade] Pokemon Name: %s", nickname);
  373. // Set the Pokemon hex code
  374. DATA_BLOCK[12] = trade->app->pokemon_hex_code;
  375. FURI_LOG_D(TAG, "[Trade] Pokemon Hex Code: %x", trade->app->pokemon_hex_code);
  376. // Set the Pokemon level
  377. FURI_LOG_D(TAG, "[Trade] Current Level: %d", trade->app->current_level);
  378. int level = trade->app->current_level;
  379. DATA_BLOCK2.party[0].level = level & 0xFF;
  380. DATA_BLOCK2.party[0].level_again = level & 0xFF;
  381. FURI_LOG_D(TAG, "[Trade] Level Hex Code: %x", DATA_BLOCK2.party[0].level);
  382. // Set the Pokemon experience
  383. int exp = 0;
  384. if(pokemon_table[trade->app->current_pokemon].xp_group == 0) {
  385. exp = 1.25 * level * level * level;
  386. } else if(pokemon_table[trade->app->current_pokemon].xp_group == 1) {
  387. exp = (1.2 * level * level * level) - (15 * level * level) + (100 * level) - 140;
  388. } else if(pokemon_table[trade->app->current_pokemon].xp_group == 2) {
  389. exp = level * level * level;
  390. } else if(pokemon_table[trade->app->current_pokemon].xp_group == 3) {
  391. exp = 0.8 * level * level * level;
  392. }
  393. DATA_BLOCK2.party[0].exp[0] = (exp >> 16) & 0xFF;
  394. DATA_BLOCK2.party[0].exp[1] = (exp >> 8) & 0xFF;
  395. DATA_BLOCK2.party[0].exp[2] = exp & 0xFF;
  396. FURI_LOG_D(TAG, "[Trade] XP 1: %x", DATA_BLOCK2.party[0].exp[0]);
  397. FURI_LOG_D(TAG, "[Trade] XP 2: %x", DATA_BLOCK2.party[0].exp[1]);
  398. FURI_LOG_D(TAG, "[Trade] XP 3: %x", DATA_BLOCK2.party[0].exp[2]);
  399. // Set the Pokemon stat experience
  400. uint16_t statexp = 0x0000;
  401. // uint16_t statexp = (65535 / 100) * level;
  402. DATA_BLOCK2.party[0].hp_ev = ((statexp >> 8) & 0x00FF) | ((statexp << 8) & 0xFF00);
  403. DATA_BLOCK2.party[0].atk_ev = ((statexp >> 8) & 0x00FF) | ((statexp << 8) & 0xFF00);
  404. DATA_BLOCK2.party[0].def_ev = ((statexp >> 8) & 0x00FF) | ((statexp << 8) & 0xFF00);
  405. DATA_BLOCK2.party[0].spd_ev = ((statexp >> 8) & 0x00FF) | ((statexp << 8) & 0xFF00);
  406. DATA_BLOCK2.party[0].special_ev = ((statexp >> 8) & 0x00FF) | ((statexp << 8) & 0xFF00);
  407. FURI_LOG_D(TAG, "[Trade] Pokemon Stat EXP: %d", statexp);
  408. FURI_LOG_D(TAG, "[Trade] Pokemon HP EV: %x", DATA_BLOCK2.party[0].hp_ev);
  409. FURI_LOG_D(TAG, "[Trade] Pokemon Attack EV: %x", DATA_BLOCK2.party[0].atk_ev);
  410. FURI_LOG_D(TAG, "[Trade] Pokemon Defence EV: %x", DATA_BLOCK2.party[0].def_ev);
  411. FURI_LOG_D(TAG, "[Trade] Pokemon Speed EV: %x", DATA_BLOCK2.party[0].spd_ev);
  412. FURI_LOG_D(TAG, "[Trade] Pokemon Special EV: %x", DATA_BLOCK2.party[0].special_ev);
  413. // Set the Pokemon max hp
  414. // uint16_t max_hp = (0.01 * (2 * pokemon_table[trade->app->current_pokemon].base_hp + 15 + (0.25 * statexp)) * level) + level + 10;
  415. uint16_t max_hp = floor((((2 * (pokemon_table[trade->app->current_pokemon].base_hp + 15)) + floor(sqrt(statexp) / 4)) * level) / 100) + (level + 10);
  416. DATA_BLOCK2.party[0].max_hp = ((max_hp >> 8) & 0x00FF) | ((max_hp << 8) & 0xFF00);
  417. DATA_BLOCK2.party[0].hp = ((max_hp >> 8) & 0x00FF) | ((max_hp << 8) & 0xFF00);
  418. FURI_LOG_D(TAG, "[Trade] Pokemon Max HP: %x", DATA_BLOCK2.party[0].max_hp);
  419. // Set the Pokemon stats
  420. // uint8_t attack = (0.01 * (2 * pokemon_table[trade->app->current_pokemon].base_atk + 15 + (0.25 * statexp)) * level) + 5;
  421. uint16_t attack = floor((((2 * (pokemon_table[trade->app->current_pokemon].base_atk + 15)) + floor(sqrt(statexp) / 4)) * level) / 100) + 5;
  422. DATA_BLOCK2.party[0].atk = ((attack >> 8) & 0x00FF) | ((attack << 8) & 0xFF00);
  423. // uint8_t defence = (0.01 * (2 * pokemon_table[trade->app->current_pokemon].base_def + 15 + (0.25 * statexp)) * level) + 5;
  424. uint16_t defence = floor((((2 * (pokemon_table[trade->app->current_pokemon].base_def + 15)) + floor(sqrt(statexp) / 4)) * level) / 100) + 5;
  425. DATA_BLOCK2.party[0].def = ((defence >> 8) & 0x00FF) | ((defence << 8) & 0xFF00);
  426. // uint8_t speed = (0.01 * (2 * pokemon_table[trade->app->current_pokemon].base_spd + 15 + (0.25 * statexp)) * level) + 5;
  427. uint16_t speed = floor((((2 * (pokemon_table[trade->app->current_pokemon].base_spd + 15)) + floor(sqrt(statexp) / 4)) * level) / 100) + 5;
  428. DATA_BLOCK2.party[0].spd = ((speed >> 8) & 0x00FF) | ((speed << 8) & 0xFF00);
  429. // uint8_t special = (0.01 * (2 * pokemon_table[trade->app->current_pokemon].base_special + 15 + (0.25 * statexp)) * level) + 5;
  430. uint16_t special = floor((((2 * (pokemon_table[trade->app->current_pokemon].base_special + 15)) + floor(sqrt(statexp) / 4)) * level) / 100) + 5;
  431. DATA_BLOCK2.party[0].special = ((special >> 8) & 0x00FF) | ((special << 8) & 0xFF00);
  432. FURI_LOG_D(TAG, "[Trade] Pokemon Attack: %x", DATA_BLOCK2.party[0].atk);
  433. FURI_LOG_D(TAG, "[Trade] Pokemon Defence: %x", DATA_BLOCK2.party[0].def);
  434. FURI_LOG_D(TAG, "[Trade] Pokemon Speed: %x", DATA_BLOCK2.party[0].spd);
  435. FURI_LOG_D(TAG, "[Trade] Pokemon Special: %x", DATA_BLOCK2.party[0].special);
  436. // Set the Pokemon types
  437. DATA_BLOCK2.party[0].type[0] = pokemon_table[trade->app->current_pokemon].type1;
  438. DATA_BLOCK2.party[0].type[1] = pokemon_table[trade->app->current_pokemon].type2;
  439. FURI_LOG_D(TAG, "[Trade] Type 1: %x", DATA_BLOCK2.party[0].type[0]);
  440. FURI_LOG_D(TAG, "[Trade] Type 2: %x", DATA_BLOCK2.party[0].type[1]);
  441. // Set the Pokemon moves
  442. DATA_BLOCK2.party[0].move[0] = trade->app->move1_hex_code;
  443. DATA_BLOCK2.party[0].move[1] = trade->app->move2_hex_code;
  444. DATA_BLOCK2.party[0].move[2] = trade->app->move3_hex_code;
  445. DATA_BLOCK2.party[0].move[3] = trade->app->move4_hex_code;
  446. FURI_LOG_D(TAG, "[Trade] Move 1 Hex Code: %x", trade->app->move1_hex_code);
  447. FURI_LOG_D(TAG, "[Trade] Move 2 Hex Code: %x", trade->app->move2_hex_code);
  448. FURI_LOG_D(TAG, "[Trade] Move 3 Hex Code: %x", trade->app->move3_hex_code);
  449. FURI_LOG_D(TAG, "[Trade] Move 4 Hex Code: %x", trade->app->move4_hex_code);
  450. // B3 (Pin6) / SO (2)
  451. furi_hal_gpio_write(&GAME_BOY_SO, false);
  452. furi_hal_gpio_init(&GAME_BOY_SO, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
  453. // B2 (Pin5) / SI (3)
  454. furi_hal_gpio_write(&GAME_BOY_SI, false);
  455. furi_hal_gpio_init(&GAME_BOY_SI, GpioModeInput, GpioPullNo, GpioSpeedVeryHigh);
  456. // // C3 (Pin7) / CLK (5)
  457. furi_hal_gpio_init(
  458. &GAME_BOY_CLK,
  459. GpioModeInterruptRise,
  460. GpioPullNo,
  461. GpioSpeedVeryHigh); // <-- This line causes the "OK" to stop functioning when exiting the application, so a reboot of the Flipper Zero is required.
  462. furi_hal_gpio_remove_int_callback(&GAME_BOY_CLK);
  463. furi_hal_gpio_add_int_callback(&GAME_BOY_CLK, input_clk_gameboy, trade);
  464. // furi_hal_gpio_disable_int_callback(&GAME_BOY_CLK);
  465. // furi_hal_gpio_remove_int_callback(&GAME_BOY_CLK);
  466. // Reset GPIO pins to default state
  467. // furi_hal_gpio_init(&GAME_BOY_CLK, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  468. }
  469. bool trade_custom_callback(uint32_t event, void* context) {
  470. UNUSED(event);
  471. furi_assert(context);
  472. Trade* trade = (Trade*)context;
  473. view_dispatcher_send_custom_event(trade->app->view_dispatcher, 0);
  474. return true;
  475. }
  476. void disconnect_pin(const GpioPin* pin) {
  477. furi_hal_gpio_init(pin, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  478. furi_hal_gpio_write(pin, true);
  479. }
  480. void trade_exit_callback(void* context) {
  481. furi_assert(context);
  482. procesing = false;
  483. furi_hal_light_set(LightGreen, 0x00);
  484. furi_hal_light_set(LightBlue, 0x00);
  485. furi_hal_light_set(LightRed, 0x00);
  486. }
  487. Trade* trade_alloc(App* app) {
  488. Trade* trade = (Trade*)malloc(sizeof(Trade));
  489. trade->app = app;
  490. trade->view = view_alloc();
  491. procesing = true;
  492. view_set_context(trade->view, trade);
  493. view_allocate_model(trade->view, ViewModelTypeLockFree, sizeof(SelectPokemonModel));
  494. view_set_draw_callback(trade->view, trade_draw_callback);
  495. view_set_input_callback(trade->view, trade_input_callback);
  496. view_set_enter_callback(trade->view, trade_enter_callback);
  497. view_set_custom_callback(trade->view, trade_custom_callback);
  498. view_set_exit_callback(trade->view, trade_exit_callback);
  499. return trade;
  500. }
  501. void trade_free(App* app) {
  502. furi_assert(app);
  503. // Free resources
  504. procesing = false;
  505. furi_hal_gpio_remove_int_callback(&GAME_BOY_CLK);
  506. disconnect_pin(&GAME_BOY_CLK);
  507. view_free(app->trade->view);
  508. free(app->trade);
  509. }
  510. View* trade_get_view(App* app) {
  511. furi_assert(app);
  512. return app->trade->view;
  513. }