trade.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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 '\u2642': return MALE_;
  331. case '\u2640': 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_stats = trade->app->current_stats;
  347. model->current_move = trade->app->current_move;
  348. model->move1_hex_code = trade->app->move1_hex_code;
  349. model->move2_hex_code = trade->app->move2_hex_code;
  350. model->move3_hex_code = trade->app->move3_hex_code;
  351. model->move4_hex_code = trade->app->move4_hex_code;
  352. model->trading = false;
  353. model->connected = false;
  354. model->gameboy_status = GAMEBOY_INITIAL;
  355. },
  356. true);
  357. FURI_LOG_D(TAG, "[Trade] Current Pokemon: %d", trade->app->current_pokemon);
  358. // Set the Pokemon name
  359. unsigned char nickname[11];
  360. for (size_t i = 0; i < 11; ++i) {
  361. nickname[i] = 0x50;
  362. }
  363. for (size_t i = 0; i < strlen(pokemon_table[trade->app->current_pokemon].name); ++i) {
  364. nickname[i] = convertCharToTagHex(pokemon_table[trade->app->current_pokemon].name[i]);
  365. }
  366. memcpy(DATA_BLOCK2.nickname[0].str, nickname, sizeof(nickname));
  367. FURI_LOG_D(TAG, "[Trade] Pokemon Name: %s", pokemon_table[trade->app->current_pokemon].name);
  368. // Set the Pokemon hex code
  369. DATA_BLOCK[12] = trade->app->pokemon_hex_code;
  370. FURI_LOG_D(TAG, "[Trade] Pokemon Hex Code: %x", trade->app->pokemon_hex_code);
  371. // Set the Pokemon level
  372. FURI_LOG_D(TAG, "[Trade] Current Level: %d", trade->app->current_level);
  373. uint8_t level = trade->app->current_level;
  374. DATA_BLOCK2.party[0].level = level & 0xFF;
  375. DATA_BLOCK2.party[0].level_again = level & 0xFF;
  376. FURI_LOG_D(TAG, "[Trade] Level Hex Code: %x", DATA_BLOCK2.party[0].level);
  377. // Set the Pokemon experience
  378. int32_t exp = 0;
  379. if(pokemon_table[trade->app->current_pokemon].xp_group == 0) {
  380. exp = 1.25 * level * level * level;
  381. } else if(pokemon_table[trade->app->current_pokemon].xp_group == 1) {
  382. exp = (1.2 * level * level * level) - (15 * level * level) + (100 * level) - 140;
  383. } else if(pokemon_table[trade->app->current_pokemon].xp_group == 2) {
  384. exp = level * level * level;
  385. } else if(pokemon_table[trade->app->current_pokemon].xp_group == 3) {
  386. exp = 0.8 * level * level * level;
  387. }
  388. DATA_BLOCK2.party[0].exp[0] = (exp >> 16) & 0xFF;
  389. DATA_BLOCK2.party[0].exp[1] = (exp >> 8) & 0xFF;
  390. DATA_BLOCK2.party[0].exp[2] = exp & 0xFF;
  391. FURI_LOG_D(TAG, "[Trade] XP 1: %x", DATA_BLOCK2.party[0].exp[0]);
  392. FURI_LOG_D(TAG, "[Trade] XP 2: %x", DATA_BLOCK2.party[0].exp[1]);
  393. FURI_LOG_D(TAG, "[Trade] XP 3: %x", DATA_BLOCK2.party[0].exp[2]);
  394. // Set the Pokemon stat experience
  395. uint16_t statexp = 0x0000;
  396. if(trade->app->current_stats == 1 || trade->app->current_stats == 4) {
  397. statexp = (65535 / 100) * level;
  398. } else if(trade->app->current_stats == 2 || trade->app->current_stats == 5) {
  399. statexp = 65535;
  400. }
  401. DATA_BLOCK2.party[0].hp_ev = ((statexp >> 8) & 0x00FF) | ((statexp << 8) & 0xFF00);
  402. DATA_BLOCK2.party[0].atk_ev = ((statexp >> 8) & 0x00FF) | ((statexp << 8) & 0xFF00);
  403. DATA_BLOCK2.party[0].def_ev = ((statexp >> 8) & 0x00FF) | ((statexp << 8) & 0xFF00);
  404. DATA_BLOCK2.party[0].spd_ev = ((statexp >> 8) & 0x00FF) | ((statexp << 8) & 0xFF00);
  405. DATA_BLOCK2.party[0].special_ev = ((statexp >> 8) & 0x00FF) | ((statexp << 8) & 0xFF00);
  406. FURI_LOG_D(TAG, "[Trade] Pokemon Stat EXP: %d", statexp);
  407. FURI_LOG_D(TAG, "[Trade] Pokemon HP EV: %x", DATA_BLOCK2.party[0].hp_ev);
  408. FURI_LOG_D(TAG, "[Trade] Pokemon Attack EV: %x", DATA_BLOCK2.party[0].atk_ev);
  409. FURI_LOG_D(TAG, "[Trade] Pokemon Defence EV: %x", DATA_BLOCK2.party[0].def_ev);
  410. FURI_LOG_D(TAG, "[Trade] Pokemon Speed EV: %x", DATA_BLOCK2.party[0].spd_ev);
  411. FURI_LOG_D(TAG, "[Trade] Pokemon Special EV: %x", DATA_BLOCK2.party[0].special_ev);
  412. // Set the Pokemon stats
  413. uint8_t atk_iv = 15;
  414. uint8_t def_iv = 15;
  415. uint8_t spd_iv = 15;
  416. uint8_t special_iv = 15;
  417. uint8_t hp_iv = 15;
  418. if(trade->app->current_stats <= 2) {
  419. atk_iv = rand() % 15;
  420. def_iv = rand() % 15;
  421. spd_iv = rand() % 15;
  422. special_iv = rand() % 15;
  423. DATA_BLOCK2.party[0].iv = ((atk_iv & 0b00001111) << 12) | ((def_iv & 0b00001111) << 8) | ((spd_iv & 0b00001111) << 4) | ((special_iv & 0b00001111));
  424. hp_iv = (DATA_BLOCK2.party[0].iv & 0xAA) >> 4;
  425. }
  426. FURI_LOG_D(TAG, "[Trade] Pokemon IV: %x", DATA_BLOCK2.party[0].iv);
  427. FURI_LOG_D(TAG, "[Trade] Pokemon HP IV: %x", hp_iv);
  428. FURI_LOG_D(TAG, "[Trade] Pokemon Attack IV: %x", atk_iv);
  429. FURI_LOG_D(TAG, "[Trade] Pokemon Defence IV: %x", def_iv);
  430. FURI_LOG_D(TAG, "[Trade] Pokemon Speed IV: %x", spd_iv);
  431. FURI_LOG_D(TAG, "[Trade] Pokemon Special IV: %x", special_iv);
  432. uint16_t max_hp = floor((((2 * (pokemon_table[trade->app->current_pokemon].base_hp + hp_iv)) + floor(sqrt(statexp) / 4)) * level) / 100) + (level + 10);
  433. DATA_BLOCK2.party[0].max_hp = ((max_hp >> 8) & 0x00FF) | ((max_hp << 8) & 0xFF00);
  434. DATA_BLOCK2.party[0].hp = ((max_hp >> 8) & 0x00FF) | ((max_hp << 8) & 0xFF00);
  435. uint16_t attack = floor((((2 * (pokemon_table[trade->app->current_pokemon].base_atk + atk_iv)) + floor(sqrt(statexp) / 4)) * level) / 100) + 5;
  436. DATA_BLOCK2.party[0].atk = ((attack >> 8) & 0x00FF) | ((attack << 8) & 0xFF00);
  437. uint16_t defence = floor((((2 * (pokemon_table[trade->app->current_pokemon].base_def + def_iv)) + floor(sqrt(statexp) / 4)) * level) / 100) + 5;
  438. DATA_BLOCK2.party[0].def = ((defence >> 8) & 0x00FF) | ((defence << 8) & 0xFF00);
  439. uint16_t speed = floor((((2 * (pokemon_table[trade->app->current_pokemon].base_spd + spd_iv)) + floor(sqrt(statexp) / 4)) * level) / 100) + 5;
  440. DATA_BLOCK2.party[0].spd = ((speed >> 8) & 0x00FF) | ((speed << 8) & 0xFF00);
  441. uint16_t special = floor((((2 * (pokemon_table[trade->app->current_pokemon].base_special + special_iv)) + floor(sqrt(statexp) / 4)) * level) / 100) + 5;
  442. DATA_BLOCK2.party[0].special = ((special >> 8) & 0x00FF) | ((special << 8) & 0xFF00);
  443. FURI_LOG_D(TAG, "[Trade] Pokemon Max HP: %x", DATA_BLOCK2.party[0].max_hp);
  444. FURI_LOG_D(TAG, "[Trade] Pokemon Attack: %x", DATA_BLOCK2.party[0].atk);
  445. FURI_LOG_D(TAG, "[Trade] Pokemon Defence: %x", DATA_BLOCK2.party[0].def);
  446. FURI_LOG_D(TAG, "[Trade] Pokemon Speed: %x", DATA_BLOCK2.party[0].spd);
  447. FURI_LOG_D(TAG, "[Trade] Pokemon Special: %x", DATA_BLOCK2.party[0].special);
  448. // Set the Pokemon types
  449. DATA_BLOCK2.party[0].type[0] = pokemon_table[trade->app->current_pokemon].type1;
  450. if(pokemon_table[trade->app->current_pokemon].type2 == 0xFF) {
  451. DATA_BLOCK2.party[0].type[1] = pokemon_table[trade->app->current_pokemon].type1;
  452. } else {
  453. DATA_BLOCK2.party[0].type[1] = pokemon_table[trade->app->current_pokemon].type2;
  454. }
  455. FURI_LOG_D(TAG, "[Trade] Type 1: %x", DATA_BLOCK2.party[0].type[0]);
  456. FURI_LOG_D(TAG, "[Trade] Type 2: %x", DATA_BLOCK2.party[0].type[1]);
  457. // Set the Pokemon moves
  458. DATA_BLOCK2.party[0].move[0] = trade->app->move1_hex_code;
  459. DATA_BLOCK2.party[0].move[1] = trade->app->move2_hex_code;
  460. DATA_BLOCK2.party[0].move[2] = trade->app->move3_hex_code;
  461. DATA_BLOCK2.party[0].move[3] = trade->app->move4_hex_code;
  462. FURI_LOG_D(TAG, "[Trade] Move 1 Hex Code: %x", trade->app->move1_hex_code);
  463. FURI_LOG_D(TAG, "[Trade] Move 2 Hex Code: %x", trade->app->move2_hex_code);
  464. FURI_LOG_D(TAG, "[Trade] Move 3 Hex Code: %x", trade->app->move3_hex_code);
  465. FURI_LOG_D(TAG, "[Trade] Move 4 Hex Code: %x", trade->app->move4_hex_code);
  466. // B3 (Pin6) / SO (2)
  467. furi_hal_gpio_write(&GAME_BOY_SO, false);
  468. furi_hal_gpio_init(&GAME_BOY_SO, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
  469. // B2 (Pin5) / SI (3)
  470. furi_hal_gpio_write(&GAME_BOY_SI, false);
  471. furi_hal_gpio_init(&GAME_BOY_SI, GpioModeInput, GpioPullNo, GpioSpeedVeryHigh);
  472. // // C3 (Pin7) / CLK (5)
  473. furi_hal_gpio_init(
  474. &GAME_BOY_CLK,
  475. GpioModeInterruptRise,
  476. GpioPullNo,
  477. GpioSpeedVeryHigh); // <-- This line causes the "OK" to stop functioning when exiting the application, so a reboot of the Flipper Zero is required.
  478. furi_hal_gpio_remove_int_callback(&GAME_BOY_CLK);
  479. furi_hal_gpio_add_int_callback(&GAME_BOY_CLK, input_clk_gameboy, trade);
  480. // furi_hal_gpio_disable_int_callback(&GAME_BOY_CLK);
  481. // furi_hal_gpio_remove_int_callback(&GAME_BOY_CLK);
  482. // Reset GPIO pins to default state
  483. // furi_hal_gpio_init(&GAME_BOY_CLK, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  484. }
  485. bool trade_custom_callback(uint32_t event, void* context) {
  486. UNUSED(event);
  487. furi_assert(context);
  488. Trade* trade = (Trade*)context;
  489. view_dispatcher_send_custom_event(trade->app->view_dispatcher, 0);
  490. return true;
  491. }
  492. void disconnect_pin(const GpioPin* pin) {
  493. furi_hal_gpio_init(pin, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  494. furi_hal_gpio_write(pin, true);
  495. }
  496. void trade_exit_callback(void* context) {
  497. furi_assert(context);
  498. procesing = false;
  499. furi_hal_light_set(LightGreen, 0x00);
  500. furi_hal_light_set(LightBlue, 0x00);
  501. furi_hal_light_set(LightRed, 0x00);
  502. }
  503. Trade* trade_alloc(App* app) {
  504. Trade* trade = (Trade*)malloc(sizeof(Trade));
  505. trade->app = app;
  506. trade->view = view_alloc();
  507. procesing = true;
  508. view_set_context(trade->view, trade);
  509. view_allocate_model(trade->view, ViewModelTypeLockFree, sizeof(SelectPokemonModel));
  510. view_set_draw_callback(trade->view, trade_draw_callback);
  511. view_set_input_callback(trade->view, trade_input_callback);
  512. view_set_enter_callback(trade->view, trade_enter_callback);
  513. view_set_custom_callback(trade->view, trade_custom_callback);
  514. view_set_exit_callback(trade->view, trade_exit_callback);
  515. return trade;
  516. }
  517. void trade_free(App* app) {
  518. furi_assert(app);
  519. // Free resources
  520. procesing = false;
  521. furi_hal_gpio_remove_int_callback(&GAME_BOY_CLK);
  522. disconnect_pin(&GAME_BOY_CLK);
  523. view_free(app->trade->view);
  524. free(app->trade);
  525. }
  526. View* trade_get_view(App* app) {
  527. furi_assert(app);
  528. return app->trade->view;
  529. }