1 /*
2 * Author: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
3 * Copyright (c) 2015 Intel Corporation.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #include <string>
26 #include <stdexcept>
27 #include "tm1637.h"
28 #include <stdarg.h>
29
30 const uint8_t m_brkt[2] = {0x39, 0x0f};
31 const uint8_t m_nums[10] = {0x3f, 0x06, 0x5b, 0x4f, 0x66,
32 0x6d, 0x7d, 0x07, 0x7f, 0x6f};
33 const uint8_t m_char[26] = {0x77, 0x7c, 0x39, 0x5e, 0x79,
34 0x71, 0x6f, 0x76, 0x30, 0x1e,
35 0x00, 0x38, 0x00, 0x00, 0x5c,
36 0x73, 0x67, 0x50, 0x5b, 0x78,
37 0x3e, 0x1c, 0x00, 0x00, 0x6e,
38 0x5b};
39
40 using namespace std;
41 using namespace upm;
42
TM1637(int clk_pin,int dio_pin,int bright,M_FAST_GPIO mmio)43 upm::TM1637::TM1637(int clk_pin, int dio_pin, int bright, M_FAST_GPIO mmio) {
44
45 if((m_clk = mraa_gpio_init(clk_pin)) == NULL){
46 throw std::invalid_argument(std::string(__FUNCTION__) +
47 ": mraa_gpio_init(clk) failed, invalid pin?");
48 return;
49 }
50
51 if((m_dio = mraa_gpio_init(dio_pin)) == NULL){
52 throw std::invalid_argument(std::string(__FUNCTION__) +
53 ": mraa_gpio_init(dio) failed, invalid pin?");
54 return;
55 }
56
57 mraa_gpio_dir(m_clk, MRAA_GPIO_OUT);
58 mraa_gpio_dir(m_dio, MRAA_GPIO_OUT);
59
60 // Let the resistors pull the lines high
61 mraa_gpio_mode(m_clk, MRAA_GPIO_PULLUP);
62 mraa_gpio_mode(m_dio, MRAA_GPIO_PULLUP);
63
64 if(mmio){
65 if(mraa_gpio_use_mmaped(m_clk, 1) != MRAA_SUCCESS ||
66 mraa_gpio_use_mmaped(m_dio, 1) != MRAA_SUCCESS){
67 throw std::runtime_error(std::string(__FUNCTION__) +
68 ": mraa_gpio_use_mmaped() failed");
69 return;
70 }
71 }
72
73 mraa_gpio_write(m_clk, 0);
74 mraa_gpio_write(m_dio, 0);
75
76 for (int i = 0; i < M_DISPLAY_DIGITS; i++) {
77 m_digits[i] = 0x00;
78 }
79 setBrightness(bright);
80 }
~TM1637()81 upm::TM1637::~TM1637() {
82 for (int i = 0; i < M_DISPLAY_DIGITS; i++) {
83 m_digits[i] = 0x00;
84 }
85 update();
86
87 mraa_gpio_close(m_clk);
88 mraa_gpio_close(m_dio);
89 }
write(uint8_t * digits)90 mraa_result_t upm::TM1637::write(uint8_t *digits) {
91 for (int i = 0; i < M_DISPLAY_DIGITS; i++) {
92 m_digits[i] = digits[i];
93 }
94 update();
95 return MRAA_SUCCESS;
96 }
write(int d,...)97 mraa_result_t upm::TM1637::write(int d, ...) {
98 va_list args;
99 va_start(args, d);
100 m_digits[0] = (uint8_t)d;
101
102 for (int i = 1; i < M_DISPLAY_DIGITS; i++) {
103 m_digits[i] = (uint8_t)va_arg(args, int);
104 d++;
105 }
106 va_end(args);
107 update();
108 return MRAA_SUCCESS;
109 }
writeAt(int index,char symbol)110 mraa_result_t upm::TM1637::writeAt(int index, char symbol) {
111 if(index < 0 || index >= M_DISPLAY_DIGITS){
112 cerr << "TM1637: invalid index in " << __FUNCTION__ << endl;
113 return MRAA_ERROR_INVALID_PARAMETER;
114 }
115 m_digits[index] = encode(symbol);
116 update();
117 return MRAA_SUCCESS;
118 }
write(std::string digits)119 mraa_result_t upm::TM1637::write(std::string digits) {
120 int len = digits.length();
121 if( len > M_DISPLAY_DIGITS){
122 len = M_DISPLAY_DIGITS;
123 }
124 for (int i = 0; i < len; i++) {
125 m_digits[i] = encode(digits[i]);
126 }
127 update();
128 return MRAA_SUCCESS;
129 }
setColon(bool value)130 void upm::TM1637::setColon(bool value) {
131 if(value){
132 m_digits[1] |= 0x80;
133 }
134 else{
135 m_digits[1] &= 0x7f;
136 }
137 update();
138 }
setBrightness(int value)139 void upm::TM1637::setBrightness(int value) {
140 m_brightness = value & 0x07;
141 update();
142 }
i2c_start()143 void upm::TM1637::i2c_start() {
144 mraa_gpio_write(m_clk, 1);
145 mraa_gpio_write(m_dio, 1);
146 mraa_gpio_write(m_dio, 0);
147 }
i2c_stop()148 void upm::TM1637::i2c_stop() {
149 mraa_gpio_write(m_clk, 0);
150 mraa_gpio_write(m_dio, 0);
151 mraa_gpio_write(m_clk, 1);
152 mraa_gpio_write(m_dio, 1);
153 }
i2c_writeByte(uint8_t value)154 void upm::TM1637::i2c_writeByte(uint8_t value) {
155 for(uint8_t i = 0; i < 8; i++)
156 {
157 mraa_gpio_write(m_clk, 0);
158 if(value & 0x01)
159 mraa_gpio_write(m_dio, 1);
160 else
161 mraa_gpio_write(m_dio, 0);
162 value >>= 1;
163 mraa_gpio_write(m_clk, 1);
164 }
165
166 // Ack clock without skew, TM1637 is fast enough
167 mraa_gpio_write(m_clk, 0);
168 mraa_gpio_write(m_clk, 1);
169 mraa_gpio_write(m_clk, 0);
170 }
update()171 void upm::TM1637::update() {
172 i2c_start();
173 i2c_writeByte(TM1637_ADDR);
174 i2c_stop();
175
176 i2c_start();
177 i2c_writeByte(TM1637_REG);
178 for (int i = 0; i < M_DISPLAY_DIGITS; i++) {
179 i2c_writeByte(m_digits[i]);
180 }
181 i2c_stop();
182
183 i2c_start();
184 i2c_writeByte(TM1637_CMD | m_brightness);
185 i2c_stop();
186 }
encode(char c)187 uint8_t upm::TM1637::encode(char c) {
188 if(c >= '0' && c <= '9')
189 return m_nums[(int)c - 48];
190 if(c >= 'a' && c <= 'z')
191 return m_char[(int)c - 97];
192 if(c >= 'A' && c <= 'Z')
193 return m_char[(int)c - 65];
194 if(c == '[')
195 return m_brkt[0];
196 if(c == ']')
197 return m_brkt[1];
198 if(c == '(' || c == ')')
199 return m_brkt[(int)c - 40];
200 if(c == '-')
201 return 0x40;
202 if(c == '_')
203 return 0x08;
204 if(c == '}')
205 return 0x70;
206 if(c == '{')
207 return 0x46;
208 return 0x00;
209 }
210