2 * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3 * Copyright (c) 2014 Intel Corporation.
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:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
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.
33 const uint8_t digitToSegment[] = {
53 TM1637::TM1637 (uint8_t di, uint8_t dcki) {
54 maa_result_t error = MAA_SUCCESS;
58 m_clkPinCtx = maa_gpio_init(dcki);
59 if (m_clkPinCtx == NULL) {
60 fprintf(stderr, "Are you sure that pin%d you requested is valid on your platform?", dcki);
64 m_dataPinCtx = maa_gpio_init(di);
65 if (m_dataPinCtx == NULL) {
66 fprintf(stderr, "Are you sure that pin%d you requested is valid on your platform?", di);
70 // set direction (out)
71 error = maa_gpio_dir(m_clkPinCtx, MAA_GPIO_IN);
72 if (error != MAA_SUCCESS) {
73 maa_result_print(error);
76 // set direction (out)
77 error = maa_gpio_dir(m_dataPinCtx, MAA_GPIO_IN);
78 if (error != MAA_SUCCESS) {
79 maa_result_print(error);
82 error = maa_gpio_write (m_dataPinCtx, LOW);
83 error = maa_gpio_write (m_clkPinCtx, LOW);
87 maa_result_t error = MAA_SUCCESS;
88 error = maa_gpio_close (m_dataPinCtx);
89 if (error != MAA_SUCCESS) {
90 maa_result_print(error);
92 error = maa_gpio_close (m_clkPinCtx);
93 if (error != MAA_SUCCESS) {
94 maa_result_print(error);
99 TM1637::setBrightness (uint8_t level) {
100 m_brightness = level;
104 TM1637::setSegments (const uint8_t segments[], uint8_t length, uint8_t pos) {
106 writeByte(TM1637_I2C_COMM1);
110 writeByte(TM1637_I2C_COMM2 + (pos & 0x03));
111 for (uint8_t idx = 0; idx < length; idx++) {
112 writeByte(segments[idx]);
117 writeByte(TM1637_I2C_COMM3 + (m_brightness & 0x0f));
122 TM1637::write (std::string msg) {
124 uint8_t data[] = { 0x0, 0x0, 0x0, 0x0 };
125 for (uint8_t idx = 0; idx < msg.length(); idx++) {
128 data[idx] = digitToSegment[strtol(&leter, NULL, 16)];
131 setBrightness (0x0f);
136 TM1637::pinMode (maa_gpio_context ctx, gpio_dir_t mode) {
137 maa_result_t error = MAA_SUCCESS;
138 error = maa_gpio_dir(ctx, mode);
139 if (error != MAA_SUCCESS) {
140 maa_result_print(error);
146 pinMode (m_dataPinCtx, MAA_GPIO_OUT);
147 usleep(PULSE_LENGTH);
152 pinMode (m_dataPinCtx, MAA_GPIO_OUT);
153 usleep(PULSE_LENGTH);
154 pinMode (m_clkPinCtx, MAA_GPIO_IN);
155 usleep(PULSE_LENGTH);
156 pinMode (m_dataPinCtx, MAA_GPIO_IN);
157 usleep(PULSE_LENGTH);
161 TM1637::writeByte(uint8_t value) {
162 for (uint8_t idx = 0; idx < 8; idx++) {
163 pinMode(m_clkPinCtx, MAA_GPIO_OUT);
164 usleep(PULSE_LENGTH);
166 pinMode(m_dataPinCtx, MAA_GPIO_IN);
168 pinMode(m_dataPinCtx, MAA_GPIO_OUT);
170 usleep(PULSE_LENGTH);
172 pinMode(m_clkPinCtx, MAA_GPIO_IN);
173 usleep(PULSE_LENGTH);
177 pinMode(m_clkPinCtx, MAA_GPIO_OUT);
178 pinMode(m_dataPinCtx, MAA_GPIO_IN);
179 usleep(PULSE_LENGTH);
181 pinMode(m_clkPinCtx, MAA_GPIO_IN);
182 usleep(PULSE_LENGTH);
184 uint8_t ack = maa_gpio_read (m_dataPinCtx);
186 pinMode(m_dataPinCtx, MAA_GPIO_OUT);
187 } usleep(PULSE_LENGTH);
189 pinMode(m_clkPinCtx, MAA_GPIO_OUT);