-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTWIKeyboard.cpp
More file actions
117 lines (102 loc) · 2.46 KB
/
TWIKeyboard.cpp
File metadata and controls
117 lines (102 loc) · 2.46 KB
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
/*
* TWIKeyboard: Arduino Library for Akafugu TWI/I2C Keyboard and LED controller
* (C) 2011 Akafugu Corporation
*
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
*/
#include "TWIKeyboard.h"
#if defined(ARDUINO) && ARDUINO < 100
# define write send
# define read receive
#endif
TWIKeyboard::TWIKeyboard(int addr)
: m_addr(addr)
{
}
void TWIKeyboard::changeAddress(int new_addr)
{
Wire.beginTransmission(m_addr);
Wire.write(0x81); // change address
Wire.write(new_addr);
Wire.endTransmission();
}
void TWIKeyboard::clearLeds()
{
// Send clear command
Wire.beginTransmission(m_addr);
Wire.write(0x82); // clear
Wire.endTransmission();
}
void TWIKeyboard::begin()
{
// Clear the buffer
Wire.beginTransmission(m_addr);
Wire.write(0xFF);
Wire.write(0xFF);
Wire.write(0xFF);
Wire.write(0xFF);
Wire.write(0xFE); // reset
Wire.endTransmission();
}
/* --- */
void TWIKeyboard::setLed(uint8_t no, uint8_t brightness)
{
Wire.beginTransmission(m_addr);
Wire.write(0x83);
Wire.write(no);
Wire.write(brightness);
Wire.endTransmission();
}
void TWIKeyboard::pulseLed(uint8_t no, bool on)
{
Wire.beginTransmission(m_addr);
Wire.write(0x84);
Wire.write(no);
Wire.write((uint8_t)on);
Wire.endTransmission();
}
void TWIKeyboard::dimLed(uint8_t no, uint8_t brightness)
{
Wire.beginTransmission(m_addr);
Wire.write(0x85);
Wire.write(no);
Wire.write(brightness);
Wire.endTransmission();
}
/* ---- */
void TWIKeyboard::setKeyRepeat(uint8_t button, uint8_t mode)
{
Wire.beginTransmission(m_addr);
Wire.write(0x92);
Wire.write(button);
Wire.write(mode);
Wire.endTransmission();
}
uint8_t TWIKeyboard::getKeyUp()
{
uint8_t rdata = 0;
Wire.beginTransmission(m_addr);
Wire.write(0x90);
Wire.endTransmission();
Wire.requestFrom(m_addr, 1);
if (Wire.available()) rdata = Wire.read();
return rdata;
}
uint8_t TWIKeyboard::getKeyDown()
{
uint8_t rdata = 0;
Wire.beginTransmission(m_addr);
Wire.write(0x91);
Wire.endTransmission();
Wire.requestFrom(m_addr, 1);
if (Wire.available()) rdata = Wire.read();
return rdata;
}