Minitel.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  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. init();
  27. }
  28. Minitel::Minitel(int rx, int tx) : SoftwareSerial(rx,tx) {
  29. init();
  30. }
  31. void Minitel::init() {
  32. Serial.begin(1200);
  33. begin(1200);
  34. refreshSettings();
  35. }
  36. byte Minitel::getGraphicChar(String s) {
  37. byte carac= 32; // caractère pixel
  38. if (s.length() == 6) {
  39. carac += s[0] == '0' ? 0 : 1;
  40. carac += s[1] == '0' ? 0 : 2;
  41. carac += s[2] == '0' ? 0 : 4;
  42. carac += s[3] == '0' ? 0 : 8;
  43. carac += s[4] == '0' ? 0 : 16;
  44. carac += s[5] == '0' ? 0 : 32;
  45. return carac;
  46. }
  47. return 9;
  48. }
  49. void Minitel::serialprint7(byte b) {
  50. boolean i = false;
  51. for(int j = 0; j<8;j++) {
  52. if (bitRead(b,j)==1) {
  53. i =!i; //calcul de la parité
  54. }
  55. }
  56. if (i) {
  57. bitWrite(b,7,1); //ecriture de la partié
  58. }
  59. else {
  60. bitWrite(b,7,0); //ecriture de la partié
  61. }
  62. write(b); //ecriture du byte sur le software serial
  63. }
  64. void Minitel::graphic(String s, int x, int y){
  65. moveCursorTo(x, y);
  66. graphic(s);
  67. }
  68. void Minitel::graphic(String s) {
  69. serialprint7(getGraphicChar(s));
  70. }
  71. void Minitel::textByte(byte c) {
  72. serialprint7(c);
  73. }
  74. void Minitel::textByte(byte b, int x, int y) {
  75. moveCursorTo(x, y);
  76. textByte(b);
  77. }
  78. boolean Minitel::textChar(byte c) {
  79. byte charByte = getCharByte(c);
  80. if (isValidChar(charByte)) {
  81. serialprint7(charByte);
  82. return true;
  83. }
  84. return false;
  85. }
  86. boolean Minitel::textChar(byte c, int x, int y) {
  87. moveCursorTo(x, y);
  88. return textChar(c);
  89. }
  90. void Minitel::text(String s, int x, int y) {
  91. text(s, x, y, HORIZONTAL);
  92. }
  93. void Minitel::text(String s) {
  94. text(s, HORIZONTAL);
  95. }
  96. void Minitel::text(String s, int x, int y, int orientation) {
  97. moveCursorTo(x, y);
  98. text(s, orientation);
  99. }
  100. void Minitel::text(String s, int orientation) {
  101. for (int i=0; i<s.length(); i++) {
  102. char c = s.charAt(i);
  103. boolean indent = false;
  104. if (isAccent(c)) {
  105. i+=1; // chars with accents take 2 array indexes
  106. c = s.charAt(i);
  107. indent = printAccentChar(c);
  108. }
  109. else {
  110. indent = textChar(c);
  111. }
  112. if (indent && orientation == VERTICAL) {
  113. moveCursor(LEFT);
  114. moveCursor(DOWN);
  115. }
  116. }
  117. }
  118. // Characters
  119. /*
  120. String chars0 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // 0 -> 33
  121. String chars1= "!\"#$%&'()*+,-./0123456789:;<=>?@"; // 33 -> 64
  122. String chars2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // 65 -> 90
  123. String chars3 = "abcdefghijklmnopqrstuvwxyz"; // 97 -> 122
  124. */
  125. byte Minitel::getCharByte(char c) {
  126. String characters = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]x_xabcdefghijklmnopqrstuvwxyz";
  127. return (byte) characters.lastIndexOf(c);
  128. }
  129. void Minitel::specialChar(byte c, int x, int y) {
  130. moveCursorTo(x, y);
  131. specialChar(c);
  132. }
  133. void Minitel::specialChar(byte c) {
  134. if (c == SPE_CHAR_BOOK || c == SPE_CHAR_PARAGRAPH
  135. || c == SPE_CHAR_ARROW_LEFT || c == SPE_CHAR_ARROW_UP
  136. || c == SPE_CHAR_ARROW_RIGHT || c == SPE_CHAR_ARROW_DOWN
  137. || c == SPE_CHAR_CIRCLE || c == SPE_CHAR_MINUS_PLUS
  138. || c == SPE_CHAR_1_4 || c == SPE_CHAR_1_2
  139. || c == SPE_CHAR_3_4 || c == SPE_CHAR_UPPER_OE
  140. || c == SPE_CHAR_LOWER_OE || c == SPE_CHAR_BETA) {
  141. serialprint7(25);
  142. serialprint7(c);
  143. }
  144. }
  145. boolean Minitel::isValidChar(byte index) {
  146. if (index >= 32 && index < 123) {
  147. return true;
  148. }
  149. return false;
  150. }
  151. boolean Minitel::isAccent(char c) {
  152. String accents = "áàâäéèëêíìîïóòôöúùûü";
  153. if (accents.indexOf(c) >=0) {
  154. return true;
  155. }
  156. return false;
  157. }
  158. boolean Minitel::printAccentChar(char c) {
  159. if (isAccent(c)) {
  160. String accents = "áàâäéèëêíìîïóòôöúùûü";
  161. int index = (accents.indexOf(c)-1)/2;
  162. int accentTypeIndex = index%4;
  163. printAccent(accentTypeIndex);
  164. int letterIndex = floor(index/4);
  165. char letter = getAccentLetter(letterIndex);
  166. textChar(letter);
  167. return true;
  168. }
  169. return false;
  170. }
  171. void Minitel::printAccent(int index) {
  172. serialprint7(25);
  173. switch(index) {
  174. case(0) :
  175. serialprint7(GRAVE);
  176. break;
  177. case(1) :
  178. serialprint7(ACCUTE);
  179. break;
  180. case(2) :
  181. serialprint7(CIRCUMFLEX);
  182. break;
  183. default :
  184. serialprint7(UMLAUT);
  185. }
  186. }
  187. char Minitel::getAccentLetter(int letterIndex) {
  188. switch(letterIndex) {
  189. case(0) :
  190. return('a');
  191. break;
  192. case(1) :
  193. return('e');
  194. break;
  195. case(2) :
  196. return('i');
  197. break;
  198. case(3) :
  199. return('o');
  200. break;
  201. default :
  202. return('u');
  203. }
  204. }
  205. void Minitel::repeat(byte n) {
  206. serialprint7(18);
  207. serialprint7(64+n);
  208. }
  209. void Minitel::bgColor(byte c) {
  210. if (c >= 0 && c <=7) {
  211. serialprint7(27);
  212. serialprint7(c+80);
  213. _currentBgColor = c;
  214. }
  215. }
  216. void Minitel::textColor(byte c) {
  217. if (c >= 0 && c <=7) {
  218. serialprint7(27);
  219. serialprint7(c+64);
  220. _currentTextColor = c;
  221. }
  222. }
  223. void Minitel::useDefaultColors() {
  224. bgColor(BLACK);
  225. textColor(WHITE);
  226. }
  227. void Minitel::moveCursorTo(int x, int y) {
  228. serialprint7(31); // Code positionnement de curseur
  229. serialprint7(64+y); // coordonnées x (x+64) (x de 1 à 40)
  230. serialprint7(64+x); // coordonnées y (y+64) (y de 1 à 24)
  231. refreshSettings();
  232. }
  233. void Minitel::moveCursor(byte dir) {
  234. if (dir == LEFT || dir == RIGHT || dir == UP || dir == DOWN) {
  235. serialprint7(dir);
  236. }
  237. }
  238. void Minitel::moveCursorTo(byte location) {
  239. if (location == HOME || location == LINE_END || location == TOP_LEFT) {
  240. serialprint7(location);
  241. }
  242. else if (location == CENTER || location == TOP_RIGHT || location == BOTTOM_RIGHT || location == BOTTOM_LEFT) {
  243. if (location == CENTER) {
  244. moveCursorTo(20, 12);
  245. }
  246. else if (location == TOP_RIGHT) {
  247. moveCursorTo(40, 1);
  248. }
  249. else if (location == BOTTOM_RIGHT) {
  250. moveCursorTo(40, 24);
  251. }
  252. else if (location == BOTTOM_LEFT) {
  253. moveCursorTo(1, 24);
  254. }
  255. refreshSettings() ;
  256. }
  257. }
  258. void Minitel::moveCursor(byte dir, int n) {
  259. if (dir == LEFT || dir == RIGHT || dir == UP || dir == DOWN) {
  260. for (int i=0; i<n; i++) {
  261. serialprint7(dir);
  262. }
  263. }
  264. }
  265. void Minitel::refreshSettings() {
  266. // Common parameters
  267. mode(_currentMode);
  268. textColor(_currentTextColor);
  269. bgColor(_currentBgColor); // Only in graphic mode ?
  270. blink(_currentBlink);
  271. cursor(_currentShowCursor);
  272. // Graphic mode specific parameters
  273. if (_currentMode == GRAPHIC_MODE) {
  274. pixelate(_currentUnderline);
  275. }
  276. // Text mode specific parameters
  277. if (_currentMode == TEXT_MODE) {
  278. video(_currentVideo);
  279. charSize(_currentSize);
  280. }
  281. }
  282. void Minitel::cursor() {
  283. cursor(true);
  284. }
  285. void Minitel::noCursor() {
  286. cursor(false);
  287. }
  288. void Minitel::cursor(boolean b) {
  289. if(b) {
  290. serialprint7(CURSOR_SHOW);
  291. }
  292. else {
  293. serialprint7(CURSOR_HIDE);
  294. }
  295. _currentShowCursor = b;
  296. }
  297. void Minitel::clearScreen() {
  298. serialprint7(CLEARSCREEN);
  299. }
  300. void Minitel::mode(byte mode) {
  301. if (mode == GRAPHIC_MODE || mode == TEXT_MODE) {
  302. serialprint7(mode);
  303. _currentMode = mode;
  304. }
  305. }
  306. void Minitel::graphicMode() {
  307. mode(GRAPHIC_MODE);
  308. }
  309. void Minitel::textMode() {
  310. mode(TEXT_MODE);
  311. }
  312. void Minitel::blink() {
  313. blink(true);
  314. }
  315. void Minitel::noBlink() {
  316. blink(false);
  317. }
  318. void Minitel::blink(boolean b) {
  319. serialprint7(27);
  320. if (b) {
  321. serialprint7(BLINK_ON);
  322. }
  323. else {
  324. serialprint7(BLINK_OFF);
  325. }
  326. _currentBlink = b;
  327. }
  328. void Minitel::charSize(byte type) {
  329. if (type == SIZE_NORMAL || type == SIZE_DOUBLE_HEIGHT || type == SIZE_DOUBLE_WIDTH || type == SIZE_DOUBLE) {
  330. serialprint7(27);
  331. serialprint7(type);
  332. _currentSize = type;
  333. }
  334. }
  335. void Minitel::incrustation(boolean b) {
  336. serialprint7(27);
  337. if (b) {
  338. serialprint7(INCRUSTATION_ON);
  339. }
  340. else {
  341. serialprint7(INCRUSTATION_OFF);
  342. }
  343. }
  344. void Minitel::incrustation() {
  345. incrustation(INCRUSTATION_ON);
  346. }
  347. void Minitel::noIncrustation() {
  348. incrustation(INCRUSTATION_OFF);
  349. }
  350. void Minitel::pixelate() {
  351. pixelate(true);
  352. }
  353. void Minitel::noPixelate() {
  354. pixelate(false);
  355. }
  356. void Minitel::pixelate(boolean b) {
  357. serialprint7(27);
  358. if (b) {
  359. serialprint7(UNDERLINE_ON);
  360. }
  361. else {
  362. serialprint7(UNDERLINE_OFF);
  363. }
  364. _currentUnderline = b;
  365. }
  366. void Minitel::lineMask(boolean b) {
  367. serialprint7(27);
  368. if (b) {
  369. serialprint7(LINE_MASK_ON);
  370. }
  371. else {
  372. serialprint7(LINE_MASK_OFF);
  373. }
  374. }
  375. void Minitel::lineMask() {
  376. lineMask(LINE_MASK_ON);
  377. }
  378. void Minitel::noLineMask() {
  379. lineMask(LINE_MASK_OFF);
  380. }
  381. void Minitel::video(byte v) {
  382. if (v == VIDEO_INVERT || v == VIDEO_STANDARD || v == VIDEO_TRANSPARENT) {
  383. serialprint7(27);
  384. serialprint7(v);
  385. _currentVideo = v;
  386. }
  387. }
  388. void Minitel::standardVideo() {
  389. video(VIDEO_STANDARD);
  390. }
  391. void Minitel::invertVideo() {
  392. video(VIDEO_INVERT);
  393. }
  394. void Minitel::transparentVideo() {
  395. video(VIDEO_TRANSPARENT);
  396. }
  397. void Minitel::setMaxSpeed() {
  398. /*
  399. serialprint7(27);
  400. serialprint7(SPEED_4800);
  401. */
  402. }
  403. // Bip
  404. // Less than 200ms isn't taken into account
  405. void Minitel::bip(long duration) {
  406. long beginTime = millis();
  407. while(millis() < beginTime+100) {//duration) {
  408. serialprint7(27);
  409. serialprint7(BIP);
  410. delay(100);
  411. }
  412. }
  413. byte Minitel::getKeyCode() {
  414. byte b = 255;
  415. b = read();
  416. if (b != 255) {
  417. Serial.println(b);
  418. }
  419. return b;
  420. }
  421. char Minitel::getKey() {
  422. byte b = 255;
  423. b = read();
  424. char c = '^';
  425. // Menu keys
  426. if (b == 147) {
  427. _menuKeyPressed = true;
  428. delay(50);
  429. }
  430. else if (_menuKeyPressed) {
  431. if (b == 198) { // Sommaire
  432. c = '1';
  433. }
  434. else if (b == 197) { // Annul
  435. c = '2';
  436. }
  437. else if (b == 66) { // Retour
  438. c = '3';
  439. }
  440. else if (b == 195) { // Répétition
  441. c = '4';
  442. }
  443. else if (b == 68) { // Guide
  444. c = '5';
  445. }
  446. else if (b == 71) { // Correction
  447. c = '6';
  448. }
  449. else if (b == 72) { // Suite
  450. c = '7';
  451. }
  452. else if (b == 65) { // Envoi
  453. c = '8';
  454. }
  455. _menuKeyPressed = false;
  456. }
  457. else {
  458. if (b == 160) { // Space
  459. c = ' ';
  460. }
  461. else if (b == 177) { // 1
  462. c = '1';
  463. }
  464. else if (b == 178) { // 2
  465. c = '2';
  466. }
  467. else if (b == 51) { // 3
  468. c = '3';
  469. }
  470. else if (b == 180) { // 4
  471. c = '4';
  472. }
  473. else if (b == 53) { // 5
  474. c = '5';
  475. }
  476. else if (b == 54) { // 6
  477. c = '6';
  478. }
  479. else if (b == 183) { // 7
  480. c = '7';
  481. }
  482. else if (b == 184) { // 8
  483. c = '8';
  484. }
  485. else if (b == 57) { // 9
  486. c = '9';
  487. }
  488. else if (b == 48) { // 0
  489. c = '0';
  490. }
  491. else if (b == 170) { // *
  492. c = '*';
  493. }
  494. else if (b == 163) { // #
  495. c = '#';
  496. }
  497. else if (b == 172) { // ,
  498. c = ',';
  499. }
  500. else if (b == 46) { // .
  501. c = '.';
  502. }
  503. else if (b == 39) { // '
  504. c = '\'';
  505. }
  506. else if (b == 187) { // ;
  507. c = ';';
  508. }
  509. else if (b == 45) { // -
  510. c = '-';
  511. }
  512. else if (b == 58) { // :
  513. c = ':';
  514. }
  515. else if (b == 63) { // ?
  516. c = '?';
  517. }
  518. else if (b == 65) { // A
  519. c = 'A';
  520. }
  521. else if (b == 66) { // B
  522. c = 'B';
  523. }
  524. else if (b == 195) { // C
  525. c = 'C';
  526. }
  527. else if (b == 68) { // D
  528. c = 'D';
  529. }
  530. else if (b == 197) { // E
  531. c = 'E';
  532. }
  533. else if (b == 198) { // F
  534. c = 'F';
  535. }
  536. else if (b == 71) { // G
  537. c = 'G';
  538. }
  539. else if (b == 72) { // H
  540. c = 'H';
  541. }
  542. else if (b == 201) { // I
  543. c = 'I';
  544. }
  545. else if (b == 202) { // J
  546. c = 'J';
  547. }
  548. else if (b == 75) { // K
  549. c = 'K';
  550. }
  551. else if (b == 204) { // L
  552. c = 'L';
  553. }
  554. else if (b == 77) { // M
  555. c = 'M';
  556. }
  557. else if (b == 78) { // N
  558. c = 'N';
  559. }
  560. else if (b == 207) { // O
  561. c = 'O';
  562. }
  563. else if (b == 80) { // P
  564. c = 'P';
  565. }
  566. else if (b == 209) { // Q
  567. c = 'Q';
  568. }
  569. else if (b == 210) { // R
  570. c = 'R';
  571. }
  572. else if (b == 83) { // S
  573. c = 'S';
  574. }
  575. else if (b == 212) { // T
  576. c = 'T';
  577. }
  578. else if (b == 85) { //U
  579. c = 'U';
  580. }
  581. else if (b == 86) { // V
  582. c = 'V';
  583. }
  584. else if (b == 215) { // W
  585. c = 'W';
  586. }
  587. else if (b == 216) { // X
  588. c = 'X';
  589. }
  590. else if (b == 89) { // Y
  591. c = 'Y';
  592. }
  593. else if (b == 90) { // Z
  594. c = 'Z';
  595. }
  596. else if (b == 33) { // !
  597. c = '!';
  598. }
  599. else if (b == 34) { // !
  600. c = '"';
  601. }
  602. else if (b == 163) { // #
  603. c = '#';
  604. }
  605. else if (b == 36) { // $
  606. c = '$';
  607. }
  608. else if (b == 165) { // %
  609. c = '%';
  610. }
  611. else if (b == 166) { // &
  612. c = '&';
  613. }
  614. else if (b == 39) { // '
  615. c = '\'';
  616. }
  617. else if (b == 40) { // (
  618. c = '(';
  619. }
  620. else if (b == 169) { // )
  621. c = ')';
  622. }
  623. else if (b == 219) { // [
  624. c = '[';
  625. }
  626. else if (b == 222) { // ↑
  627. c = '↑';
  628. }
  629. else if (b == 221) { // ]
  630. c = '[';
  631. }
  632. else if (b == 60) { // <
  633. c = '<';
  634. }
  635. else if (b == 190) { // >
  636. c = '>';
  637. }
  638. else if (b == 192) { // @
  639. c = '@';
  640. }
  641. else if (b == 43) { // +
  642. c = '+';
  643. }
  644. else if (b == 189) { // =
  645. c = '=';
  646. }
  647. else if (b == 170) { // *
  648. c = '*';
  649. }
  650. else if (b == 175) { // /
  651. c = '/';
  652. }
  653. else if (b == 123) { // /
  654. c = '|';
  655. }
  656. }
  657. return c;
  658. }
  659. boolean Minitel::isMenuKey() {
  660. return _menuKeyPressed;
  661. }
  662. void Minitel::rect(char c, int x, int y, int w, int h) {
  663. byte b = getCharByte(c);
  664. rect(b, x, y, w, h);
  665. }
  666. void Minitel::rect(byte c, int x, int y, int w, int h) {
  667. moveCursorTo(x, y);
  668. textByte(c);
  669. repeat(w);
  670. moveCursorTo(x, y+1);
  671. for (int i=0; i<h-2; i++) {
  672. textByte(c);
  673. moveCursor(DOWN);
  674. moveCursor(LEFT);
  675. }
  676. moveCursorTo(x+w, y+1);
  677. for (int i=0; i<h-2; i++) {
  678. textByte(c);
  679. moveCursor(DOWN);
  680. moveCursor(LEFT);
  681. }
  682. moveCursorTo(x, y+h-1);
  683. textByte(c);
  684. repeat(w);
  685. }
  686. void Minitel::spiral(int x, int y, int siz, int c) {
  687. int curSiz = 1;
  688. // Center
  689. specialChar(x, y, c);
  690. x++;
  691. // Spiral
  692. for (int i=0; i< siz; i++) {
  693. for (int j=0; j<curSiz; j++) {
  694. specialChar(x, y, c);
  695. y++;
  696. }
  697. curSiz++;
  698. for (int j=0; j<curSiz; j++) {
  699. specialChar(x, y, c);
  700. x--;
  701. }
  702. for (int j=0; j<curSiz; j++) {
  703. specialChar(x, y, c);
  704. y--;
  705. }
  706. curSiz++;
  707. for (int j=0; j<curSiz; j++) {
  708. specialChar(x, y, c);
  709. x++;
  710. }
  711. }
  712. }