Advertisement
Guest User

Untitled

a guest
May 28th, 2016
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.59 KB | None | 0 0
  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.  
  8. #define SCROLL 8 //number of pixels to scroll at each press of the up/down key
  9.  
  10. 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
  11. unsigned int key;
  12. unsigned int postHeights[26] = {0};
  13. //char postComments[26];
  14. char currentView = 0; //0=post, 1=comment
  15.  
  16. unsigned char strReceived[5010];
  17.  
  18. unsigned char byteReceived;
  19. unsigned char byteSent;
  20.  
  21.  
  22. //const unsigned char str[] = "<r /r/SandersForPresident> <p t=Here are some dank memes:\nLook of disapproval: \x82\_\x82\nLenny face: (\x83\x84\x83\)\nSunglasses: (\x86\x85\_\x85\)\nShrug: \x87\\\_(\x88\)_/\x87\" a=(ext.link) r/sandersforpresident u/Zezombye 45 upvotes\"> <p t=This is the title of the second post! Gotta make it long though so that I have to scroll.\" a=(self) r/askreddit u/lephenixnoir\"> <p t=the quick brown fox jumps over the lazy dog\nTHE QUICK BROWN FOX JUMPS OVER THE LAZY DOG!!! (why are we yelling?)\" a=goddammit test\"> <p t=Fourth post\" a=.\"";
  23.  
  24. //postComments[0] = strCmt;
  25. //postComments[1] = strCmt2;
  26. //postComments[2] = strCmt3;
  27.  
  28. void dispPost(unsigned char* str, int strlen);
  29. void getPost(unsigned long posY);
  30. void dispCmt(unsigned char* str, int strlen);
  31. void sendSerial(unsigned char* code, int length);
  32. void getSerial();
  33.  
  34. int AddIn_main(int isAppli, unsigned short OptionNum)
  35.  
  36. {
  37. unsigned char serialSettings[]={0,5,0,0,0,0};
  38. unsigned char test[] = "TestTransmission\n";
  39. Serial_Open(serialSettings);
  40. ML_clear_vram();
  41. sendSerial(test, sizeof(test));
  42. dispStr(test, normfont, 1, 1, sizeof(test));
  43. GetKey(&key);
  44. getSerial();
  45. while(1) {
  46. ML_clear_vram();
  47.  
  48. //displays the current view
  49. if (currentView == 0)
  50. dispPost(strReceived, sizeof(strReceived));
  51. else
  52. dispCmt(strReceived, sizeof(strReceived));
  53. //dispCmt(strCmt, sizeof(strCmt)); //put this line once demo is finished
  54. //getPost(posY[0]);
  55.  
  56. ML_pixel(127,31,1); //debug pixel, used to test the "hitbox" of posts
  57. GetKey(&key);
  58. if (key == KEY_CTRL_DOWN || key == KEY_CTRL_UP) {
  59. if (key == KEY_CTRL_DOWN)
  60. posY[currentView] += SCROLL;
  61. else if (posY[currentView] >= SCROLL)
  62. posY[currentView] -= SCROLL;
  63. }
  64. if (key == KEY_CTRL_EXE && currentView == 0) {
  65. getPost(posY[0]);
  66. posY[1] = 0;
  67. }
  68. if (key == KEY_CTRL_EXIT && currentView == 1) {
  69. currentView = 0;
  70. }
  71. }
  72.  
  73. return 1;
  74. }
  75.  
  76. void sendSerial(unsigned char* code, int length) {
  77. int i;
  78. for (i = 0; i < length-1; i++) {
  79. Serial_WriteByte(code[i]);
  80. }
  81. Serial_WriteByte('\n');
  82. //Serial_WriteBytes(code, length);
  83. }
  84.  
  85. void getSerial() {
  86. int i = 0;
  87. memset(strReceived, 0, sizeof(strReceived));
  88. Serial_ClearReceiveBuffer();
  89. byteReceived = '\0';
  90. //locate(1,1); Print((unsigned char*)"Testttt");
  91. while (1) {
  92. while(Serial_ReadByte(&byteReceived)==0 && byteReceived != '"' && byteReceived != '\0') {
  93. strReceived[i] = byteReceived;
  94. i++;
  95. }
  96. if (byteReceived == '"') {
  97. //char* code = "bbbbi";
  98. //sendSerial(code, 5);
  99. break;
  100. }
  101. }
  102. //dispStr(strReceived, normfont, 10, 10, i);
  103. }
  104.  
  105. void getPost(unsigned long posY) {
  106. int height = 12;
  107. int rankOfPost = 0;
  108. while (1) {
  109. height += postHeights[rankOfPost]+3;
  110.  
  111. if (height > posY+33) {
  112. unsigned char str[5] = "&p ;";
  113. str[2] = rankOfPost+48;
  114. ML_clear_vram();
  115. currentView = 1;
  116.  
  117. sendSerial(str, 8);
  118. getSerial();
  119. break;
  120. }
  121. if (rankOfPost > 25)
  122. break;
  123.  
  124. rankOfPost++;
  125. }
  126. }
  127.  
  128. void dispCmt(unsigned char* str, int strlen) {
  129. int i;
  130. int currentPageHeight = 0;
  131.  
  132. for (i = 0; i < strlen; i++) {
  133. if (str[i] == '<') {
  134. if (str[i+1] == 't') { //it is the post
  135. int j = i+3;
  136. int k = 0;
  137. unsigned char post[30100] = {0};
  138.  
  139. while (str[j] != '>') { //stocks the text of the post
  140. post[k] = str[j];
  141. k++;
  142. j++;
  143. }
  144.  
  145. currentPageHeight += dispStr(post, normfont, 0, currentPageHeight-posY[1], k); //increases the page height and displays it
  146.  
  147. //ML_horizontal_line(currentPageHeight-posY[1], 0, 127, 1); //draws a line after the post
  148.  
  149. //currentPageHeight += 1;
  150. i = j;
  151. }
  152. if (str[i+1] >= '1' && str[i+1] <= '9') { //it is a comment
  153. int j = i+3;
  154. int k = 0;
  155. unsigned char comment[2000] = {0};
  156. int heightOfComment = 0;
  157. while (str[j] != '>') {
  158. comment[k] = str[j];
  159. k++;
  160. j++;
  161. }
  162.  
  163. if (str[i+1] == '1') {
  164. ML_horizontal_line(currentPageHeight-posY[1], 0, 127, 1);
  165. //heightOfComment = 2;
  166. currentPageHeight += 3;
  167. }
  168.  
  169. heightOfComment += dispStr(comment, normfont, 2*(str[i+1]-49), currentPageHeight-posY[1], k);
  170. for (k = 0; k < 2*(str[i+1]-49); k+=2) {
  171. ML_vertical_line(k, currentPageHeight-2-posY[1], currentPageHeight+heightOfComment-posY[1]-2, 1);
  172. }
  173. //if (k > 2) ML_vertical_line(k, currentPageHeight-posY[1], currentPageHeight+heightOfComment+4-posY[1], 1);
  174. currentPageHeight += heightOfComment;
  175. i = j;
  176. }
  177. }
  178. }
  179. }
  180.  
  181. //interprets the html and the tags
  182. void dispPost(unsigned char* str, int strlen) {
  183. int i;
  184. int currentPostRank = 0;
  185. int currentPageHeight = 13;
  186. ML_horizontal_line(0-posY[0], 0, 127, 1);
  187. ML_horizontal_line(10-posY[0], 0, 127, 1);
  188.  
  189. for (i = 0; i < strlen; i++) {
  190. if (str[i] == '<') {
  191. if (str[i+1] == 'p') { //it is a post
  192. int j = i+2;
  193. int heightOfPost = 0;
  194. unsigned char title[512] = {0}; //because f* dynamic allocation, max title size is 300 characters
  195. int k = 0;
  196. int strlen = sizeof(title);
  197. while (str[j] != '>') { //as long as the post tag doesn't end
  198. /*while (str[j] == ' ') //loops through the next attribute
  199. j++;
  200.  
  201. if (str[j] == 't') { //title attribute*/
  202.  
  203.  
  204. //while (str[j] != '"') {
  205. //j += 2;
  206. title[k] = str[j];
  207. k++;
  208. j++;
  209. //}
  210.  
  211. /*}
  212. if (str[j] == 'a') {
  213. unsigned char attributes[64] = {0};
  214. int k = 0;
  215. int strlen = sizeof(attributes);
  216. int test;
  217. j+=2;
  218. while (str[j] != '"') {
  219. attributes[k] = str[j];
  220. k++;
  221. j++;
  222. }
  223. test = dispStr(attributes, normfont, 0, (currentPageHeight+heightOfPost)-posY[0], k);
  224. heightOfPost += test;
  225. j++;
  226. }
  227. if (str[j] != ' ' && str[j] != 't' && str[j] != 'a' && str[j] != 's') {
  228. break;
  229. }*/
  230. }
  231.  
  232. heightOfPost += dispStr(title, normfont, 0, currentPageHeight-posY[0], k);
  233. j++;
  234.  
  235. postHeights[currentPostRank] = heightOfPost;
  236.  
  237. ML_horizontal_line(currentPageHeight+postHeights[currentPostRank]-1-posY[0], 0, 127, 1);
  238.  
  239. currentPageHeight += postHeights[currentPostRank]+3;
  240. currentPostRank++;
  241. i = j;
  242. }
  243. if (str[i+1] == 'r') {
  244. int j = i+3;
  245. int k = 0;
  246. int l;
  247. int subLength = 0;
  248. unsigned char subreddit[64] = {0};
  249. while (str[j] != '>') {
  250. subreddit[k] = str[j];
  251. k++;
  252. j++;
  253. }
  254. for (l = 0; l < k; l++)
  255. subLength += normfont.length[subreddit[l]-32] +1;
  256.  
  257. dispStr(subreddit, normfont, 65-(subLength/2), 3-posY[0], k);
  258. }
  259. }
  260. }
  261. }
  262.  
  263. //displays a given string, using a given font, at the given coordinates
  264. //returns the height of the string
  265. int dispStr(unsigned char* str, struct Font font, int x2, int y, int strlen) {
  266. int k;
  267. int x = x2;
  268. int y2 = y;
  269. for (k=0; k < strlen; k++) { //browses through the given string
  270.  
  271. //word wrap: if the current character isn't a space, simply display it
  272. if (str[k] != 32 && str[k] != '\0' && str[k] != '\n') {
  273. //if (option = 1) {
  274. long j = 1 << (6*font.length[str[k]-32])-1; //initializes a long for bit checking. The long is equal to 0b10000.. with number of zeroes being the maximum length of the character, minus 1 because there's already a 1.
  275. char i;
  276.  
  277. for (i = 0; i < 6*font.length[str[k]-32]; i++) { //browses through the pixels of the character specified, shifting the 1 of j to the right each time, so that it makes 0b01000.., 0b001000... etc
  278.  
  279. if (font.ltr[str[k]-32] & (j >> i)) { //checks if the bit that is a 1 in the j is also a 1 in the character
  280.  
  281. ML_pixel(x+i%(font.length[str[k]-32]), y+i/font.length[str[k]-32], 1); //if so, locates the pixel at the coordinates, using modulo and division to calculate the coordinates relative to the top left of the character
  282. }
  283. }
  284. //}
  285.  
  286. x += font.length[str[k]-32] + 1; //now that the character has been fully displayed, shifts the cursor right by the length of character + 1
  287. } else if (str[k] == '\n') {
  288. y += 7;
  289. x = x2;
  290. } else if (str[k] == ' ') { //the current character is a space, so see if it manages to display the word without going over x=128
  291.  
  292. int i = x+4; //initializes an int to count the number of total pixels the next word takes
  293. int j = k+1; //initializes the char to the current char+1 (which is always another character)
  294. while (str[j] != 32 && str[j] != '\0') { //as long as it doesn't encounter another space or end of string
  295. i += font.length[str[j]-32]+1; //it increments i by the length of the character + 1
  296. j++;
  297. }
  298.  
  299. if (i > 128) { //the word can't be displayed, note that it is STRICTLY superior because we added an unnecessary pixel at the end
  300. y += 7; //goes on next line which is 8 pixels down
  301. x = x2; //puts cursor on beginning of line
  302. } else {
  303. x += 4;
  304. }
  305. }
  306. }
  307. return y+8-y2;
  308. }
  309. //****************************************************************************
  310. //************** ****************
  311. //************** Notice! ****************
  312. //************** ****************
  313. //************** Please do not change the following source. ****************
  314. //************** ****************
  315. //****************************************************************************
  316.  
  317.  
  318. #pragma section _BR_Size
  319. unsigned long BR_Size;
  320. #pragma section
  321.  
  322.  
  323. #pragma section _TOP
  324.  
  325. //****************************************************************************
  326. // InitializeSystem
  327. //
  328. // param : isAppli : 1 = Application / 0 = eActivity
  329. // OptionNum : Option Number (only eActivity)
  330. //
  331. // retval : 1 = No error / 0 = Error
  332. //
  333. //****************************************************************************
  334. int InitializeSystem(int isAppli, unsigned short OptionNum)
  335. {
  336. return INIT_ADDIN_APPLICATION(isAppli, OptionNum);
  337. }
  338. #pragma section
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement