You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.c 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. #include "fxlib.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "MonochromeLib.h"
  5. #include "fonts.h"
  6. #include "serial.h"
  7. #define SCROLL 8 //number of pixels to scroll at each press of the up/down key
  8. unsigned long posY[2] = {0}; //array holding the scroll for each view, so that if the user comes back from a comment page, he keeps his scroll position on the post page
  9. unsigned int key;
  10. unsigned int postHeights[26] = {0};
  11. //char postComments[26];
  12. char currentView = 0; //0=post, 1=comment
  13. unsigned char strReceived[5010];
  14. char subreddit[3];
  15. unsigned char byteReceived;
  16. unsigned char byteSent;
  17. char stringFinished = 1;
  18. //postComments[0] = strCmt;
  19. //postComments[1] = strCmt2;
  20. //postComments[2] = strCmt3;
  21. void dispPost(unsigned char* str, int strlen);
  22. void getPost(unsigned long posY);
  23. void dispCmt(unsigned char* str, int strlen);
  24. void sendSerial(unsigned char* code);
  25. char getSerial();
  26. void chooseSub();
  27. char dispIntroScreen();
  28. int AddIn_main(int isAppli, unsigned short OptionNum) {
  29. unsigned char serialSettings[]={0,5,0,0,0,0};
  30. //unsigned char test[] = "TestTransmission\n";
  31. Serial_Open(serialSettings);
  32. if (dispIntroScreen())
  33. return 1;
  34. chooseSub();
  35. ML_clear_vram();
  36. //GetKey(&key);
  37. //dispStr(test, normfont, 1, 1, sizeof(test));
  38. while(1) {
  39. ML_clear_vram();
  40. //displays the current view
  41. if (currentView == 0)
  42. dispPost(strReceived, sizeof(strReceived));
  43. else
  44. dispCmt(strReceived, 5010);
  45. //getPost(posY[0]);
  46. ML_pixel(127,31,1); //debug pixel, used to test the "hitbox" of posts
  47. GetKey(&key);
  48. if (key == KEY_CTRL_DOWN || key == KEY_CTRL_UP) {
  49. if (key == KEY_CTRL_DOWN)
  50. posY[currentView] += SCROLL;
  51. else if (posY[currentView] >= SCROLL)
  52. posY[currentView] -= SCROLL;
  53. }
  54. if (key == KEY_CTRL_SHIFT && posY[currentView] >= 64) {
  55. posY[currentView] -= 64;
  56. }
  57. if (key == KEY_CTRL_ALPHA) {
  58. posY[currentView] += 64;
  59. }
  60. if (key == KEY_CTRL_EXE && currentView == 0) {
  61. getPost(posY[0]);
  62. posY[1] = 0;
  63. }
  64. if (key == KEY_CTRL_EXIT && currentView == 1) {
  65. currentView = 0;
  66. sendSerial(subreddit);
  67. getSerial();
  68. }
  69. if (key == KEY_CTRL_VARS && !stringFinished) {
  70. sendSerial("np");
  71. getSerial();
  72. posY[currentView] = 0;
  73. }
  74. if (key == KEY_CTRL_OPTN) {
  75. chooseSub();
  76. }
  77. }
  78. return 1;
  79. }
  80. char dispIntroScreen() {
  81. unsigned char nomAppli[10] = "Caddit";
  82. unsigned char author[15] = "Par Zezombye";
  83. unsigned char pressKey[75] = "Appuyez sur une touche pour commencer la connexion.";
  84. unsigned char credits[100] = "Merci \x91 Leph\x89nixnoir, Dark Storm\n& la communaut\x89 de Plan\x90te Casio!";
  85. unsigned char str[100] = "Connexion en cours...\n\nAppuyez sur [DEL] pour annuler.";
  86. int length = 0;
  87. int l;
  88. int k = 9;
  89. ML_horizontal_line(0, 0, 127, 1);
  90. ML_horizontal_line(17, 0, 127, 1);
  91. for (l = 0; l < k; l++)
  92. length += normfont.length[nomAppli[l]-32] +1;
  93. dispStr(nomAppli, 65-(length/2), 2);
  94. length = 0;
  95. k = 14;
  96. for (l = 0; l < k; l++)
  97. length += normfont.length[author[l]-32] +1;
  98. dispStr(author, 65-(length/2), 9);
  99. dispStr(pressKey, 0, 24);
  100. dispStr(credits, 0, 49);
  101. GetKey(&key);
  102. ML_clear_vram();
  103. dispStr(str, 0, 0);
  104. ML_display_vram();
  105. sendSerial("hi");
  106. if (getSerial())
  107. return 1;
  108. return 0;
  109. }
  110. void chooseSub() {
  111. char str[200] = "Choisissez un subreddit\n\n1: /r/talesfromtechsupport\n2: /r/talesfromretail\n3: /r/askreddit\n4: /r/nosleep\n5: /r/caddit";
  112. char sub;
  113. ML_clear_vram();
  114. dispStr(str, 1, 1);
  115. do {
  116. GetKey(&key);
  117. } while (!(key >= KEY_CHAR_1 && key <= KEY_CHAR_5));
  118. if (key == KEY_CHAR_1)
  119. sub = 't';
  120. else if (key == KEY_CHAR_2)
  121. sub = 'r';
  122. else if (key == KEY_CHAR_3)
  123. sub = 'a';
  124. else if (key == KEY_CHAR_4)
  125. sub = 'n';
  126. else if (key == KEY_CHAR_5)
  127. sub = 'c';
  128. subreddit[0] = 's';
  129. subreddit[1] = sub;
  130. sendSerial(subreddit);
  131. currentView = 0;
  132. getSerial();
  133. }
  134. void sendSerial(unsigned char* code) {
  135. Serial_WriteByte(code[0]);
  136. Serial_WriteByte(code[1]);
  137. Serial_WriteByte(';');
  138. Serial_WriteByte('\n');
  139. }
  140. char getSerial() {
  141. int i = 0;
  142. memset(strReceived, 0, sizeof(strReceived));
  143. Serial_ClearReceiveBuffer();
  144. byteReceived = '\0';
  145. while (1) {
  146. while(Serial_ReadByte(&byteReceived)==0 && byteReceived != '"' && byteReceived != '\0') {
  147. strReceived[i] = byteReceived;
  148. i++;
  149. }
  150. if (byteReceived == '"') {
  151. if (strReceived[i-1] == ',') {
  152. sendSerial("ak");
  153. i--;
  154. strReceived[i] = '\0';
  155. byteReceived = '\0';
  156. Serial_ClearReceiveBuffer();
  157. } else {
  158. if (strReceived[i-1] == ';')
  159. stringFinished = 0;
  160. else
  161. stringFinished = 1;
  162. break;
  163. }
  164. }
  165. if (IsKeyDown(KEY_CTRL_DEL))
  166. return 1;
  167. }
  168. return 0;
  169. //dispStr(strReceived, normfont, 1, 1, i);
  170. //GetKey(&key);
  171. //dispStr(strReceived, normfont, 10, 10, i);
  172. }
  173. void getPost(unsigned long posY) {
  174. int height = 12;
  175. int rankOfPost = 0;
  176. while (1) {
  177. height += postHeights[rankOfPost]+3;
  178. if (height > posY+33) {
  179. unsigned char str[3] = "p ";
  180. str[1] = rankOfPost+65;
  181. ML_clear_vram();
  182. currentView = 1;
  183. sendSerial(str);
  184. getSerial();
  185. break;
  186. }
  187. if (rankOfPost > 25)
  188. break;
  189. rankOfPost++;
  190. }
  191. }
  192. void dispCmt(unsigned char* str, int strlen) {
  193. int i;
  194. int currentPageHeight = 0;
  195. for (i = 0; i < strlen; i++) {
  196. if (str[i] == '<') {
  197. //char test[2] = {0, 0};
  198. //test[0] = str[i+1];
  199. //dispStr("Found a <", normfont, 1, 1, sizeof("Found a <"));
  200. //dispStr(test, normfont, 1, 10, 1);
  201. //GetKey(&key);
  202. if (str[i+1] == 't') { //it is the post
  203. int j = i+3;
  204. int k = 0;
  205. unsigned char post[10000] = {0};
  206. //dispStr("Found text post", normfont, 1, 10, sizeof("Found text post"));
  207. //GetKey(&key);
  208. while (str[j] != '>') { //stocks the text of the post
  209. post[k] = str[j];
  210. k++;
  211. j++;
  212. }
  213. currentPageHeight += dispStr(post, 0, currentPageHeight-posY[1]); //increases the page height and displays it
  214. //ML_horizontal_line(currentPageHeight-posY[1], 0, 127, 1); //draws a line after the post
  215. //currentPageHeight += 1;
  216. i = j;
  217. }
  218. if (str[i+1] >= '1' && str[i+1] <= '9') { //it is a comment
  219. int j = i+3;
  220. int k = 0;
  221. unsigned char comment[2000] = {0};
  222. int heightOfComment = 0;
  223. //dispStr("Found a cmt", normfont, 1, 1, sizeof("Found a cmt"));
  224. //GetKey(&key);
  225. while (str[j] != '>') {
  226. comment[k] = str[j];
  227. k++;
  228. j++;
  229. }
  230. if (str[i+1] == '1') {
  231. ML_horizontal_line(currentPageHeight-posY[1], 0, 127, 1);
  232. //heightOfComment = 2;
  233. currentPageHeight += 3;
  234. }
  235. heightOfComment += dispStr(comment, 2*(str[i+1]-49), currentPageHeight-posY[1]);
  236. for (k = 0; k < 2*(str[i+1]-49); k+=2) {
  237. ML_vertical_line(k, currentPageHeight-2-posY[1], currentPageHeight+heightOfComment-posY[1]-2, 1);
  238. }
  239. //if (k > 2) ML_vertical_line(k, currentPageHeight-posY[1], currentPageHeight+heightOfComment+4-posY[1], 1);
  240. currentPageHeight += heightOfComment;
  241. i = j;
  242. }
  243. }
  244. }
  245. }
  246. //interprets the html and the tags
  247. void dispPost(unsigned char* str, int strlen) {
  248. int i;
  249. int currentPostRank = 0;
  250. int currentPageHeight = 13;
  251. ML_horizontal_line(0-posY[0], 0, 127, 1);
  252. ML_horizontal_line(10-posY[0], 0, 127, 1);
  253. for (i = 0; i < strlen; i++) {
  254. if (str[i] == '<') {
  255. if (str[i+1] == 'p') { //it is a post
  256. int j = i+2;
  257. int heightOfPost = 0;
  258. unsigned char title[512] = {0}; //because f* dynamic allocation, max title size is 300 characters
  259. int k = 0;
  260. int strlen = sizeof(title);
  261. while (str[j] != '>') { //as long as the post tag doesn't end
  262. /*while (str[j] == ' ') //loops through the next attribute
  263. j++;
  264. if (str[j] == 't') { //title attribute*/
  265. //while (str[j] != '"') {
  266. //j += 2;
  267. title[k] = str[j];
  268. k++;
  269. j++;
  270. //}
  271. /*}
  272. if (str[j] == 'a') {
  273. unsigned char attributes[64] = {0};
  274. int k = 0;
  275. int strlen = sizeof(attributes);
  276. int test;
  277. j+=2;
  278. while (str[j] != '"') {
  279. attributes[k] = str[j];
  280. k++;
  281. j++;
  282. }
  283. test = dispStr(attributes, normfont, 0, (currentPageHeight+heightOfPost)-posY[0], k);
  284. heightOfPost += test;
  285. j++;
  286. }
  287. if (str[j] != ' ' && str[j] != 't' && str[j] != 'a' && str[j] != 's') {
  288. break;
  289. }*/
  290. }
  291. heightOfPost += dispStr(title, 0, currentPageHeight-posY[0]);
  292. j++;
  293. postHeights[currentPostRank] = heightOfPost;
  294. ML_horizontal_line(currentPageHeight+postHeights[currentPostRank]-1-posY[0], 0, 127, 1);
  295. currentPageHeight += postHeights[currentPostRank]+3;
  296. currentPostRank++;
  297. i = j;
  298. }
  299. if (str[i+1] == 'r') {
  300. int j = i+3;
  301. int k = 0;
  302. int l;
  303. int subLength = 0;
  304. unsigned char subreddit[64] = {0};
  305. while (str[j] != '>') {
  306. subreddit[k] = str[j];
  307. k++;
  308. j++;
  309. }
  310. for (l = 0; l < k; l++)
  311. subLength += normfont.length[subreddit[l]-32] +1;
  312. dispStr(subreddit, 65-(subLength/2), 3-posY[0]);
  313. }
  314. }
  315. }
  316. }
  317. //****************************************************************************
  318. //************** ****************
  319. //************** Notice! ****************
  320. //************** ****************
  321. //************** Please do not change the following source. ****************
  322. //************** ****************
  323. //****************************************************************************
  324. #pragma section _BR_Size
  325. unsigned long BR_Size;
  326. #pragma section
  327. #pragma section _TOP
  328. //****************************************************************************
  329. // InitializeSystem
  330. //
  331. // param : isAppli : 1 = Application / 0 = eActivity
  332. // OptionNum : Option Number (only eActivity)
  333. //
  334. // retval : 1 = No error / 0 = Error
  335. //
  336. //****************************************************************************
  337. int InitializeSystem(int isAppli, unsigned short OptionNum)
  338. {
  339. return INIT_ADDIN_APPLICATION(isAppli, OptionNum);
  340. }
  341. #pragma section