Advertisement
Guest User

KH930 Keyboard Emulator

a guest
Dec 4th, 2010
1,051
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.92 KB | None | 0 0
  1. /*
  2.   KnitterKey
  3.   Types on the matrix keyboard of a Brother Electroknit KH930.
  4.    
  5.   You may use this code for anything you damned well please, so
  6.   long as it's neighborly and--given the opportunity--you share a
  7.   beer or Club Mate with Travis Goodspeed.
  8.  
  9.   Each of the digital I/O pins should be connected to the bases of
  10.   the column 547 NPN transistors through 1.2K resistors.  The analog I/O
  11.   pins should be connected to the row transistors in a similar manner.
  12.   Collectors should face the row side, while emitters face the column side.
  13.   The emitters of the row transistors should connect to the collectors of
  14.   the column transistors.
  15.  
  16.   N.B., that this code was written as a quick and dirty hack.  Like all
  17.   such hacks, you're probably better off rewriting it than trying to adapt
  18.   it to your needs.
  19.  */
  20.  
  21. //Configurations stuff:
  22. #define default_track 1
  23. #define default_pattern 903
  24. #define default_offset -1
  25.  
  26.  
  27. //! Presses a key by its code.  Use keypress() instead.
  28. void key(int row, int col){
  29.   int rows[4]={A0, A1, A2, A3};
  30.  
  31.   digitalWrite(rows[row], HIGH);
  32.   digitalWrite(col, HIGH);
  33.   delay(500);
  34.   letoff();
  35.   delay(500);
  36. }
  37.  
  38. //! Clears the input.
  39. void letoff(){
  40.   digitalWrite(A0, LOW);
  41.   digitalWrite(A1, LOW);
  42.   digitalWrite(A2, LOW);
  43.   digitalWrite(A3, LOW);
  44.   for(int i=0;i<10;i++)
  45.     digitalWrite(i, LOW);
  46. }
  47.  
  48. #define C 0x80
  49. #define CR 0x81
  50. #define CE 0x82
  51. #define STEP 0x83
  52. #define VAR1 0x84
  53. #define VAR2 0x85
  54. #define VAR3 0x86
  55. #define VAR4 0x87
  56. #define VAR5 0x88
  57. #define VAR6 0x89
  58. #define KHC 0x89
  59. #define VAR7 0x8A
  60. #define KRC 0x8A
  61.  
  62. #define CHECK 0x8B
  63. #define MEMO 0x8C
  64. #define INPT 0x8D
  65. #define LEFT 0x8E
  66. #define RIGHT 0x8F
  67. #define SOUND 0x90
  68. #define BLACKSQUARE 0x91
  69. #define WHITESQUARE 0x92
  70. #define M 'M'
  71. #define START 0x93
  72. #define SEL1 0x94
  73. #define SEL2 0x95
  74. #define S 'S'
  75. #define R 'R'
  76. #define YELLOW 0x96
  77. #define GREEN 0x97
  78.  
  79. //! keypress
  80. void keypress(char c){
  81.   switch(c){
  82.   case C:
  83.     key(1,1);
  84.     break;
  85.   case CR:
  86.     key(1,8); //CR
  87.     break;
  88.   case CE:
  89.     key(1,7); //CE
  90.     break;
  91.   case 1: case '1':
  92.     key(0,8); //1
  93.     break;
  94.   case 2: case '2':
  95.     key(0,7); //2
  96.     break;
  97.   case 3: case '3':
  98.     key(0,6); //3
  99.     break;
  100.   case 4: case '4':
  101.     key(0,5);
  102.     break;
  103.   case 5: case '5':
  104.     key(0,4);
  105.     break;
  106.   case 6: case '6':
  107.     key(0,3);
  108.     break;
  109.   case 7: case '7':
  110.     key(0,2);
  111.     break;
  112.   case 8: case '8':
  113.     key(0,1);
  114.     break;
  115.   case 9: case '9':
  116.     key(0,0);
  117.     break;
  118.   case 0: case '0':
  119.     key(0,9);
  120.     break;
  121.   case STEP:
  122.     key(1,9);
  123.     break;
  124.   case VAR1:
  125.     key(3,9);
  126.     break;
  127.   case VAR2:
  128.     key(3,8);
  129.     break;
  130.   case VAR3:
  131.     key(3,6);
  132.     break;
  133.   case VAR4:
  134.     key(3,5);
  135.     break;
  136.   case VAR5:
  137.     key(3,7);
  138.     break;
  139.   case VAR6: //case KHC:
  140.     key(3,4);
  141.     break;
  142.   case VAR7: //case KRC:
  143.     key(3,3);
  144.     break;
  145.   case CHECK:
  146.     key(2,8);
  147.     break;
  148.   case MEMO:
  149.     key(2,6);
  150.     break;
  151.   case INPT:
  152.     key(2,9);
  153.     break;
  154.   case LEFT:
  155.     key(2,1);
  156.     break;
  157.   case RIGHT:
  158.     key(2,2);
  159.     break;
  160.   case SOUND:
  161.     key(2,7);
  162.     break;
  163.   case BLACKSQUARE:
  164.     key(2,3);
  165.     break;
  166.   case WHITESQUARE:
  167.     key(2,4);
  168.     break;
  169.   case M:
  170.     key(1,2);
  171.     break;
  172.   case START:
  173.     key(3,0);
  174.     break;
  175.   case SEL1:
  176.     key(1,0);
  177.     break;
  178.   case SEL2:
  179.     key(2,0);
  180.     break;
  181.   case 'S':
  182.     key(3,1);
  183.     break;
  184.   case 'R':
  185.     key(3,2);
  186.     break;
  187.   case YELLOW:
  188.     key(1,6);
  189.     break;
  190.   case GREEN:
  191.     key(1,5);
  192.     break;
  193.   }
  194. }
  195.  
  196. //! Tries all numbers to test keyboard.
  197. void selftest(){
  198.   keypress(CE);
  199.   typeint(123);
  200.   keypress(CE);
  201.   typeint(456);
  202.   keypress(CE);
  203.   typeint(789);
  204.   keypress(CE);
  205.   typeint(101);
  206.   keypress(CE);
  207.  
  208. }
  209.  
  210. //! Loads a track of data from the floppy disk.
  211. void loaddisk(int track){
  212.   //Now load file from disk.
  213.   //Track should probably be 1.
  214.   keypress(CE);
  215.   typeint(551);
  216.   delay(1000); //none
  217.   keypress(STEP);
  218.   delay(2000);
  219.   keypress(track);
  220.   keypress(STEP);
  221.   delay(10000);
  222.  
  223.   //while(1);
  224. }
  225.  
  226. void typeint(int number){
  227.   int
  228.     hundreds=(number/100)%10,
  229.     tens=(number/10)%10,
  230.     ones=number%10;
  231.    keypress(hundreds);
  232.    keypress(tens);
  233.    keypress(ones);
  234.    delay(1000);
  235. }
  236.  
  237. void printpattern(int pattern, int offset){
  238.   //Custom patterns begin at 0x900 in BCD.
  239.   //Offset is positive for green, negative for yellow.
  240.   //Offsets of more than 9 will cause trouble.
  241.  
  242.   //keypress(KHC);  //For the love of god, start with this on.
  243.   keypress(CE);
  244.   keypress(STEP);//delay(1000);
  245.   keypress(STEP);//delay(1000);
  246.   keypress(CE);//delay(1000);
  247.  
  248.   typeint(pattern);
  249.  
  250.   keypress(STEP);
  251.   delay(1000);
  252.  
  253.   //Do the offset.
  254.   keypress(CE);
  255.   if(offset>=0){
  256.     keypress(GREEN);
  257.     typeint(offset);
  258.   }else{
  259.     keypress(YELLOW);
  260.     typeint(abs(offset));
  261.   }
  262.  
  263.   keypress(STEP);
  264.   delay(1000);
  265.  
  266.   while(1);
  267. }
  268.  
  269. void setup() {                
  270.   // initialize the digital pin as an output.
  271.   // Pin 13 has an LED connected on most Arduino boards:
  272.   pinMode(13, OUTPUT);    
  273.  
  274.   pinMode(A0, OUTPUT);
  275.   pinMode(A1, OUTPUT);
  276.   pinMode(A2, OUTPUT);
  277.   pinMode(A3, OUTPUT);
  278.  
  279.   for(int i=0;i<10;i++)
  280.     pinMode(i, OUTPUT);
  281.  
  282.   letoff();
  283.  
  284.   digitalWrite(13, HIGH);   // set the LED on
  285.   //Startup delay in case we're just being plugged in.
  286.  
  287.   //delay(1000);
  288.   //selftest();
  289.   delay(3000);
  290.  
  291.   //Load a pattern from the emulated disk.
  292.   loaddisk(default_track);
  293.  
  294.   //Set up pattern 903 centered at 1 yellow.
  295.   printpattern(default_pattern,default_offset);
  296.   digitalWrite(13, LOW);   // cut the LED to show that we're ready to print
  297. }
  298.  
  299. //! Main loop.
  300. void loop() {
  301.   //Everything is done in setup() after a reset.
  302.   //This loop only exists for the hell of it.
  303.  
  304.   digitalWrite(13, LOW);    // set the LED off
  305.   delay(1000);
  306.  
  307.   digitalWrite(13, HIGH);   // set the LED on
  308.   delay(1000);
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement