Commit | Line | Data |
---|---|---|
3d0ec3ff TM |
1 | /* |
2 | * PS2 pinout cinskej kabel: data cerveny (18), clock bily (19), 5V cerny, GND zluty | |
3 | */ | |
4 | ||
5 | #define SOFTSERIAL | |
6 | ||
7 | #ifndef SOFTSERIAL | |
8 | #include <ps2dev.h> | |
9 | PS2dev keyboard(19, 18); //clock, data | |
10 | #else | |
11 | #include <SoftwareSerial.h> | |
12 | //#include <NewSoftSerial.h> | |
13 | SoftwareSerial mySerial(18, 19); | |
14 | #endif | |
15 | ||
16 | #include <LedControl.h> | |
17 | ||
18 | LedControl lc=LedControl(16,14,15,2); //datain, clk, load, number of chips | |
19 | unsigned char dispbuf[32]; | |
20 | ||
21 | #include <Keypad.h> | |
22 | ||
23 | int keyledPin = 3; | |
24 | ||
25 | const byte ROWS = 4; //four rows | |
26 | const byte COLS = 3; //four columns | |
27 | //define the cymbols on the buttons of the keypads | |
28 | char hexaKeys[ROWS][COLS] = { | |
29 | {'1','2','3'}, | |
30 | {'4','5','6'}, | |
31 | {'7','8','9'}, | |
32 | {'*','0','#'} | |
33 | }; | |
34 | byte rowPins[ROWS] = {10, 9, 8, 7}; //connect to the row pinouts of the keypad | |
35 | byte colPins[COLS] = {4, 5, 6}; //connect to the column pinouts of the keypad | |
36 | ||
37 | //initialize an instance of class NewKeypad | |
38 | Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); | |
39 | ||
40 | char scancodes[] = {0x45, 0x16, 0x1E, 0x26, 0x25, 0x2E, 0x36, 0x3D, 0x3E, 0x46, 0x7C, 0x7C}; //Scancodes for numbers 0-9, *, # | |
41 | ||
42 | void cleardisp(char val) { | |
43 | for(int i = 0; i<8; i++) dispbuf[i] = val; | |
44 | } | |
45 | ||
46 | void drawdisp(int shift) { | |
47 | lc.clearDisplay(0); | |
48 | for(int i = 0; i<8; i++) { | |
49 | if(shift) dispbuf[i] = dispbuf[i+1]; | |
50 | if(dispbuf[i] != '_') lc.setDigit(0,7-i,dispbuf[i],false); //addr, digit, value, decimalpoint | |
51 | } | |
52 | } | |
53 | ||
54 | void intro() { | |
55 | cleardisp('_'); | |
56 | for(int i = 0; i<100; i++) { | |
57 | analogWrite(keyledPin, i); | |
58 | if(i >= 20*1) dispbuf[3] = dispbuf[4] = 0; | |
59 | if(i >= 20*2) dispbuf[2] = dispbuf[5] = 0; | |
60 | if(i >= 20*3) dispbuf[1] = dispbuf[6] = 0; | |
61 | if(i >= 20*4) dispbuf[0] = dispbuf[7] = 0; | |
62 | drawdisp(0); | |
63 | delay(20); | |
64 | } | |
65 | } | |
66 | ||
67 | void outro() { | |
68 | cleardisp(0); | |
69 | for(int i = 100; i>=0; i--) { | |
70 | analogWrite(keyledPin, i); | |
71 | if(i <= 20*1) dispbuf[3] = dispbuf[4] = '_'; | |
72 | if(i <= 20*2) dispbuf[2] = dispbuf[5] = '_'; | |
73 | if(i <= 20*3) dispbuf[1] = dispbuf[6] = '_'; | |
74 | if(i <= 20*4) dispbuf[0] = dispbuf[7] = '_'; | |
75 | drawdisp(0); | |
76 | delay(20); | |
77 | } | |
78 | } | |
79 | ||
80 | ||
81 | void setup(){ | |
82 | /* | |
83 | The MAX72XX is in power-saving mode on startup, | |
84 | we have to do a wakeup call | |
85 | */ | |
86 | lc.shutdown(0,false); | |
87 | /* Set the brightness to a medium values */ | |
88 | lc.setIntensity(0,15); //0 - 15 | |
89 | /* and clear the display */ | |
90 | lc.clearDisplay(0); | |
91 | ||
92 | intro(); | |
93 | cleardisp('_'); drawdisp(0); | |
94 | ||
95 | #ifndef SOFTSERIAL | |
96 | // send the keyboard start up | |
97 | keyboard.keyboard_init(); | |
98 | #else | |
99 | mySerial.begin(4800); | |
100 | #endif | |
101 | ||
102 | Serial.begin(9600); | |
103 | ||
104 | ||
105 | ||
106 | pinMode(LED_BUILTIN, OUTPUT); | |
107 | } | |
108 | ||
109 | void loop(){ | |
110 | unsigned char leds; | |
111 | #ifndef SOFTSERIAL | |
112 | if(keyboard.keyboard_handle(&leds)) { | |
113 | //Serial.print('LEDS'); | |
114 | //Serial.print(leds, HEX); | |
115 | digitalWrite(LED_BUILTIN, leds); | |
116 | } | |
117 | #endif | |
118 | ||
119 | char customKey = customKeypad.getKey(); | |
120 | ||
121 | if (customKey){ | |
122 | Serial.println(customKey); | |
123 | //analogWrite(keyledPin, random(0, 255)); | |
124 | analogWrite(keyledPin, 255); | |
125 | unsigned char numkey = customKey-0x30; | |
126 | if(numkey < 10) { | |
127 | #ifndef SOFTSERIAL | |
128 | //Send PS2 | |
129 | keyboard.keyboard_mkbrk(scancodes[numkey]); | |
130 | #else | |
131 | mySerial.print(customKey); | |
132 | #endif | |
133 | ||
134 | //Single digit | |
135 | //lc.setDigit(0,7,numkey,false); //addr, digit, value, decimalpoint | |
136 | ||
137 | //Scroll out display | |
138 | dispbuf[8] = numkey; | |
139 | drawdisp(1); | |
140 | } | |
141 | } | |
142 | ||
143 | if (customKey == '*') { | |
144 | //Send PS2 | |
145 | #ifndef SOFTSERIAL | |
146 | keyboard.keyboard_mkbrk(scancodes[10]); | |
147 | #endif | |
148 | ||
149 | //analogWrite(keyledPin, 0); | |
150 | outro(); | |
151 | ||
152 | cleardisp('_'); | |
153 | drawdisp(0); | |
154 | } | |
155 | ||
156 | if (customKey == '#') { | |
157 | #ifndef SOFTSERIAL | |
158 | keyboard.keyboard_mkbrk(0x5A); //enter | |
159 | #endif | |
160 | } | |
161 | } |