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.

340 lines
6.8 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. digitalWrite(outPin, HIGH);
  100. } else if (receivingData == 1) {
  101. receivingStep++;
  102. digitalWrite(outPin, HIGH);
  103. }
  104. if (receivingData == 1 && test == 0) {
  105. test = 1;
  106. receivingBit += dataBit == HIGH ? 1 : 0;
  107. if (receivingStep >= receivingSteps) {
  108. if (counter <= 8) {
  109. data = data >> 1;
  110. if (receivingBit > receivingSteps / 2) {
  111. bitSet(data, 7);
  112. }
  113. }
  114. counter++;
  115. receivingStep = 0;
  116. receivingBit = 0;
  117. digitalWrite(outPin, LOW);
  118. if (counter >= numbits) {
  119. receivingData = 0;
  120. }
  121. }
  122. }
  123. }
  124. if (clockValue == HIGH && test == 1) {
  125. test = 0;
  126. }
  127. }
  128. void setupKeyMapping() {
  129. m[0] = 0;
  130. m[223] = ' '; // Spacebar
  131. // top row special
  132. m[63] = 0x12; // ?? Setup
  133. m[62] = 'j'; // up
  134. m[61] = 'k'; // down
  135. m[59] = 'h'; // left
  136. m[60] = 'l'; // right
  137. // top numbers row
  138. m[228] = 0x1B; // ESC
  139. m[206] = '1';
  140. m[205] = '2';
  141. m[204] = '3';
  142. m[203] = '4';
  143. m[202] = '5';
  144. m[201] = '6';
  145. m[200] = '7';
  146. m[199] = '8';
  147. m[198] = '9';
  148. m[207] = '0';
  149. m[210] = '-';
  150. m[161] = '^';
  151. m[0] = ' '; // Empty cap
  152. m[247] = 0x08; // Backspace
  153. m[56] = 0x10; // Break
  154. // top letter row
  155. m[246] = '\t';
  156. m[142] = 'q';
  157. m[142+32] = 'Q';
  158. m[136] = 'w';
  159. m[136+32] = 'W';
  160. m[154] = 'e';
  161. m[154+32] = 'E';
  162. m[141] = 'r';
  163. m[141+32] = 'R';
  164. m[139] = 't';
  165. m[139+32] = 'T';
  166. m[134] = 'y';
  167. m[134+32] = 'Y';
  168. m[138] = 'u';
  169. m[138+32] = 'U';
  170. m[150] = 'i';
  171. m[150+32] = 'I';
  172. m[144] = 'o';
  173. m[144+32] = 'O';
  174. m[143] = 'p';
  175. m[143+32] = 'P';
  176. m[191] = '@';
  177. m[164] = '[';
  178. m[245] = 0x0A;
  179. m[128] = 0x7F;
  180. // middle letter row
  181. /*
  182. m[] = '';
  183. m[] = '';
  184. m[] = '';
  185. m[] = '';
  186. m[] = '';
  187. m[] = '';
  188. m[] = '';
  189. m[] = '';
  190. m[] = '';
  191. m[] = '';
  192. m[] = '';
  193. m[] = '';
  194. m[] = '';
  195. m[] = '';
  196. m[] = '';
  197. m[] = '';
  198. m[] = '';
  199. m[] = '';
  200. m[] = '';
  201. m[] = '';
  202. m[] = '';
  203. m[] = '';
  204. m[] = '';
  205. m[] = '';
  206. m[] = '';
  207. m[] = '';
  208. m[] = '';
  209. m[] = '';
  210. m[] = '';
  211. m[] = '';
  212. m[] = '';
  213. m[] = '';
  214. m[] = '';
  215. m[] = '';
  216. m[] = '';
  217. m[] = '';
  218. m[] = '';
  219. m[] = '';
  220. m[] = '';
  221. m[] = '';
  222. m[] = '';
  223. m[] = '';
  224. m[] = '';
  225. m[] = '';
  226. m[] = '';
  227. m[] = '';
  228. m[] = '';
  229. m[] = '';
  230. /**/
  231. }
  232. char translateKeyToChar(int key) {
  233. if (sizeof(m) <= key) {
  234. return 0;
  235. }
  236. return m[key];
  237. }
  238. void printChar(char keyChar) {
  239. Serial.print("'"); Serial.print(keyChar); Serial.print("' ("); Serial.print(int(keyChar)); Serial.println(")");
  240. }
  241. void processKbdByte(int data) {
  242. int key = data;
  243. char keyChar = translateKeyToChar(key);
  244. if (modConsoleLog) {
  245. Serial.print("Key: <"); Serial.print(int(key)); Serial.print("> ");
  246. Serial.print("Char: "); printChar(keyChar);
  247. }
  248. #ifdef KEYBOARD
  249. Keyboard.press(keyChar);
  250. delay(10);
  251. Keyboard.release(keyChar);
  252. #endif
  253. typeKey(keyChar);
  254. }
  255. // ----------------------
  256. // Input and Output Merge
  257. // ----------------------
  258. void onTimerInterrupt()
  259. {
  260. onSlaveClockInterrupt();
  261. if (slaveClockStep == 0) {
  262. onHostClockCycle();
  263. }
  264. slaveClockStep = (slaveClockStep + 1) % slaveClockDivider;
  265. }
  266. // ----
  267. // Main
  268. // ----
  269. void setup(void)
  270. {
  271. Serial.begin(9600);
  272. setupKeyMapping();
  273. pinMode(pinData, OUTPUT);
  274. pinMode(dataPin, INPUT);
  275. pinMode(outPin, OUTPUT);
  276. pinMode(clockPin, OUTPUT);
  277. pinMode(pinStatus, INPUT_PULLUP);
  278. digitalWrite(pinData, HIGH);
  279. digitalWrite(outPin, LOW);
  280. attachInterrupt(digitalPinToInterrupt(pinStatus), onHostStatusChange, CHANGE);
  281. Timer1.initialize(timerDelay);
  282. Timer1.attachInterrupt(onTimerInterrupt);
  283. Serial.println("Keyboard ready");
  284. }
  285. void loop(void)
  286. {
  287. // type key from serial
  288. if (!nextKeyReady && Serial.available() > 0) {
  289. long key = Serial.parseInt(SKIP_ALL);
  290. if (key != 0) {
  291. typeKey(key);
  292. }
  293. }
  294. /**/
  295. // type key from keyboard
  296. if (counter >= numbits) {
  297. processKbdByte(data);
  298. data = B0;
  299. counter = 0;
  300. }
  301. /**/
  302. /*/ auto-type test
  303. delay(500);
  304. typeKey('B');
  305. /**/
  306. }