MinitelPong.ino 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /**
  2. * Minitel library for Arduino (v0.1) / May 2013
  3. * http://github.com/01010101/Minitel
  4. *
  5. * By Jerome Saint-Clair aka 01010101
  6. * http://saint-clair.net
  7. *
  8. * For the Graffiti Research Lab France
  9. * http://graffitiresearchlab.fr
  10. *
  11. * Based on works by the Tetalab (Fabrice, Renaud, PG & Phil)
  12. * http://tetalab.org
  13. */
  14. #include <SoftwareSerial.h>
  15. #include <Minitel.h>
  16. Minitel m1(6,7);
  17. Minitel m2(8,9);
  18. int cursorXIni = 3;
  19. int cursorYIni = 10;
  20. int cursorX = cursorXIni;
  21. int cursorY = cursorYIni;
  22. int cursorXDir = 1;
  23. int cursorYDir = -1;
  24. void setup() {
  25. m1.textMode();
  26. m2.textMode();
  27. drawPart1();
  28. drawPart2();
  29. }
  30. int score = 0;
  31. void loop() {
  32. int cursorXPre = cursorX;
  33. int cursorYPre = cursorY;
  34. cursorX+=cursorXDir;
  35. cursorY+=cursorYDir;
  36. /*
  37. if (cursorX == 2 || cursorX == 79) {
  38. cursorXDir *= -1;
  39. }
  40. */
  41. if (cursorX == 81) {
  42. cursorX = cursorXIni;
  43. cursorY = cursorYIni;
  44. cursorYDir = 1;
  45. score++;
  46. if (score == 10) {
  47. score = 0;
  48. }
  49. cursorYDir = score % 2 == 1 ? -1 : 1;
  50. drawScore(m1, score, 33, 3, true);
  51. Serial.println(score);
  52. }
  53. if (cursorY == 2 || cursorY == 23) {
  54. cursorYDir *= -1;
  55. }
  56. if (!isOverLapping(cursorXPre, cursorYPre)) {
  57. if (cursorXPre < 40) {
  58. m1.moveCursorTo(cursorXPre, cursorYPre);
  59. m1.textColor(BLACK);
  60. m1.textByte(127, cursorXPre, cursorYPre);
  61. m1.textColor(WHITE);
  62. }
  63. else if (cursorXPre > 41) {
  64. m2.moveCursorTo(cursorXPre-40, cursorYPre);
  65. m2.textColor(BLACK);
  66. m2.textByte(127, cursorXPre-40, cursorYPre);
  67. m2.textColor(WHITE);
  68. }
  69. }
  70. if (!isOverLapping(cursorX, cursorY)) {
  71. if (cursorX < 40) {
  72. m1.moveCursorTo(cursorX, cursorY);
  73. m1.textByte(127, cursorX, cursorY);
  74. }
  75. else if (cursorX > 41) {
  76. m2.moveCursorTo(cursorX-40, cursorY);
  77. m2.textByte(127, cursorX-40, cursorY);
  78. }
  79. }
  80. }
  81. boolean isOverLapping(int x, int y) {
  82. if (x >=33 && y >= 3 && x<=35 && y<=7) {
  83. delay(100);
  84. return true;
  85. }
  86. if (x >=47 && y >= 3 && x<=49 && y<=7) {
  87. delay(100);
  88. return true;
  89. }
  90. return false;
  91. }
  92. void drawMiddleLine(Minitel m, int x) {
  93. // Middle line
  94. for (int i=1; i<24; i++) {
  95. if (i%4 != 0) {
  96. m.textByte(127, x, i);
  97. }
  98. }
  99. }
  100. void drawRaquet(Minitel m, int x, int y) {
  101. // Raquet
  102. m.textByte(127, x, y);
  103. m.textByte(127, x, y+1);
  104. m.textByte(127, x, y+2);
  105. m.textByte(127, x, y+3);
  106. }
  107. void drawTopBottomLines(Minitel m) {
  108. // Top line
  109. m.textByte(127, 1, 1);
  110. m.repeat(39);
  111. // Bottom line
  112. m.textByte(127, 1, 24);
  113. m.repeat(39);
  114. }
  115. void drawPart1() {
  116. m1.clearScreen();
  117. m1.noCursor();
  118. drawTopBottomLines(m1);
  119. drawMiddleLine(m1, 40);
  120. // Moving
  121. drawRaquet(m1, 1, 8);
  122. //textByte(127, 15, 12);
  123. // Score
  124. drawScore(m1, 0, 33, 3, false);
  125. m1.cursor();
  126. }
  127. void drawPart2() {
  128. m2.clearScreen();
  129. m2.noCursor();
  130. drawTopBottomLines(m2);
  131. drawMiddleLine(m2, 1);
  132. // Moving
  133. drawRaquet(m2, 40, 7);
  134. // Score
  135. drawScore(m2, 0, 7, 3, false);
  136. m2.noCursor();
  137. }
  138. void drawScore(Minitel m, int score, int x, int y, boolean erase) {
  139. if (erase) eraseScore(m, x, y);
  140. switch (score) {
  141. case 0 :
  142. drawScore0(m, x, y);
  143. break;
  144. case 1 :
  145. drawScore1(m, x, y);
  146. break;
  147. case 2 :
  148. drawScore2(m, x, y);
  149. break;
  150. case 3 :
  151. drawScore3(m, x, y);
  152. break;
  153. case 4 :
  154. drawScore4(m, x, y);
  155. break;
  156. case 5 :
  157. drawScore5(m, x, y);
  158. break;
  159. case 6 :
  160. drawScore6(m, x, y);
  161. break;
  162. case 7 :
  163. drawScore7(m, x, y);
  164. break;
  165. case 8 :
  166. drawScore8(m, x, y);
  167. break;
  168. case 9 :
  169. drawScore9(m, x, y);
  170. break;
  171. }
  172. }
  173. void eraseScore(Minitel m, int x, int y) {
  174. m.textColor(BLACK);
  175. m.textByte(127, x, y);
  176. m.repeat(2);
  177. m.textByte(127, x, y+1);
  178. m.textByte(127, x+2, y+1);
  179. m.textByte(127, x, y+2);
  180. m.repeat(2);
  181. m.textByte(127, x, y+3);
  182. m.textByte(127, x+2, y+3);
  183. m.textByte(127, x, y+4);
  184. m.repeat(2);
  185. m.textColor(WHITE);
  186. }
  187. void drawScore0(Minitel m, int x, int y) {
  188. // 0
  189. m.textByte(127, x, y);
  190. m.repeat(2);
  191. m.textByte(127, x, y+1);
  192. m.textByte(127, x+2, y+1);
  193. m.textByte(127, x, y+2);
  194. m.textByte(127, x+2, y+2);
  195. m.textByte(127, x, y+3);
  196. m.textByte(127, x+2, y+3);
  197. m.textByte(127, x, y+4);
  198. m.repeat(2);
  199. }
  200. void drawScore1(Minitel m, int x, int y) {
  201. // 1
  202. m.textByte(127, x+2, y);
  203. m.textByte(127, x+2, y+1);
  204. m.textByte(127, x+2, y+2);
  205. m.textByte(127, x+2, y+3);
  206. m.textByte(127, x+2, y+4);
  207. }
  208. void drawScore2(Minitel m, int x, int y) {
  209. // 2
  210. m.textByte(127, x, y);
  211. m.repeat(2);
  212. m.textByte(127, x+2, y+1);
  213. m.textByte(127, x, y+2);
  214. m.repeat(2);
  215. m.textByte(127, x, y+3);
  216. m.textByte(127, x, y+4);
  217. m.repeat(2);
  218. }
  219. void drawScore3(Minitel m, int x, int y) {
  220. // 3
  221. m.textByte(127, x, y);
  222. m.repeat(2);
  223. m.textByte(127, x+2, y+1);
  224. m.textByte(127, x+1, y+2);
  225. m.repeat(1);
  226. m.textByte(127, x+2, y+3);
  227. m.textByte(127, x, y+4);
  228. m.repeat(2);
  229. }
  230. void drawScore4(Minitel m, int x, int y) {
  231. // 4
  232. m.textByte(127, x, y);
  233. m.textByte(127, x+2, y);
  234. m.textByte(127, x, y+1);
  235. m.textByte(127, x+2, y+1);
  236. m.textByte(127, x, y+2);
  237. m.repeat(2);
  238. m.textByte(127, x+2, y+3);
  239. m.textByte(127, x+2, y+4);
  240. }
  241. void drawScore5(Minitel m, int x, int y) {
  242. // 5
  243. m.textByte(127, x, y);
  244. m.repeat(2);
  245. m.textByte(127, x, y+1);
  246. m.textByte(127, x, y+2);
  247. m.repeat(2);
  248. m.textByte(127, x+2, y+3);
  249. m.textByte(127, x, y+4);
  250. m.repeat(2);
  251. }
  252. void drawScore6(Minitel m, int x, int y) {
  253. // 6
  254. m.textByte(127, x, y);
  255. m.repeat(2);
  256. m.textByte(127, x, y+1);
  257. m.textByte(127, x, y+2);
  258. m.repeat(2);
  259. m.textByte(127, x, y+3);
  260. m.textByte(127, x+2, y+3);
  261. m.textByte(127, x, y+4);
  262. m.repeat(2);
  263. }
  264. void drawScore7(Minitel m, int x, int y) {
  265. // 7
  266. m.textByte(127, x, y);
  267. m.repeat(2);
  268. m.textByte(127, x+2, y+1);
  269. m.textByte(127, x+2, y+2);
  270. m.textByte(127, x+2, y+3);
  271. m.textByte(127, x+2, y+4);
  272. }
  273. void drawScore8(Minitel m, int x, int y) {
  274. // 8
  275. m.textByte(127, x, y);
  276. m.repeat(2);
  277. m.textByte(127, x, y+1);
  278. m.textByte(127, x+2, y+1);
  279. m.textByte(127, x, y+2);
  280. m.repeat(2);
  281. m.textByte(127, x, y+3);
  282. m.textByte(127, x+2, y+3);
  283. m.textByte(127, x, y+4);
  284. m.repeat(2);
  285. }
  286. void drawScore9(Minitel m, int x, int y) {
  287. // 9
  288. m.textByte(127, x, y);
  289. m.repeat(2);
  290. m.textByte(127, x, y+1);
  291. m.textByte(127, x+2, y+1);
  292. m.textByte(127, x, y+2);
  293. m.repeat(2);
  294. m.textByte(127, x+2, y+3);
  295. m.textByte(127, x, y+4);
  296. m.repeat(2);
  297. }