4digitdisplay: rename to tm1637
[contrib/upm.git] / src / my9221 / my9221.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 "my9221.h"
30
31 using namespace upm;
32
33 MY9221::MY9221 (uint8_t di, uint8_t dcki) {
34     maa_result_t error = MAA_SUCCESS;
35     maa_init();
36
37     // init clock context
38     m_clkPinCtx = maa_gpio_init(dcki);
39     if (m_clkPinCtx == NULL) {
40         fprintf(stderr, "Are you sure that pin%d you requested is valid on your platform?", dcki);
41         exit(1);
42     }
43     // init data context
44     m_dataPinCtx = maa_gpio_init(di);
45     if (m_dataPinCtx == NULL) {
46         fprintf(stderr, "Are you sure that pin%d you requested is valid on your platform?", di);
47         exit(1);
48     }
49     
50     // set direction (out)
51     error = maa_gpio_dir(m_clkPinCtx, MAA_GPIO_OUT);
52     if (error != MAA_SUCCESS) {
53         maa_result_print(error);
54     }
55
56     // set direction (out)
57     error = maa_gpio_dir(m_dataPinCtx, MAA_GPIO_OUT);
58     if (error != MAA_SUCCESS) {
59         maa_result_print(error);
60     }
61 }
62
63 MY9221::~MY9221() {
64     maa_result_t error = MAA_SUCCESS;
65     error = maa_gpio_close (m_dataPinCtx);
66     if (error != MAA_SUCCESS) {
67         maa_result_print(error);
68     }
69     error = maa_gpio_close (m_clkPinCtx);
70     if (error != MAA_SUCCESS) {
71         maa_result_print(error);
72     }
73 }
74
75 maa_result_t
76 MY9221::setBarLevel (uint8_t level) {
77     if (level > 10) {
78         return MAA_ERROR_INVALID_PARAMETER;
79     }
80
81     send16bitBlock (CMDMODE);
82     for(uint8_t block_idx = 0; block_idx < 12; block_idx++) {
83         uint32_t state = (block_idx < level) ? BIT_HIGH : BIT_LOW;
84         send16bitBlock (state);
85     }
86     lockData ();
87 }
88
89 maa_result_t
90 MY9221::lockData () {
91     maa_result_t error = MAA_SUCCESS;
92     error = maa_gpio_write (m_dataPinCtx, LOW);
93     usleep(100);
94     
95     for(int idx = 0; idx < 4; idx++) {
96         error = maa_gpio_write (m_dataPinCtx, HIGH);
97         error = maa_gpio_write (m_dataPinCtx, LOW);
98     }
99 }
100
101 maa_result_t
102 MY9221::send16bitBlock (short data) {
103     maa_result_t error = MAA_SUCCESS;
104     for (uint8_t bit_idx = 0; bit_idx < MAX_BIT_PER_BLOCK; bit_idx++) {
105         uint32_t state = (data & 0x8000) ? HIGH : LOW;
106         error = maa_gpio_write (m_dataPinCtx, state);
107         state = maa_gpio_read (m_clkPinCtx);
108
109         if (state) {
110             state = LOW;
111         } else {
112             state = HIGH;
113         }
114
115         error = maa_gpio_write (m_clkPinCtx, state);
116
117         data <<= 1;
118     }
119 }