Fix some dosctrings errors and trailing whitespaces
[contrib/upm.git] / src / tm1637 / tm1637.cxx
1 /*
2  * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3  * Copyright (c) 2014 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 <iostream>
26 #include <unistd.h>
27 #include <stdlib.h>
28
29 #include "tm1637.h"
30
31 using namespace upm;
32
33 const uint8_t digitToSegment[] = {
34      // XGFEDCBA
35     0b00111111,    // 0
36     0b00000110,    // 1
37     0b01011011,    // 2
38     0b01001111,    // 3
39     0b01100110,    // 4
40     0b01101101,    // 5
41     0b01111101,    // 6
42     0b00000111,    // 7
43     0b01111111,    // 8
44     0b01101111,    // 9
45     0b01110111,    // A
46     0b01111100,    // B
47     0b00111001,    // C
48     0b01000111,    // D
49     0b01111001,    // E
50     0b01110001     // F
51 };
52
53 TM1637::TM1637 (uint8_t di, uint8_t dcki) {
54     mraa_result_t error = MRAA_SUCCESS;
55     mraa_init();
56
57     // init clock context
58     m_clkPinCtx = mraa_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);
61         exit(1);
62     }
63     // init data context
64     m_dataPinCtx = mraa_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);
67         exit(1);
68     }
69
70     // set direction (out)
71     error = mraa_gpio_dir(m_clkPinCtx, MRAA_GPIO_IN);
72     if (error != MRAA_SUCCESS) {
73         mraa_result_print(error);
74     }
75
76     // set direction (out)
77     error = mraa_gpio_dir(m_dataPinCtx, MRAA_GPIO_IN);
78     if (error != MRAA_SUCCESS) {
79         mraa_result_print(error);
80     }
81
82     error = mraa_gpio_write (m_dataPinCtx, LOW);
83     error = mraa_gpio_write (m_clkPinCtx, LOW);
84 }
85
86 TM1637::~TM1637() {
87     mraa_result_t error = MRAA_SUCCESS;
88     error = mraa_gpio_close (m_dataPinCtx);
89     if (error != MRAA_SUCCESS) {
90         mraa_result_print(error);
91     }
92     error = mraa_gpio_close (m_clkPinCtx);
93     if (error != MRAA_SUCCESS) {
94         mraa_result_print(error);
95     }
96 }
97
98 mraa_result_t
99 TM1637::setBrightness (uint8_t level) {
100     m_brightness = level;
101 }
102
103 mraa_result_t
104 TM1637::setSegments (const uint8_t segments[], uint8_t length, uint8_t pos) {
105     start();
106     writeByte(TM1637_I2C_COMM1);
107     stop();
108
109     start();
110     writeByte(TM1637_I2C_COMM2 + (pos & 0x03));
111     for (uint8_t idx = 0; idx < length; idx++) {
112         writeByte(segments[idx]);
113     }
114     stop();
115
116     start();
117     writeByte(TM1637_I2C_COMM3 + (m_brightness & 0x0f));
118     stop();
119 }
120
121 mraa_result_t
122 TM1637::write (std::string msg) {
123     char leter = '\0';
124     uint8_t data[] = { 0x0, 0x0, 0x0, 0x0 };
125     for (uint8_t idx = 0; idx < msg.length(); idx++) {
126         leter = msg[idx];
127         if (idx < 4) {
128             data[idx] = digitToSegment[strtol(&leter, NULL, 16)];
129         }
130     }
131     setBrightness (0x0f);
132     setSegments(data);
133 }
134
135 mraa_result_t
136 TM1637::pinMode (mraa_gpio_context ctx, gpio_dir_t mode) {
137     mraa_result_t error = MRAA_SUCCESS;
138     error = mraa_gpio_dir(ctx, mode);
139     if (error != MRAA_SUCCESS) {
140         mraa_result_print(error);
141     }
142 }
143
144 mraa_result_t
145 TM1637::start() {
146     pinMode (m_dataPinCtx, MRAA_GPIO_OUT);
147     usleep(PULSE_LENGTH);
148 }
149
150 mraa_result_t
151 TM1637::stop() {
152     pinMode (m_dataPinCtx, MRAA_GPIO_OUT);
153     usleep(PULSE_LENGTH);
154     pinMode (m_clkPinCtx, MRAA_GPIO_IN);
155     usleep(PULSE_LENGTH);
156     pinMode (m_dataPinCtx, MRAA_GPIO_IN);
157     usleep(PULSE_LENGTH);
158 }
159
160 mraa_result_t
161 TM1637::writeByte(uint8_t value) {
162     for (uint8_t idx = 0; idx < 8; idx++) {
163         pinMode(m_clkPinCtx, MRAA_GPIO_OUT);
164         usleep(PULSE_LENGTH);
165         if (value & 0x01) {
166               pinMode(m_dataPinCtx, MRAA_GPIO_IN);
167         } else {
168               pinMode(m_dataPinCtx, MRAA_GPIO_OUT);
169         }
170         usleep(PULSE_LENGTH);
171
172         pinMode(m_clkPinCtx, MRAA_GPIO_IN);
173         usleep(PULSE_LENGTH);
174         value = value >> 1;
175     }
176
177     pinMode(m_clkPinCtx, MRAA_GPIO_OUT);
178     pinMode(m_dataPinCtx, MRAA_GPIO_IN);
179     usleep(PULSE_LENGTH);
180
181     pinMode(m_clkPinCtx, MRAA_GPIO_IN);
182     usleep(PULSE_LENGTH);
183
184     uint8_t ack = mraa_gpio_read (m_dataPinCtx);
185     if (ack == 0) {
186         pinMode(m_dataPinCtx, MRAA_GPIO_OUT);
187     } usleep(PULSE_LENGTH);
188
189     pinMode(m_clkPinCtx, MRAA_GPIO_OUT);
190     usleep(50);
191 }