Minitel.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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 "Arduino.h"
  15. #include "SoftwareSerial.h"
  16. #include "Minitel.h"
  17. byte _currentBgColor = BLACK;
  18. byte _currentTextColor = WHITE;
  19. byte _currentMode = TEXT_MODE;
  20. byte _currentVideo = VIDEO_STANDARD;
  21. byte _currentSize = SIZE_NORMAL;
  22. boolean _currentUnderline = false;
  23. boolean _currentBlink = false;
  24. boolean _currentShowCursor = false;
  25. Minitel::Minitel() : SoftwareSerial(6,7) {
  26. pinMode(6, OUTPUT);
  27. pinMode(7, OUTPUT);
  28. init();
  29. }
  30. Minitel::Minitel(int rx, int tx) : SoftwareSerial(rx,tx) {
  31. pinMode(rx, INPUT);
  32. pinMode(tx, OUTPUT);
  33. init();
  34. }
  35. void Minitel::init() {
  36. //Serial.begin(1200);
  37. begin(1200);
  38. refreshSettings();
  39. }
  40. byte Minitel::getGraphicChar(String s) {
  41. byte carac= 32; // caractère pixel
  42. if (s.length() == 6) {
  43. carac += s[0] == '0' ? 0 : 1;
  44. carac += s[1] == '0' ? 0 : 2;
  45. carac += s[2] == '0' ? 0 : 4;
  46. carac += s[3] == '0' ? 0 : 8;
  47. carac += s[4] == '0' ? 0 : 16;
  48. carac += s[5] == '0' ? 0 : 32;
  49. return carac;
  50. }
  51. return 9;
  52. }
  53. void Minitel::serialprint7(byte b) {
  54. boolean i = false;
  55. for(int j = 0; j<8;j++) {
  56. if (bitRead(b,j)==1) {
  57. i =!i; //calcul de la parité
  58. }
  59. }
  60. if (i) {
  61. bitWrite(b,7,1); //ecriture de la partié
  62. }
  63. else {
  64. bitWrite(b,7,0); //ecriture de la partié
  65. }
  66. write(b); //ecriture du byte sur le software serial
  67. }
  68. void Minitel::graphic(String s, int x, int y){
  69. moveCursorTo(x, y);
  70. graphic(s);
  71. }
  72. void Minitel::graphic(String s) {
  73. serialprint7(getGraphicChar(s));
  74. }
  75. void Minitel::textByte(byte c) {
  76. serialprint7(c);
  77. }
  78. void Minitel::textByte(byte b, int x, int y) {
  79. moveCursorTo(x, y);
  80. textByte(b);
  81. }
  82. boolean Minitel::textChar(byte c) {
  83. byte charByte = getCharByte(c);
  84. if (isValidChar(charByte)) {
  85. serialprint7(charByte);
  86. return true;
  87. }
  88. return false;
  89. }
  90. boolean Minitel::textChar(byte c, int x, int y) {
  91. moveCursorTo(x, y);
  92. return textChar(c);
  93. }
  94. void Minitel::text(String s, int x, int y) {
  95. text(s, x, y, HORIZONTAL);
  96. }
  97. void Minitel::text(String s) {
  98. text(s, HORIZONTAL);
  99. }
  100. void Minitel::text(String s, int x, int y, int orientation) {
  101. moveCursorTo(x, y);
  102. text(s, orientation);
  103. }
  104. void Minitel::text(String s, int orientation) {
  105. for (int i=0; i<s.length(); i++) {
  106. char c = s.charAt(i);
  107. boolean indent = false;
  108. if (isAccent(c)) {
  109. i+=1; // chars with accents take 2 array indexes
  110. c = s.charAt(i);
  111. indent = printAccentChar(c);
  112. }
  113. else {
  114. indent = textChar(c);
  115. }
  116. if (indent && orientation == VERTICAL) {
  117. moveCursor(LEFT);
  118. moveCursor(DOWN);
  119. }
  120. }
  121. }
  122. // Characters
  123. /*
  124. String chars0 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // 0 -> 33
  125. String chars1= "!\"#$%&'()*+,-./0123456789:;<=>?@"; // 33 -> 64
  126. String chars2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // 65 -> 90
  127. String chars3 = "abcdefghijklmnopqrstuvwxyz"; // 97 -> 122
  128. */
  129. byte Minitel::getCharByte(char c) {
  130. String characters = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]x_xabcdefghijklmnopqrstuvwxyz";
  131. return (byte) characters.lastIndexOf(c);
  132. }
  133. void Minitel::specialChar(byte c, int x, int y) {
  134. moveCursorTo(x, y);
  135. specialChar(c);
  136. }
  137. void Minitel::specialChar(byte c) {
  138. if (c == SPE_CHAR_BOOK || c == SPE_CHAR_PARAGRAPH
  139. || c == SPE_CHAR_ARROW_LEFT || c == SPE_CHAR_ARROW_UP
  140. || c == SPE_CHAR_ARROW_RIGHT || c == SPE_CHAR_ARROW_DOWN
  141. || c == SPE_CHAR_CIRCLE || c == SPE_CHAR_MINUS_PLUS
  142. || c == SPE_CHAR_1_4 || c == SPE_CHAR_1_2
  143. || c == SPE_CHAR_3_4 || c == SPE_CHAR_UPPER_OE
  144. || c == SPE_CHAR_LOWER_OE || c == SPE_CHAR_BETA) {
  145. serialprint7(25);
  146. serialprint7(c);
  147. }
  148. }
  149. boolean Minitel::isValidChar(byte index) {
  150. if (index >= 32 && index < 123) {
  151. return true;
  152. }
  153. return false;
  154. }
  155. boolean Minitel::isAccent(char c) {
  156. String accents = "áàâäéèëêíìîïóòôöúùûü";
  157. if (accents.indexOf(c) >=0) {
  158. return true;
  159. }
  160. return false;
  161. }
  162. boolean Minitel::printAccentChar(char c) {
  163. if (isAccent(c)) {
  164. String accents = "áàâäéèëêíìîïóòôöúùûü";
  165. int index = (accents.indexOf(c)-1)/2;
  166. int accentTypeIndex = index%4;
  167. printAccent(accentTypeIndex);
  168. int letterIndex = floor(index/4);
  169. char letter = getAccentLetter(letterIndex);
  170. textChar(letter);
  171. return true;
  172. }
  173. return false;
  174. }
  175. void Minitel::printAccent(int index) {
  176. serialprint7(25);
  177. switch(index) {
  178. case(0) :
  179. serialprint7(GRAVE);
  180. break;
  181. case(1) :
  182. serialprint7(ACCUTE);
  183. break;
  184. case(2) :
  185. serialprint7(CIRCUMFLEX);
  186. break;
  187. default :
  188. serialprint7(UMLAUT);
  189. }
  190. }
  191. char Minitel::getAccentLetter(int letterIndex) {
  192. switch(letterIndex) {
  193. case(0) :
  194. return('a');
  195. break;
  196. case(1) :
  197. return('e');
  198. break;
  199. case(2) :
  200. return('i');
  201. break;
  202. case(3) :
  203. return('o');
  204. break;
  205. default :
  206. return('u');
  207. }
  208. }
  209. void Minitel::repeat(byte n) {
  210. serialprint7(18);
  211. serialprint7(64+n);
  212. }
  213. void Minitel::bgColor(byte c) {
  214. if (c >= 0 && c <=7) {
  215. serialprint7(27);
  216. serialprint7(c+80);
  217. _currentBgColor = c;
  218. }
  219. }
  220. void Minitel::textColor(byte c) {
  221. if (c >= 0 && c <=7) {
  222. serialprint7(27);
  223. serialprint7(c+64);
  224. _currentTextColor = c;
  225. }
  226. }
  227. void Minitel::useDefaultColors() {
  228. bgColor(BLACK);
  229. textColor(WHITE);
  230. }
  231. void Minitel::moveCursorTo(int x, int y) {
  232. serialprint7(31); // Code positionnement de curseur
  233. serialprint7(64+y); // coordonnées x (x+64) (x de 1 à 40)
  234. serialprint7(64+x); // coordonnées y (y+64) (y de 1 à 24)
  235. refreshSettings();
  236. }
  237. void Minitel::moveCursor(byte dir) {
  238. if (dir == LEFT || dir == RIGHT || dir == UP || dir == DOWN) {
  239. serialprint7(dir);
  240. }
  241. }
  242. void Minitel::moveCursorTo(byte location) {
  243. if (location == HOME || location == LINE_END || location == TOP_LEFT) {
  244. serialprint7(location);
  245. }
  246. else if (location == CENTER || location == TOP_RIGHT || location == BOTTOM_RIGHT || location == BOTTOM_LEFT) {
  247. if (location == CENTER) {
  248. moveCursorTo(20, 12);
  249. }
  250. else if (location == TOP_RIGHT) {
  251. moveCursorTo(40, 1);
  252. }
  253. else if (location == BOTTOM_RIGHT) {
  254. moveCursorTo(40, 24);
  255. }
  256. else if (location == BOTTOM_LEFT) {
  257. moveCursorTo(1, 24);
  258. }
  259. refreshSettings() ;
  260. }
  261. }
  262. void Minitel::moveCursor(byte dir, int n) {
  263. if (dir == LEFT || dir == RIGHT || dir == UP || dir == DOWN) {
  264. for (int i=0; i<n; i++) {
  265. serialprint7(dir);
  266. }
  267. }
  268. }
  269. void Minitel::refreshSettings() {
  270. // Common parameters
  271. mode(_currentMode);
  272. textColor(_currentTextColor);
  273. bgColor(_currentBgColor); // Only in graphic mode ?
  274. blink(_currentBlink);
  275. cursor(_currentShowCursor);
  276. // Graphic mode specific parameters
  277. if (_currentMode == GRAPHIC_MODE) {
  278. pixelate(_currentUnderline);
  279. }
  280. // Text mode specific parameters
  281. if (_currentMode == TEXT_MODE) {
  282. video(_currentVideo);
  283. charSize(_currentSize);
  284. }
  285. }
  286. void Minitel::cursor() {
  287. cursor(true);
  288. }
  289. void Minitel::noCursor() {
  290. cursor(false);
  291. }
  292. void Minitel::cursor(boolean b) {
  293. if(b) {
  294. serialprint7(CURSOR_SHOW);
  295. }
  296. else {
  297. serialprint7(CURSOR_HIDE);
  298. }
  299. _currentShowCursor = b;
  300. }
  301. void Minitel::clearScreen() {
  302. serialprint7(CLEARSCREEN);
  303. }
  304. void Minitel::mode(byte mode) {
  305. if (mode == GRAPHIC_MODE || mode == TEXT_MODE) {
  306. serialprint7(mode);
  307. _currentMode = mode;
  308. }
  309. }
  310. void Minitel::graphicMode() {
  311. mode(GRAPHIC_MODE);
  312. }
  313. void Minitel::textMode() {
  314. mode(TEXT_MODE);
  315. }
  316. void Minitel::blink() {
  317. blink(true);
  318. }
  319. void Minitel::noBlink() {
  320. blink(false);
  321. }
  322. void Minitel::blink(boolean b) {
  323. serialprint7(27);
  324. if (b) {
  325. serialprint7(BLINK_ON);
  326. }
  327. else {
  328. serialprint7(BLINK_OFF);
  329. }
  330. _currentBlink = b;
  331. }
  332. void Minitel::charSize(byte type) {
  333. if (type == SIZE_NORMAL || type == SIZE_DOUBLE_HEIGHT || type == SIZE_DOUBLE_WIDTH || type == SIZE_DOUBLE) {
  334. serialprint7(27);
  335. serialprint7(type);
  336. _currentSize = type;
  337. }
  338. }
  339. void Minitel::incrustation(boolean b) {
  340. serialprint7(27);
  341. if (b) {
  342. serialprint7(INCRUSTATION_ON);
  343. }
  344. else {
  345. serialprint7(INCRUSTATION_OFF);
  346. }
  347. }
  348. void Minitel::incrustation() {
  349. incrustation(INCRUSTATION_ON);
  350. }
  351. void Minitel::noIncrustation() {
  352. incrustation(INCRUSTATION_OFF);
  353. }
  354. void Minitel::pixelate() {
  355. pixelate(true);
  356. }
  357. void Minitel::noPixelate() {
  358. pixelate(false);
  359. }
  360. void Minitel::pixelate(boolean b) {
  361. serialprint7(27);
  362. if (b) {
  363. serialprint7(UNDERLINE_ON);
  364. }
  365. else {
  366. serialprint7(UNDERLINE_OFF);
  367. }
  368. _currentUnderline = b;
  369. }
  370. void Minitel::lineMask(boolean b) {
  371. serialprint7(27);
  372. if (b) {
  373. serialprint7(LINE_MASK_ON);
  374. }
  375. else {
  376. serialprint7(LINE_MASK_OFF);
  377. }
  378. }
  379. void Minitel::lineMask() {
  380. lineMask(LINE_MASK_ON);
  381. }
  382. void Minitel::noLineMask() {
  383. lineMask(LINE_MASK_OFF);
  384. }
  385. void Minitel::video(byte v) {
  386. if (v == VIDEO_INVERT || v == VIDEO_STANDARD || v == VIDEO_TRANSPARENT) {
  387. serialprint7(27);
  388. serialprint7(v);
  389. _currentVideo = v;
  390. }
  391. }
  392. void Minitel::standardVideo() {
  393. video(VIDEO_STANDARD);
  394. }
  395. void Minitel::invertVideo() {
  396. video(VIDEO_INVERT);
  397. }
  398. void Minitel::transparentVideo() {
  399. video(VIDEO_TRANSPARENT);
  400. }
  401. void Minitel::setMaxSpeed() {
  402. /*
  403. serialprint7(27);
  404. serialprint7(SPEED_4800);
  405. */
  406. }
  407. // Bip
  408. // Less than 200ms isn't taken into account
  409. void Minitel::bip(long duration) {
  410. long beginTime = millis();
  411. while(millis() < beginTime+100) {//duration) {
  412. serialprint7(27);
  413. serialprint7(BIP);
  414. delay(100);
  415. }
  416. }
  417. void Minitel::rect(char c, int x, int y, int w, int h) {
  418. byte b = getCharByte(c);
  419. rect(b, x, y, w, h);
  420. }
  421. void Minitel::rect(byte c, int x, int y, int w, int h) {
  422. moveCursorTo(x, y);
  423. textByte(c);
  424. repeat(w);
  425. moveCursorTo(x, y+1);
  426. for (int i=0; i<h-2; i++) {
  427. textByte(c);
  428. moveCursor(DOWN);
  429. moveCursor(LEFT);
  430. }
  431. moveCursorTo(x+w, y+1);
  432. for (int i=0; i<h-2; i++) {
  433. textByte(c);
  434. moveCursor(DOWN);
  435. moveCursor(LEFT);
  436. }
  437. moveCursorTo(x, y+h-1);
  438. textByte(c);
  439. repeat(w);
  440. }
  441. void Minitel::spiral(int x, int y, int siz, int c) {
  442. int curSiz = 1;
  443. // Center
  444. specialChar(x, y, c);
  445. x++;
  446. // Spiral
  447. for (int i=0; i< siz; i++) {
  448. for (int j=0; j<curSiz; j++) {
  449. specialChar(x, y, c);
  450. y++;
  451. }
  452. curSiz++;
  453. for (int j=0; j<curSiz; j++) {
  454. specialChar(x, y, c);
  455. x--;
  456. }
  457. for (int j=0; j<curSiz; j++) {
  458. specialChar(x, y, c);
  459. y--;
  460. }
  461. curSiz++;
  462. for (int j=0; j<curSiz; j++) {
  463. specialChar(x, y, c);
  464. x++;
  465. }
  466. }
  467. }