mraa: change all existing code to use libmraa.
[contrib/upm.git] / src / tm1637 / tm1637.h
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 #pragma once
25
26 #include <string>
27 #include <mraa/aio.h>
28 #include <mraa/gpio.h>
29
30 #define SEG_A               0b00000001
31 #define SEG_B               0b00000010
32 #define SEG_C               0b00000100
33 #define SEG_D               0b00001000
34 #define SEG_E               0b00010000
35 #define SEG_F               0b00100000
36 #define SEG_G               0b01000000
37
38 #define TM1637_I2C_COMM1    0x40
39 #define TM1637_I2C_COMM2    0xC0
40 #define TM1637_I2C_COMM3    0x80
41
42 #define PULSE_LENGTH        50
43
44 #define HIGH                1
45 #define LOW                 0
46
47 namespace upm {
48
49 /**
50  * @brief C++ API for Seven segments screen
51  *
52  * This file defines the TM1637 C++ interface for lib4digitdisplay
53  *
54  * @snippet 4digitdisplay.cxx Interesting
55  *
56  *      A
57  *     ---
58  *  F |   | B
59  *     -G-
60  *  E |   | C
61  *     ---
62  *      D
63  *
64  */
65 class TM1637 {
66     public:
67         /**
68          * Instanciates a TM1637 object
69          *
70          * @param di data pin
71          * @param dcki clock pin
72          */
73         TM1637 (uint8_t di, uint8_t dcki);
74         /**
75          * TM1637 object destructor, this will close all used Gpio
76          * pins  (di and dcki)
77          */
78         ~TM1637 ();
79
80         /**
81          * Set the brightness of the seven segment display
82          *
83          * @param level The brightness level of leds
84          */
85         mraa_result_t setBrightness (uint8_t level);
86
87         /**
88          * Set the the segment screen data and number of segments
89          *
90          * @param segments[] data to write on the segments, each elemnt
91          * in array is segment
92          * @param length number of elements in segments array
93          * @param pos data writing offset
94          */
95         mraa_result_t setSegments (const uint8_t segments[], uint8_t length = 4, uint8_t pos = 0);
96
97         /**
98          * Write message on the screen.
99          *
100          * @param msg The message to be written on the sreen
101          */
102         mraa_result_t write (std::string msg);
103
104         /**
105          * Return name of the component
106          */
107         std::string name()
108         {
109             return m_name;
110         }
111
112     private:
113         mraa_result_t start();
114         mraa_result_t stop();
115         mraa_result_t writeByte (uint8_t value);
116         mraa_result_t pinMode (mraa_gpio_context ctx, gpio_dir_t mode);
117
118         mraa_gpio_context m_clkPinCtx;
119         mraa_gpio_context m_dataPinCtx;
120
121         std::string m_name;
122         uint8_t m_brightness;
123 };
124
125 }