1 /*
2  * Author: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
3  * Copyright (c) 2015 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 #pragma once
26 
27 #include <unistd.h>
28 #include <stdint.h>
29 #include <string>
30 #include <iostream>
31 
32 #include <mraa/gpio.h>
33 
34 
35 // TM1637-specific register addresses for writing all digits at a time
36 #define TM1637_ADDR    0x40
37 #define TM1637_REG     0xC0
38 #define TM1637_CMD     0x88
39 
40 // Display-specific values
41 #define M_DISPLAY_DIGITS  4
42 
43 namespace upm
44 {
45 
46 /**
47  * @brief TM1637 7-Segment Display library
48  * @defgroup tm1637 libupm-tm1637
49  * @ingroup seeed gpio display
50  */
51 
52 /**
53  * @library tm1637
54  * @sensor tm1637
55  * @comname TM1637 7-Segment Display
56  * @altname Grove 4-Digit Display
57  * @type display
58  * @man seeed
59  * @con gpio
60  *
61  * @brief API for the TM1637 7-Segment Display
62  *
63  * TM1637 is a display controller for LED-based 7-segment displays.
64  * It can be used to address and write data to multiple display digits. This
65  * driver is based on the Grove version of the TM1637 display that uses 4
66  * digits, thus making it ideal for clock displays, timers, counters, or even
67  * score displays in a two-player arcade game.
68  *
69  * @image html tm1637.jpeg
70  * @snippet tm1637.cxx Interesting
71  */
72 
73   class TM1637
74   {
75   public:
76       /**
77        * Enum for the memory-mapped GPIO
78        */
79       typedef enum {
80         NO  = 0,
81         YES = 1
82       } M_FAST_GPIO;
83       /**
84        * TM1637 constructor
85        *
86        * @param clk_pin Clock pin the sensor is connected to
87        * @param dio_pin Data pin the sensor is connected to
88        * @param bright Initial brightness, from 0 (dark) to 7 (bright) (default is 3)
89        * @param mmio Fast memory-mapped GPIO writes; default is yes
90        */
91       TM1637(int clk_pin, int dio_pin, int bright = 3, M_FAST_GPIO mmio = YES);
92       /**
93        * TM1637 destructor
94        */
95       ~TM1637();
96       /**
97        * Writes digits to the display in a 7-segment encoding
98        *
99        * @param digits Array of digits to send to the display
100        * @return 0 if successful, error code otherwise
101        */
102       mraa_result_t write(uint8_t *digits);
103       /**
104        * Writes digits to the display in a 7-segment encoding
105        *
106        * @param d List of multiple arguments to send to the display
107        * @return 0 if successful, error code otherwise
108        */
109       mraa_result_t write(int d, ...);
110       /**
111        * Writes a symbol (digit or letter) to the display in a specified index
112        *
113        * @param index 0-based index of the digit to change from the left
114        * @param symbol Digit or letter to display
115        * @return 0 if successful, error code otherwise
116        */
117       mraa_result_t writeAt(int index, char symbol);
118       /**
119        * Writes all the digits or letters to the display as a string
120        *
121        * @param digits String of symbols to display
122        * @return 0 if successful, error code otherwise
123        */
124       mraa_result_t write(std::string digits);
125       /**
126        * Toggles the colon between digits on the display
127        *
128        * @param value True to turn the colon on, false to turn it off
129        */
130       void setColon(bool value);
131       /**
132        * Controls the brightness of the display
133        *
134        * @param value Brightness, from 0 (darkest) to 7 (brightest)
135        */
136       void setBrightness(int value);
137 
138   private:
139       void i2c_start();
140       void i2c_stop();
141       void i2c_writeByte(uint8_t value);
142       void update();
143       uint8_t encode(char c);
144 
145       mraa_gpio_context m_clk, m_dio;
146       std::string m_name;
147       uint8_t m_digits[4];
148       uint8_t m_brightness;
149   };
150 }
151