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.

242 lines
5.1 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. }
  130. char translateKeyToChar(int key) {
  131. return key; //m[key];
  132. if (sizeof(m) <= key) {
  133. return 0;
  134. }
  135. return m[key];
  136. }
  137. void printChar(char keyChar) {
  138. Serial.print("'"); Serial.print(keyChar); Serial.print("' ("); Serial.print(int(keyChar)); Serial.println(")");
  139. }
  140. void processKbdByte(int data) {
  141. int key = data;
  142. if (modConsoleLog) {
  143. Serial.print("Key: <"); Serial.print(int(key)); Serial.print("> ");
  144. }
  145. char keyChar = translateKeyToChar(key);
  146. #ifdef KEYBOARD
  147. Keyboard.press(keyChar);
  148. delay(10);
  149. Keyboard.release(keyChar);
  150. #endif
  151. typeKey(keyChar);
  152. if (modConsoleLog) {
  153. Serial.print("Press: ");
  154. printChar(keyChar);
  155. }
  156. }
  157. // ----------------------
  158. // Input and Output Merge
  159. // ----------------------
  160. void onTimerInterrupt()
  161. {
  162. onSlaveClockInterrupt();
  163. if (slaveClockStep == 0) {
  164. onHostClockCycle();
  165. }
  166. slaveClockStep = (slaveClockStep + 1) % slaveClockDivider;
  167. }
  168. // ----
  169. // Main
  170. // ----
  171. void setup(void)
  172. {
  173. Serial.begin(9600);
  174. setupKeyMapping();
  175. pinMode(pinData, OUTPUT);
  176. pinMode(dataPin, INPUT);
  177. pinMode(outPin, OUTPUT);
  178. pinMode(clockPin, OUTPUT);
  179. pinMode(pinStatus, INPUT_PULLUP);
  180. digitalWrite(pinData, HIGH);
  181. digitalWrite(outPin, LOW);
  182. attachInterrupt(digitalPinToInterrupt(pinStatus), onHostStatusChange, CHANGE);
  183. Timer1.initialize(timerDelay);
  184. Timer1.attachInterrupt(onTimerInterrupt);
  185. Serial.println("Keyboard ready");
  186. }
  187. void loop(void)
  188. {
  189. // type key from serial
  190. if (!nextKeyReady && Serial.available() > 0) {
  191. long key = Serial.parseInt(SKIP_ALL);
  192. if (key != 0) {
  193. typeKey(key);
  194. }
  195. }
  196. /**/
  197. // type key from keyboard
  198. if (counter >= numbits) {
  199. processKbdByte(data);
  200. data = B0;
  201. counter = 0;
  202. }
  203. /**/
  204. /*/ auto-type test
  205. delay(500);
  206. typeKey('B');
  207. /**/
  208. }