Converter for Consul 262.5 terminal keyboard and VDX 52600 terminal.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

424 lines
8.4 KiB

  1. // Resources:
  2. // [Consul 262.4 Converter] https://deskthority.net/viewtopic.php?t=26908
  3. // [Consul 262.5 manual in CS] http://www.sapi.cz/prislusenstvi/c262-5.php#odkazp4
  4. #include <TimerOne.h>
  5. // pinout config
  6. const int pinData = 6; // out, host data
  7. const int pinStatus = 7; // in, host status
  8. const int clockPin = 5; // out, kbd clock
  9. const int dataPin = 3; // in, kbd data
  10. const int outPin = 4; // out, kbd led
  11. // constant config
  12. const int slaveClockDivider = 8;
  13. const int timerDelay = 520 / slaveClockDivider;
  14. // variables
  15. volatile int slaveClockStep = 0;
  16. char m[255];
  17. volatile int data = 0;
  18. int test = 0;
  19. volatile int counter = 0;
  20. int numbits = 10;
  21. // MODS >>>
  22. // [1] send debug scancode information to serial port
  23. bool modConsoleLog = true;
  24. // <<< MODS
  25. // ----------
  26. // KBD Output
  27. // ----------
  28. volatile long lastChange = 0;
  29. volatile int x = 0;
  30. volatile int dataWord = 0;
  31. volatile int dataState = 0;
  32. volatile int dataDelay = 0;
  33. volatile int packetDelay = 0;
  34. volatile int packetTail = 0;
  35. volatile bool nextKeyReady = false;
  36. volatile byte nextKey = 0;
  37. void typeKey(byte key) {
  38. nextKey = key;
  39. nextKeyReady = true;
  40. //Serial.print("Typing key "); Serial.println((int) key);
  41. }
  42. void sendKey(byte key) {
  43. dataWord = key;
  44. dataState = 8;
  45. dataDelay = 0;
  46. packetDelay = 0;
  47. packetTail = 15;
  48. //Serial.print("Sending key "); Serial.println((int) key);
  49. }
  50. void onHostStatusChange() {
  51. long timeNow = millis();
  52. long changeDiff = timeNow - lastChange;
  53. lastChange = timeNow;
  54. if (changeDiff >= 10 && nextKeyReady) {
  55. nextKeyReady = false;
  56. sendKey(nextKey);
  57. Timer1.start(); // synchronize with the host
  58. slaveClockStep = 0;
  59. }
  60. }
  61. void onHostClockCycle(void)
  62. {
  63. int dataBit = HIGH;
  64. if (packetDelay > 0) {
  65. packetDelay--;
  66. } else if (dataDelay > 0) {
  67. dataDelay--;
  68. dataBit = LOW;
  69. } else if (dataState > 0) {
  70. int bitToSend = (dataWord >> (dataState - 1)) & 1;
  71. dataBit = !bitToSend ? LOW : HIGH;
  72. dataState--;
  73. } else if (packetTail > 0) {
  74. packetTail--;
  75. dataBit = LOW;
  76. } else {
  77. }
  78. digitalWrite(pinData, dataBit);
  79. }
  80. // ---------
  81. // KBD Input
  82. // ---------
  83. const int receivingSteps = 16;
  84. volatile int clockStep = 0;
  85. volatile int receivingStep = 0;
  86. volatile int receivingData = 0;
  87. volatile int receivingBit = 0;
  88. void onSlaveClockInterrupt() {
  89. clockStep = (clockStep + 1) % 2;
  90. int clockValue = (clockStep % 2) ? HIGH : LOW;
  91. digitalWrite(clockPin, clockValue);
  92. int dataBit = digitalRead(dataPin);
  93. if (clockValue == LOW) {
  94. if (receivingData == 0 && dataBit == LOW) {
  95. receivingData = 1;
  96. receivingStep = 0;
  97. receivingBit = 0;
  98. test = 0;
  99. } else if (receivingData == 1) {
  100. receivingStep++;
  101. }
  102. if (receivingData == 1 && test == 0) {
  103. test = 1;
  104. receivingBit += dataBit == HIGH ? 1 : 0;
  105. if (receivingStep >= receivingSteps) {
  106. if (counter <= 8) {
  107. data = data >> 1;
  108. if (receivingBit > receivingSteps / 2) {
  109. bitSet(data, 7);
  110. }
  111. }
  112. counter++;
  113. receivingStep = 0;
  114. receivingBit = 0;
  115. if (counter >= numbits) {
  116. receivingData = 0;
  117. }
  118. }
  119. }
  120. }
  121. if (clockValue == HIGH && test == 1) {
  122. test = 0;
  123. }
  124. }
  125. void setupKeyMapping() {
  126. m[0] = 0;
  127. // top row special
  128. m[63] = 0x12; // ?? Setup
  129. m[62] = 'j'; // up
  130. m[61] = 'k'; // down
  131. m[59] = 'h'; // left
  132. m[60] = 'l'; // right
  133. // top numbers row
  134. m[228] = 0x1B; // ESC
  135. m[206] = '1';
  136. m[205] = '2';
  137. m[204] = '3';
  138. m[203] = '4';
  139. m[202] = '5';
  140. m[201] = '6';
  141. m[200] = '7';
  142. m[199] = '8';
  143. m[198] = '9';
  144. m[207] = '0';
  145. m[210] = '-';
  146. m[161] = '^';
  147. m[0] = ' '; // Empty cap
  148. m[247] = 0x08; // Backspace
  149. m[56] = 0x10; // Break
  150. // top letter row
  151. m[246] = '\t';
  152. m[142] = 'q';
  153. m[142+32] = 'Q';
  154. m[136] = 'w';
  155. m[136+32] = 'W';
  156. m[154] = 'e';
  157. m[154+32] = 'E';
  158. m[141] = 'r';
  159. m[141+32] = 'R';
  160. m[139] = 't';
  161. m[139+32] = 'T';
  162. m[134] = 'y';
  163. m[134+32] = 'Y';
  164. m[138] = 'u';
  165. m[138+32] = 'U';
  166. m[150] = 'i';
  167. m[150+32] = 'I';
  168. m[144] = 'o';
  169. m[144+32] = 'O';
  170. m[143] = 'p';
  171. m[143+32] = 'P';
  172. m[191] = '@';
  173. m[164] = '[';
  174. m[245] = 0x0A;
  175. m[128] = 0x7F;
  176. // middle letter row
  177. m[158] = 'a';
  178. m[158+32] = 'A';
  179. m[140] = 's';
  180. m[140+32] = 'S';
  181. m[155] = 'd';
  182. m[155+32] = 'D';
  183. m[153] = 'f';
  184. m[153+32] = 'F';
  185. m[152] = 'g';
  186. m[152+32] = 'G';
  187. m[151] = 'h';
  188. m[151+32] = 'H';
  189. m[149] = 'j';
  190. m[149+32] = 'J';
  191. m[148] = 'k';
  192. m[148+32] = 'K';
  193. m[147] = 'l';
  194. m[147+32] = 'L';
  195. m[196] = ';';
  196. m[196+32] = '+';
  197. m[197] = ':';
  198. m[197+32] = '*';
  199. m[162] = ']';
  200. m[162+32] = '}';
  201. m[242] = 0x0D;
  202. // bottom letter row
  203. m[58] = 0x11; // No Scroll
  204. m[133] = 'z';
  205. m[133+32] = 'Z';
  206. m[135] = 'x';
  207. m[135+32] = 'X';
  208. m[156] = 'c';
  209. m[156+32] = 'C';
  210. m[137] = 'v';
  211. m[137+32] = 'V';
  212. m[157] = 'b';
  213. m[157+32] = 'B';
  214. m[145] = 'n';
  215. m[145+32] = 'N';
  216. m[146] = 'm';
  217. m[146+32] = 'M';
  218. m[211] = ',';
  219. m[211+32] = '<';
  220. m[209] = '.';
  221. m[209+32] = '>';
  222. m[208] = '/';
  223. m[208+32] = '?';
  224. m[163] = '\\';
  225. m[163+32] = '|';
  226. m[78] = 'x'; // Blank cap
  227. m[77] = 'X'; // Blank cap + shift
  228. // spacebar
  229. m[223] = ' '; // Spacebar
  230. /*/ numpad
  231. m[] = '';
  232. m[+32] = '';
  233. m[] = '';
  234. m[+32] = '';
  235. m[] = '';
  236. m[+32] = '';
  237. m[] = '';
  238. m[+32] = '';
  239. m[] = '';
  240. m[+32] = '';
  241. /*
  242. m[] = '';
  243. m[] = '';
  244. m[] = '';
  245. m[] = '';
  246. m[] = '';
  247. m[] = '';
  248. m[] = '';
  249. m[] = '';
  250. m[] = '';
  251. m[] = '';
  252. m[] = '';
  253. m[] = '';
  254. m[] = '';
  255. m[] = '';
  256. m[] = '';
  257. m[] = '';
  258. m[] = '';
  259. m[] = '';
  260. m[] = '';
  261. m[] = '';
  262. m[] = '';
  263. m[] = '';
  264. m[] = '';
  265. m[] = '';
  266. m[] = '';
  267. m[] = '';
  268. m[] = '';
  269. m[] = '';
  270. m[] = '';
  271. m[] = '';
  272. m[] = '';
  273. m[] = '';
  274. m[] = '';
  275. m[] = '';
  276. m[] = '';
  277. m[] = '';
  278. m[] = '';
  279. m[] = '';
  280. m[] = '';
  281. m[] = '';
  282. m[] = '';
  283. m[] = '';
  284. m[] = '';
  285. m[] = '';
  286. m[] = '';
  287. m[] = '';
  288. m[] = '';
  289. m[] = '';
  290. /**/
  291. //
  292. // 208 local ON
  293. // 209 leave local
  294. // 210 half/full duplex
  295. // 211 go home?
  296. // 212 print Ps junk
  297. // 213 mem protect
  298. // 214 disable NAT
  299. // 217 clear line?
  300. // 218 shows cursor
  301. // 219 hides cursor
  302. // 221 mem prot
  303. // 223 reset prompt
  304. // 241 random junk symbols
  305. // 243 random junk symbols
  306. // 244 stop junk
  307. // 248 hides cursor
  308. // 251 Hard copy
  309. // 250 Keyb. Inh.
  310. }
  311. char translateKeyToChar(int key) {
  312. if (sizeof(m) <= key) {
  313. return 0;
  314. }
  315. return m[key];
  316. }
  317. void printChar(char keyChar) {
  318. Serial.print("'"); Serial.print(keyChar); Serial.print("' ("); Serial.print(int(keyChar)); Serial.println(")");
  319. }
  320. void processKbdByte(int data) {
  321. int key = data;
  322. char keyChar = translateKeyToChar(key);
  323. if (modConsoleLog) {
  324. Serial.print("Key: <"); Serial.print(int(key)); Serial.print("> ");
  325. Serial.print("Char: "); printChar(keyChar);
  326. }
  327. #ifdef KEYBOARD
  328. Keyboard.press(keyChar);
  329. delay(10);
  330. Keyboard.release(keyChar);
  331. #endif
  332. typeKey(keyChar);
  333. }
  334. // ----------------------
  335. // Input and Output Merge
  336. // ----------------------
  337. void onTimerInterrupt()
  338. {
  339. onSlaveClockInterrupt();
  340. if (slaveClockStep == 0) {
  341. onHostClockCycle();
  342. }
  343. slaveClockStep = (slaveClockStep + 1) % slaveClockDivider;
  344. }
  345. // ----
  346. // Main
  347. // ----
  348. void setup(void)
  349. {
  350. Serial.begin(9600);
  351. setupKeyMapping();
  352. pinMode(pinData, OUTPUT);
  353. pinMode(dataPin, INPUT);
  354. pinMode(outPin, OUTPUT);
  355. pinMode(clockPin, OUTPUT);
  356. pinMode(pinStatus, INPUT_PULLUP);
  357. digitalWrite(pinData, HIGH);
  358. digitalWrite(outPin, LOW);
  359. attachInterrupt(digitalPinToInterrupt(pinStatus), onHostStatusChange, CHANGE);
  360. Timer1.initialize(timerDelay);
  361. Timer1.attachInterrupt(onTimerInterrupt);
  362. Serial.println("Keyboard ready");
  363. }
  364. int qwe = 0;
  365. void loop(void)
  366. {
  367. // type key from serial
  368. if (!nextKeyReady && Serial.available() > 0) {
  369. long key = Serial.parseInt(SKIP_ALL);
  370. if (key != 0) {
  371. typeKey(key);
  372. }
  373. }
  374. /**/
  375. // type key from keyboard
  376. if (counter >= numbits) {
  377. processKbdByte(data);
  378. data = B0;
  379. counter = 0;
  380. }
  381. /**/
  382. /*/ auto-type test
  383. delay(2000);
  384. int k = 128 + qwe;
  385. typeKey(k);
  386. qwe = (qwe + 1) % 128;
  387. Serial.print("QWE "); Serial.println((int)k);
  388. if (qwe % 5 == 0) delay(2000);
  389. /**/
  390. }