Minitel.cpp 14 KB

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