Minitel.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. /**
  2. Minitel library for Arduino (v0.2) / April 2015
  3. http://github.com/01010101/Minitel
  4. By Jerome Saint-Clair aka 01010101
  5. http://saint-clair.net
  6. For the Graffiti Research Lab France
  7. http://graffitiresearchlab.fr
  8. Based on works by the Tetalab (Fabrice, Renaud, PG & Phil)
  9. http://tetalab.org
  10. */
  11. #include "Arduino.h"
  12. #include "SoftwareSerial.h"
  13. #include "Minitel.h"
  14. byte _currentBgColor = BLACK;
  15. byte _currentTextColor = WHITE;
  16. byte _currentMode = TEXT_MODE;
  17. byte _currentVideo = VIDEO_STANDARD;
  18. byte _currentSize = SIZE_NORMAL;
  19. boolean _currentUnderline = false;
  20. boolean _currentBlink = false;
  21. boolean _currentShowCursor = false;
  22. Minitel::Minitel() : SoftwareSerial(6, 7) {
  23. init((short)(4800));
  24. }
  25. Minitel::Minitel(short speed) : SoftwareSerial(6, 7) {
  26. init(speed);
  27. }
  28. Minitel::Minitel(int rx, int tx) : SoftwareSerial(rx, tx) {
  29. init((short)(4800));
  30. }
  31. Minitel::Minitel(int rx, int tx, short speed) : SoftwareSerial(rx, tx) {
  32. init(speed);
  33. }
  34. void Minitel::init(short speed) {
  35. Serial.begin(1200);
  36. begin(1200);
  37. setSpeed(speed); // Set serial speed
  38. useDefaultColors();
  39. refreshSettings();
  40. }
  41. byte Minitel::getGraphicChar(String s) {
  42. byte carac = 32; // caractère pixel
  43. if (s.length() == 6) {
  44. carac += s[0] == '0' ? 0 : 1;
  45. carac += s[1] == '0' ? 0 : 2;
  46. carac += s[2] == '0' ? 0 : 4;
  47. carac += s[3] == '0' ? 0 : 8;
  48. carac += s[4] == '0' ? 0 : 16;
  49. carac += s[5] == '0' ? 0 : 32;
  50. return carac;
  51. }
  52. return 9;
  53. }
  54. void Minitel::serialprint7(byte b) {
  55. boolean i = false;
  56. for (int j = 0; j < 8; j++) {
  57. if (bitRead(b, j) == 1) {
  58. i = !i; //calcul de la parité
  59. }
  60. }
  61. if (i) {
  62. bitWrite(b, 7, 1); //ecriture de la partié
  63. }
  64. else {
  65. bitWrite(b, 7, 0); //ecriture de la partié
  66. }
  67. write(b); //ecriture du byte sur le software serial
  68. }
  69. void Minitel::graphic(String s, int x, int y) {
  70. moveCursorTo(x, y);
  71. graphic(s);
  72. }
  73. void Minitel::graphic(String s) {
  74. serialprint7(getGraphicChar(s));
  75. }
  76. void Minitel::textByte(byte b) {
  77. serialprint7(b);
  78. }
  79. void Minitel::textByte(byte b, int x, int y) {
  80. moveCursorTo(x, y);
  81. textByte(b);
  82. }
  83. boolean Minitel::textChar(byte c) {
  84. byte charByte = getCharByte(c);
  85. if (isValidChar(charByte)) {
  86. serialprint7(charByte);
  87. return true;
  88. }
  89. return false;
  90. }
  91. boolean Minitel::textChar(byte c, int x, int y) {
  92. moveCursorTo(x, y);
  93. return textChar(c);
  94. }
  95. void Minitel::text(String s, int x, int y) {
  96. text(s, x, y, HORIZONTAL);
  97. }
  98. void Minitel::text(String s) {
  99. text(s, HORIZONTAL);
  100. }
  101. void Minitel::text(String s, int x, int y, int orientation) {
  102. moveCursorTo(x, y);
  103. text(s, orientation);
  104. }
  105. void Minitel::text(String s, int orientation) {
  106. for (unsigned int i = 0; i < s.length(); i++) {
  107. char c = s.charAt(i);
  108. boolean indent = false;
  109. if (isAccent(c)) {
  110. i += 1; // chars with accents take 2 array indexes
  111. c = s.charAt(i);
  112. indent = printAccentChar(c);
  113. }
  114. else {
  115. // TODO Check if c cedil
  116. indent = textChar(c);
  117. }
  118. if (indent && orientation == VERTICAL) {
  119. moveCursor(LEFT);
  120. moveCursor(DOWN);
  121. }
  122. }
  123. }
  124. // Characters
  125. /*
  126. xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx // 0 -> 32
  127. !"#$%&'()*+,-./0123456789:;<=>?@ // 33 -> 64
  128. ABCDEFGHIJKLMNOPQRSTUVWXYZ[\] // 65 -> 93
  129. x // 94 up arrow
  130. _ // 95 lower pipe associated to underscore
  131. x // 96 pipe
  132. abcdefghijklmnopqrstuvwxyz // 97 -> 122
  133. // 123 124 125 126 various pipes
  134. */
  135. // Used to display characters sent from the Arduino
  136. // As a result, not all Minitel supported characters can be sent to/from Arduino
  137. // However, they can be displayed using the specialChar() or graphic functions
  138. byte Minitel::getCharByte(char c) {
  139. String characters = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]x_xabcdefghijklmnopqrstuvwxyz";
  140. return (byte) characters.lastIndexOf(c);
  141. }
  142. //
  143. boolean Minitel::isSerializableKey() {
  144. return (_characterKey >= 33 && _characterKey <= 122 && _characterKey != 94 && _characterKey != 96);
  145. }
  146. void Minitel::specialChar(byte b, int x, int y) {
  147. moveCursorTo(x, y);
  148. specialChar(b);
  149. }
  150. void Minitel::specialChar(byte b) {
  151. if (isValidChar(b)) {
  152. serialprint7(25);
  153. serialprint7(b);
  154. if (b == 75) {
  155. serialprint7(99);
  156. }
  157. }
  158. }
  159. boolean Minitel::isValidChar(byte index) {
  160. if (index >= 32 && index <= 123) {
  161. return true;
  162. }
  163. return false;
  164. }
  165. //
  166. //
  167. // ACCENTS HANDLING
  168. //
  169. //
  170. boolean Minitel::isAccent(char c) {
  171. //String accents = "àáâäèéêëìíîïòóôöùúûü";
  172. if (accents.indexOf(c) >= 0) {
  173. return true;
  174. }
  175. return false;
  176. }
  177. boolean Minitel::printAccentChar(char c) {
  178. //String accents = "àáâäèéêëìíîïòóôöùúûü";
  179. int index = (accents.indexOf(c)) / 2;
  180. int accentTypeIndex = index % 4;
  181. printAccent(accentTypeIndex);
  182. // Check which letter
  183. int letterIndex = floor(index / 4);
  184. char letter = getAccentLetter(letterIndex);
  185. textChar(letter);
  186. return true; // There should be no pb printing accents
  187. }
  188. void Minitel::printAccent(int index) {
  189. switch (index) {
  190. case (0) :
  191. specialChar(SPE_CHAR_GRAVE);
  192. break;
  193. case (1) :
  194. specialChar(SPE_CHAR_ACUTE);
  195. break;
  196. case (2) :
  197. specialChar(SPE_CHAR_CIRCUMFLEX);
  198. break;
  199. default :
  200. specialChar(SPE_CHAR_UMLAUT);
  201. }
  202. }
  203. char Minitel::getAccentLetter(int letterIndex) {
  204. switch (letterIndex) {
  205. case (0) :
  206. return ('a');
  207. break;
  208. case (1) :
  209. return ('e');
  210. break;
  211. case (2) :
  212. return ('i');
  213. break;
  214. case (3) :
  215. return ('o');
  216. break;
  217. default :
  218. return ('u');
  219. }
  220. }
  221. //
  222. //
  223. // REPEAT CHARACTER
  224. //
  225. //
  226. void Minitel::repeat(byte n) {
  227. serialprint7(18);
  228. serialprint7(64 + n);
  229. }
  230. //
  231. //
  232. // COLOR MANAGEMENT
  233. //
  234. //
  235. void Minitel::bgColor(byte c) {
  236. if (c >= 0 && c <= 7) {
  237. serialprint7(27);
  238. serialprint7(c + 80);
  239. _currentBgColor = c;
  240. }
  241. }
  242. void Minitel::textColor(byte c) {
  243. if (c >= 0 && c <= 7) {
  244. serialprint7(27);
  245. serialprint7(c + 64);
  246. _currentTextColor = c;
  247. }
  248. }
  249. void Minitel::useDefaultColors() {
  250. bgColor(BLACK);
  251. textColor(WHITE);
  252. }
  253. //
  254. //
  255. // MOVING AND POSITIONNING THE CURSOR
  256. //
  257. //
  258. void Minitel::moveCursorTo(byte x, byte y) {
  259. serialprint7(31); // Code positionnement de curseur
  260. serialprint7(64 + y); // coordonnées x (x+64) (x de 1 à 40)
  261. serialprint7(64 + x); // coordonnées y (y+64) (y de 1 à 24)
  262. refreshSettings();
  263. }
  264. void Minitel::moveCursor(byte dir) {
  265. if (dir == LEFT || dir == RIGHT || dir == UP || dir == DOWN) {
  266. serialprint7(dir);
  267. }
  268. }
  269. void Minitel::moveCursorTo(byte location) {
  270. if (location == HOME || location == LINE_END || location == TOP_LEFT) {
  271. serialprint7(location);
  272. }
  273. else if (location == CENTER || location == TOP_RIGHT || location == BOTTOM_RIGHT || location == BOTTOM_LEFT) {
  274. if (location == CENTER) {
  275. moveCursorTo(20, 12);
  276. }
  277. else if (location == TOP_RIGHT) {
  278. moveCursorTo(40, 1);
  279. }
  280. else if (location == BOTTOM_RIGHT) {
  281. moveCursorTo(40, 24);
  282. }
  283. else if (location == BOTTOM_LEFT) {
  284. moveCursorTo(1, 24);
  285. }
  286. refreshSettings() ;
  287. }
  288. }
  289. void Minitel::moveCursor(byte dir, int n) {
  290. if (dir == LEFT || dir == RIGHT || dir == UP || dir == DOWN) {
  291. for (int i = 0; i < n; i++) {
  292. serialprint7(dir);
  293. }
  294. }
  295. }
  296. //
  297. //
  298. // RESTORING THE CURRENT SETTINGS AS
  299. // SOME COMMANDS SEEM TO RESET THEM
  300. //
  301. //
  302. void Minitel::refreshSettings() {
  303. // Common parameters
  304. serialprint7(_currentMode);
  305. textColor(_currentTextColor);
  306. bgColor(_currentBgColor); // Only in graphic mode ?
  307. blink(_currentBlink);
  308. cursor(_currentShowCursor);
  309. // Graphic mode specific parameters
  310. if (_currentMode == GRAPHIC_MODE) {
  311. pixelate(_currentUnderline);
  312. }
  313. // Text mode specific parameters
  314. if (_currentMode == TEXT_MODE) {
  315. video(_currentVideo);
  316. charSize(_currentSize);
  317. }
  318. }
  319. //
  320. //
  321. // SHOW / HIDE CURSOR
  322. //
  323. //
  324. void Minitel::cursor() {
  325. cursor(true);
  326. }
  327. void Minitel::noCursor() {
  328. cursor(false);
  329. }
  330. void Minitel::cursor(boolean b) {
  331. if (b) {
  332. serialprint7(CURSOR_SHOW);
  333. }
  334. else {
  335. serialprint7(CURSOR_HIDE);
  336. }
  337. _currentShowCursor = b;
  338. }
  339. //
  340. //
  341. // CLEANING SYSTEM
  342. //
  343. //
  344. void Minitel::clearScreen() {
  345. serialprint7(CLEARSCREEN);
  346. refreshSettings();
  347. }
  348. //
  349. //
  350. // TEXT OR GRAPHIC MODE SELECTION
  351. //
  352. //
  353. void Minitel::mode(byte mode) {
  354. if (mode == GRAPHIC_MODE || mode == TEXT_MODE) {
  355. _currentMode = mode;
  356. refreshSettings();
  357. }
  358. }
  359. void Minitel::graphicMode() {
  360. mode(GRAPHIC_MODE);
  361. }
  362. void Minitel::textMode() {
  363. mode(TEXT_MODE);
  364. }
  365. //
  366. //
  367. //
  368. //
  369. //
  370. void Minitel::blink() {
  371. blink(true);
  372. }
  373. void Minitel::noBlink() {
  374. blink(false);
  375. }
  376. void Minitel::blink(boolean b) {
  377. serialprint7(27);
  378. if (b) {
  379. serialprint7(BLINK_ON);
  380. }
  381. else {
  382. serialprint7(BLINK_OFF);
  383. }
  384. _currentBlink = b;
  385. }
  386. void Minitel::charSize(byte type) {
  387. if (type == SIZE_NORMAL || type == SIZE_DOUBLE_HEIGHT || type == SIZE_DOUBLE_WIDTH || type == SIZE_DOUBLE) {
  388. serialprint7(27);
  389. serialprint7(type);
  390. _currentSize = type;
  391. }
  392. }
  393. void Minitel::incrustation(boolean b) {
  394. serialprint7(27);
  395. if (b) {
  396. serialprint7(INCRUSTATION_ON);
  397. }
  398. else {
  399. serialprint7(INCRUSTATION_OFF);
  400. }
  401. }
  402. void Minitel::incrustation() {
  403. incrustation(INCRUSTATION_ON);
  404. }
  405. void Minitel::noIncrustation() {
  406. incrustation(INCRUSTATION_OFF);
  407. }
  408. void Minitel::pixelate() {
  409. pixelate(true);
  410. }
  411. void Minitel::noPixelate() {
  412. pixelate(false);
  413. }
  414. void Minitel::pixelate(boolean b) {
  415. serialprint7(27);
  416. if (b) {
  417. serialprint7(UNDERLINE_ON);
  418. }
  419. else {
  420. serialprint7(UNDERLINE_OFF);
  421. }
  422. _currentUnderline = b;
  423. }
  424. void Minitel::lineMask(boolean b) {
  425. serialprint7(27);
  426. if (b) {
  427. serialprint7(LINE_MASK_ON);
  428. }
  429. else {
  430. serialprint7(LINE_MASK_OFF);
  431. }
  432. }
  433. void Minitel::lineMask() {
  434. lineMask(LINE_MASK_ON);
  435. }
  436. void Minitel::noLineMask() {
  437. lineMask(LINE_MASK_OFF);
  438. }
  439. void Minitel::video(byte v) {
  440. if (v == VIDEO_INVERT || v == VIDEO_STANDARD || v == VIDEO_TRANSPARENT) {
  441. serialprint7(27);
  442. serialprint7(v);
  443. _currentVideo = v;
  444. }
  445. }
  446. void Minitel::standardVideo() {
  447. video(VIDEO_STANDARD);
  448. }
  449. void Minitel::invertVideo() {
  450. video(VIDEO_INVERT);
  451. }
  452. void Minitel::transparentVideo() {
  453. video(VIDEO_TRANSPARENT);
  454. }
  455. void Minitel::setSpeed(short speed) { // speed must be 300, 1200, 4800, 9600 (for Minitel 2 only)
  456. serialprint7(27);
  457. serialprint7(58);
  458. serialprint7(107);
  459. switch (speed) {
  460. case 300 :
  461. serialprint7(82);
  462. begin( 300);
  463. break;
  464. case 1200 :
  465. serialprint7(100);
  466. begin(1200);
  467. break;
  468. case 4800 :
  469. serialprint7(118);
  470. begin(4800);
  471. break;
  472. case 9600 : // For Minitel 2 ONLY !
  473. serialprint7(127);
  474. begin(9600);
  475. break;
  476. }
  477. }
  478. //
  479. //
  480. // SOUND
  481. //
  482. //
  483. // Less than 200ms isn't taken into account
  484. void Minitel::bip(unsigned long duration) {
  485. unsigned long beginTime = millis();
  486. while (millis() < beginTime + 100ul) { //duration) {
  487. serialprint7(27);
  488. serialprint7(BIP);
  489. delay(100);
  490. }
  491. }
  492. //
  493. //
  494. // KEYSTROKES ANALYSIS AND LOGGING
  495. //
  496. //
  497. /*
  498. Read and decode keyboard input and store values in according variables
  499. _specialCharacterKey if a special character Jeu G2, schema 2.8 p103
  500. _characterKey if a normal character Jeu G0, schema 2.5 p 100
  501. _menuKey is a menu key
  502. */
  503. void Minitel::readKey() {
  504. _menuKey = -1;
  505. _specialCharacterKey = -1;
  506. _characterKey = -1;
  507. byte b = 255;
  508. b = read();
  509. // Menu keys start with 147 + another number
  510. if (b == 147) {
  511. _accentKey = -1; // Drop previously set accent
  512. delay(50); // Wait a bit
  513. _menuKey = read(); // Read the next byte
  514. }
  515. // Shift or Ctrl key with GP2 character set start with 153
  516. else if (b == 153) {
  517. _accentKey = -1; // Drop previously set accent
  518. delay(50); // Wait a bit
  519. b = read(); // Read the next byte
  520. if (b == 65 || b == 66 || b == 72 || b == 195) { // Accent key
  521. _accentKey = b % 128;
  522. return;
  523. }
  524. else {
  525. _specialCharacterKey = b % 128;
  526. if (b == 75) { // Special case for the ç
  527. delay(50); // Wait a bit
  528. b = read(); // Read the next byte
  529. if (b == 99) {
  530. _specialCharacterKey = 75; // Implicit cedil with implicit c
  531. }
  532. }
  533. }
  534. }
  535. // Non prefixed keys
  536. else if (b != 255) {
  537. _characterKey = b % 128;
  538. // If an accent key was pressed before check if character can have this an accent
  539. if (_characterKey == 97 || _characterKey == 101 || _characterKey == 105 || _characterKey == 111 || _characterKey == 117 ) {
  540. // Remove accents if not supported by this letter
  541. if (_characterKey == 97 && _accentKey == SPE_CHAR_ACUTE ) { // a
  542. _accentKey = -1;
  543. }
  544. else if (_characterKey == 105 && (_accentKey == SPE_CHAR_GRAVE || _accentKey == SPE_CHAR_ACUTE )) { // i
  545. _accentKey = -1;
  546. }
  547. else if (_characterKey == 111 && (_accentKey == SPE_CHAR_GRAVE || _accentKey == SPE_CHAR_ACUTE )) { // o
  548. _accentKey = -1;
  549. }
  550. else if (_characterKey == 117 && _accentKey == SPE_CHAR_ACUTE ) { // u
  551. _accentKey = -1;
  552. }
  553. }
  554. else {
  555. _accentKey = -1;
  556. }
  557. }
  558. }
  559. //
  560. //
  561. // KEYS GETTERS
  562. //
  563. //
  564. boolean Minitel::keyTyped() {
  565. return isMenuKey() || isCharacterKey() || isSpecialCharacterKey() || accentKeyStored() ;
  566. }
  567. boolean Minitel::isMenuKey() {
  568. return _menuKey != -1;
  569. }
  570. int Minitel::getMenuKey() {
  571. return _menuKey;
  572. }
  573. boolean Minitel::isSpecialCharacterKey() {
  574. return _specialCharacterKey != -1;
  575. }
  576. int Minitel::getSpecialCharacterKey() {
  577. return _specialCharacterKey;
  578. }
  579. boolean Minitel::isCharacterKey() {
  580. return _characterKey != -1;
  581. }
  582. char Minitel::getCharacterKey() {
  583. return _characterKey;
  584. }
  585. boolean Minitel::accentKeyStored() {
  586. return _accentKey != -1;
  587. }
  588. int Minitel::getAccentKey() {
  589. return _accentKey;
  590. }
  591. //
  592. //
  593. // DRAWING FUNCTIONS
  594. //
  595. //
  596. void Minitel::rect(char c, int x, int y, int w, int h) {
  597. byte b = getCharByte(c);
  598. rect(b, x, y, w, h);
  599. }
  600. void Minitel::rect(byte c, int x, int y, int w, int h) {
  601. moveCursorTo(x, y);
  602. textByte(c);
  603. repeat(w);
  604. moveCursorTo(x, y + 1);
  605. for (int i = 0; i < h - 2; i++) {
  606. textByte(c);
  607. moveCursor(DOWN);
  608. moveCursor(LEFT);
  609. }
  610. moveCursorTo(x + w, y + 1);
  611. for (int i = 0; i < h - 2; i++) {
  612. textByte(c);
  613. moveCursor(DOWN);
  614. moveCursor(LEFT);
  615. }
  616. moveCursorTo(x, y + h - 1);
  617. textByte(c);
  618. repeat(w);
  619. }
  620. void Minitel::spiral(int x, int y, int siz, int c) {
  621. int curSiz = 1;
  622. // Center
  623. specialChar(c, x, y);
  624. x++;
  625. // Spiral
  626. for (int i = 0; i < siz; i++) {
  627. for (int j = 0; j < curSiz; j++) {
  628. specialChar(c, x, y);
  629. y++;
  630. }
  631. curSiz++;
  632. for (int j = 0; j < curSiz; j++) {
  633. specialChar(c, x, y);
  634. x--;
  635. }
  636. for (int j = 0; j < curSiz; j++) {
  637. specialChar(c, x, y);
  638. y--;
  639. }
  640. curSiz++;
  641. for (int j = 0; j < curSiz; j++) {
  642. specialChar(c, x, y);
  643. x++;
  644. }
  645. }
  646. }