63004c7bd80897a0e49f2a4403d313521c4df7bd
[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     mraa_result_t error = MRAA_SUCCESS;
35     mraa_init();
36
37     // init clock context
38     m_clkPinCtx = mraa_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 = mraa_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 = mraa_gpio_dir(m_clkPinCtx, MRAA_GPIO_OUT);
52     if (error != MRAA_SUCCESS) {
53         mraa_result_print(error);
54     }
55
56     // set direction (out)
57     error = mraa_gpio_dir(m_dataPinCtx, MRAA_GPIO_OUT);
58     if (error != MRAA_SUCCESS) {
59         mraa_result_print(error);
60     }
61 }
62
63 MY9221::~MY9221() {
64     mraa_result_t error = MRAA_SUCCESS;
65     error = mraa_gpio_close (m_dataPinCtx);
66     if (error != MRAA_SUCCESS) {
67         mraa_result_print(error);
68     }
69     error = mraa_gpio_close (m_clkPinCtx);
70     if (error != MRAA_SUCCESS) {
71         mraa_result_print(error);
72     }
73 }
74
75 mraa_result_t
76 MY9221::setBarLevel (uint8_t level, bool direction) {
77     if (level > 10) {
78         return MRAA_ERROR_INVALID_PARAMETER;
79     }
80
81     send16bitBlock (CMDMODE);
82     if (direction) {
83         level += 3;
84         for(uint8_t block_idx = 12; block_idx > 0; block_idx--) {
85             uint32_t state = (block_idx < level) ? BIT_HIGH : BIT_LOW;
86             send16bitBlock (state);
87         }
88     } else {
89         for(uint8_t block_idx = 0; block_idx < 12; block_idx++) {
90             uint32_t state = (block_idx < level) ? BIT_HIGH : BIT_LOW;
91             send16bitBlock (state);
92         }
93     }
94     return lockData ();
95 }
96
97 mraa_result_t
98 MY9221::lockData () {
99     mraa_result_t error = MRAA_SUCCESS;
100     error = mraa_gpio_write (m_dataPinCtx, LOW);
101     usleep(100);
102     
103     for(int idx = 0; idx < 4; idx++) {
104         error = mraa_gpio_write (m_dataPinCtx, HIGH);
105         error = mraa_gpio_write (m_dataPinCtx, LOW);
106     }
107 }
108
109 mraa_result_t
110 MY9221::send16bitBlock (short data) {
111     mraa_result_t error = MRAA_SUCCESS;
112     for (uint8_t bit_idx = 0; bit_idx < MAX_BIT_PER_BLOCK; bit_idx++) {
113         uint32_t state = (data & 0x8000) ? HIGH : LOW;
114         error = mraa_gpio_write (m_dataPinCtx, state);
115         state = mraa_gpio_read (m_clkPinCtx);
116
117         if (state) {
118             state = LOW;
119         } else {
120             state = HIGH;
121         }
122
123         error = mraa_gpio_write (m_clkPinCtx, state);
124
125         data <<= 1;
126     }
127 }