/
attiny85_IR_keyboard_no_serial.ino
133 lines (123 loc) · 5.87 KB
/
attiny85_IR_keyboard_no_serial.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* Attiny85 IR USB receiver by Venum.
*
* I couldn't make this without the help of:
* David A. Mellis for his attiny core https://github.com/damellis/attiny)
* Rowdy Dog Software for their library TinyTuner https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/arduino-tiny/TinyTuner-0003.zip
* Rowdy Dog Software for their tiny bootloader https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/arduino-tiny/arduino-tiny-0150-0020.zip
* Rancidbacon (http://rancidbacon.com/) for his arduino port of the v-usb library (https://code.google.com/archive/p/vusb-for-arduino/downloads)
* seejaydee for his IR library for tiny cores found on the comments of the instructable https://www.instructables.com/id/Attiny-IR-library/ code on https://drive.google.com/open?id=0B_w9z88wnDtFNHlqYU5sWEZuV0U
*
* To add new key_codes, check out the pdf http://www.usb.org/developers/hidpage/Hut1_12v2.pdf and look out for the section "Keyboard/Keypad Page" page 53 then use the numbers
* in the column Usage ID (Dec) to associate your remote buttons to keyboard codes.
* Edit the file UsbKeyboard.h and add new defines to those already existent
*/
#include "UsbKeyboard.h"
#include "tiny_IRremote.h"
#define RECV_PIN 1
IRrecv irrecv(RECV_PIN);
decode_results results;
int lastStroke=0;
void setup() {
// uncommment the statement below if you use tinytuner to calibrate the internal clock
// leave it commented if you use an external 16Mhz crystal
//OSCCAL = 0x4B; // Oscillator callibration value from Tiny Tuner http://goo.gl/J71XN
pinMode(RECV_PIN, INPUT);
digitalWrite(RECV_PIN, HIGH);
// Disable timer0 as it can interfere with USB timing
// millis and delay won't work with this timer down, but delayMicrosencods() will
TIMSK&=!(1<<TOIE0); // Disabling timer0
//TIMSK&=!(1<<TOIE1); // Disable timer1
// Clear interrupts while performing time-critical operations
cli();
// Force re-enumeration so the host will detect us
usbDeviceDisconnect();
delayMs(250);
usbDeviceConnect();
usbInit();
// Set interrupts again
sei();
irrecv.enableIRIn(); // Start the receiver
}
void loop() {
UsbKeyboard.update(); // needs to be called at least once every 10 ms
// or the computer may think that the device
// has stopped working, and give driver errors
if (irrecv.decode(&results)) {
irrecv.resume(); // Receive the next value
// WARNING: THIS CODES ARE JUST EXAMPLES THAT WORK WITH MY REMOTE, YOU MAY NEED TO CHANGE THEM FOR YOURS.
if(results.value==3429 || results.value==1381) { // Volume up -
UsbKeyboard.sendKeyStroke(KEY_VOL_UP);
} else if(results.value==3427 || results.value==1379) { // Volume down -
UsbKeyboard.sendKeyStroke(KEY_VOL_DOWN);
} else if(results.value==3406 || results.value==1358) { // arrow left
if(lastStroke!=results.value) // we don't want for this button to hit twice
UsbKeyboard.sendKeyStroke(KEY_ARROW_LEFT);
} else if(results.value==3408 || results.value==1360) { // arrow right
if(lastStroke!=results.value)
UsbKeyboard.sendKeyStroke(KEY_ARROW_RIGHT);
} else if(results.value==3405 || results.value==1357) { // arrow up
if(lastStroke!=results.value)
UsbKeyboard.sendKeyStroke(KEY_ARROW_UP);
} else if(results.value==3409 || results.value==1361) { // arrow down
if(lastStroke!=results.value)
UsbKeyboard.sendKeyStroke(KEY_ARROW_DOWN);
} else if(results.value==3416 || results.value==1368) { // mute
if(lastStroke!=results.value)
UsbKeyboard.sendKeyStroke(KEY_MUTE);
} else if(results.value==3407 || results.value==1359) { // space
if(lastStroke!=results.value)
UsbKeyboard.sendKeyStroke(KEY_SPACE);
} else if(results.value==3394 || results.value==1346) { // Full screen (f)
if(lastStroke!=results.value)
UsbKeyboard.sendKeyStroke(KEY_F);
} else if(results.value==3451 || results.value==1403) { // Play
if(lastStroke!=results.value)
UsbKeyboard.sendKeyStroke(KEY_SPACE);
} else if(results.value==3452 || results.value==1404) { // Stop
if(lastStroke!=results.value)
UsbKeyboard.sendKeyStroke(KEY_STOP);
} else if(results.value==3455 || results.value==1407 || results.value==3430 || results.value==1382) { // Next
if(lastStroke!=results.value)
UsbKeyboard.sendKeyStroke(KEY_STOP);
} else if(results.value==3453 || results.value==1405 || results.value==3428 || results.value==1380) { // Prev
if(lastStroke!=results.value)
UsbKeyboard.sendKeyStroke(KEY_NEXT);
} else if(results.value==3404 || results.value==1356) { // 0
if(lastStroke!=results.value)
UsbKeyboard.sendKeyStroke(KEY_0);
} else if(results.value==3395 || results.value==1347) { // 1
if(lastStroke!=results.value)
UsbKeyboard.sendKeyStroke(KEY_1);
} else if(results.value==3396 || results.value==1348) { // 2
if(lastStroke!=results.value)
UsbKeyboard.sendKeyStroke(KEY_2);
} else if(results.value==3397 || results.value==1349) { // 3
if(lastStroke!=results.value)
UsbKeyboard.sendKeyStroke(KEY_3);
} else if(results.value==3398 || results.value==1350) { // 4
if(lastStroke!=results.value)
UsbKeyboard.sendKeyStroke(KEY_4);
} else if(results.value==3399 || results.value==1351) { // 5
if(lastStroke!=results.value)
UsbKeyboard.sendKeyStroke(KEY_5);
} else if(results.value==3400 || results.value==1352) { // 6
if(lastStroke!=results.value)
UsbKeyboard.sendKeyStroke(KEY_6);
} else if(results.value==3401 || results.value==1353) { // 7
if(lastStroke!=results.value)
UsbKeyboard.sendKeyStroke(KEY_7);
} else if(results.value==3402 || results.value==1354) { // 8
if(lastStroke!=results.value)
UsbKeyboard.sendKeyStroke(KEY_8);
} else if(results.value==3403 || results.value==1355) { // 9
if(lastStroke!=results.value)
UsbKeyboard.sendKeyStroke(KEY_9);
}
lastStroke=results.value;
}
}
void delayMs(unsigned int ms) {
for( int i=0; i<ms; i++ ) {
delayMicroseconds(1000);
}
}