Minitel.cpp 14 KB

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