Erriez DHT22 library for Arduino  1.2.0
AM2302/AM2303 DHT22 temperature and humidity sensor library for Arduino
ErriezTM1637.h
Go to the documentation of this file.
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2018-2020 Erriez
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
79 #ifndef ERRIEZ_TM1637_H_
80 #define ERRIEZ_TM1637_H_
81 
82 #include <Arduino.h>
83 
84 // Commands
85 #define TM1637_CMD_DATA 0x40
86 #define TM1637_CMD_CTRL 0x80
87 #define TM1637_CMD_ADDR 0xc0
88 
89 // Data command bits
90 #define TM1637_DATA_WRITE 0x00
91 #define TM1637_DATA_READ_KEYS 0x02
92 #define TM1637_DATA_AUTO_INC_ADDR 0x00
93 #define TM1637_DATA_FIXED_ADDR 0x04
94 
95 // Control command bits
96 #define TM1637_CTRL_PULSE_1_16 0x00
97 #define TM1637_CTRL_PULSE_2_16 0x01
98 #define TM1637_CTRL_PULSE_4_16 0x02
99 #define TM1637_CTRL_PULSE_10_16 0x03
100 #define TM1637_CTRL_PULSE_11_16 0x04
101 #define TM1637_CTRL_PULSE_12_16 0x05
102 #define TM1637_CTRL_PULSE_13_16 0x06
103 #define TM1637_CTRL_PULSE_14_16 0x07
104 #define TM1637_CTRL_DISPLAY_OFF 0x00
105 #define TM1637_CTRL_DISPLAY_ON 0x08
106 
107 #define TM1637_NUM_GRIDS 6
108 
109 #ifdef __AVR
110 #define TM1637_CLK_LOW() { *portOutputRegister(_clkPort) &= ~_clkBit; }
111 #define TM1637_CLK_HIGH() { *portOutputRegister(_clkPort) |= _clkBit; }
112 #define TM1637_CLK_INPUT() { *portModeRegister(_clkPort) &= ~_clkBit; }
113 #define TM1637_CLK_OUTPUT() { *portModeRegister(_clkPort) |= _clkBit; }
114 #define TM1637_DIO_LOW() { *portOutputRegister(_dioPort) &= ~_dioBit; }
115 #define TM1637_DIO_HIGH() { *portOutputRegister(_dioPort) |= _dioBit; }
116 #define TM1637_DIO_INPUT() { *portModeRegister(_dioPort) &= ~_dioBit; }
117 #define TM1637_DIO_OUTPUT() { *portModeRegister(_dioPort) |= _dioBit; }
118 #define TM1637_DIO_READ() ( *portInputRegister(_dioPort) & _dioBit )
119 #else
120 #define TM1637_CLK_LOW() { digitalWrite(_clkPin, LOW); }
121 #define TM1637_CLK_HIGH() { digitalWrite(_clkPin, HIGH); }
122 #define TM1637_CLK_INPUT() { pinMode(_clkPin, INPUT); }
123 #define TM1637_CLK_OUTPUT() { pinMode(_clkPin, OUTPUT); }
124 #define TM1637_DIO_LOW() { digitalWrite(_dioPin, LOW); }
125 #define TM1637_DIO_HIGH() { digitalWrite(_dioPin, HIGH); }
126 #define TM1637_DIO_INPUT() { pinMode(_dioPin, INPUT); }
127 #define TM1637_DIO_OUTPUT() { pinMode(_dioPin, OUTPUT); }
128 #define TM1637_DIO_READ() ( digitalRead(_dioPin) )
129 #endif
130 
131 #if F_CPU >= 20000000UL
132 #define TM1637_PIN_DELAY() { delayMicroseconds(1); }
133 #else
134 #define TM1637_PIN_DELAY()
135 #endif
136 
137 
141 class TM1637
142 {
143 public:
144  TM1637(uint8_t clkPin, uint8_t dioPin, bool displayOn=true, uint8_t brightness=5);
145 
146  virtual void begin();
147  virtual void end();
148  virtual void displayOn();
149  virtual void displayOff();
150  virtual void setBrightness(uint8_t brightness);
151  virtual void clear();
152  virtual void writeData(uint8_t address, uint8_t data);
153  virtual void writeData(uint8_t address, const uint8_t *buf, uint8_t len);
154  virtual uint8_t getKeys();
155 
156 protected:
157 #ifdef __AVR
158  uint8_t _clkPort;
159  uint8_t _dioPort;
160 
161  uint8_t _clkBit;
162  uint8_t _dioBit;
163 
164 #else
165  uint8_t _clkPin;
166  uint8_t _dioPin;
167 #endif
168 
169  bool _displayOn;
170  uint8_t _brightness;
171 
172  virtual void writeDisplayControl();
173  virtual void writeCommand(uint8_t cmd);
174  virtual void start();
175  virtual void stop();
176  virtual void writeByte(uint8_t data);
177  virtual uint8_t readByte();
178 };
179 
180 #endif // ERRIEZ_TM1637_H_
uint8_t _dioPin
Data pin.
Definition: ErriezTM1637.h:166
virtual void writeByte(uint8_t data)
Write byte to TM1637.
virtual void writeData(uint8_t address, uint8_t data)
Write display register.
virtual void start()
Generate start condition.
virtual uint8_t getKeys()
Get key states.
virtual uint8_t readByte()
Read byte from TM1637.
virtual void end()
Release TM1637 pins.
uint8_t _brightness
Display brightness for display control register.
Definition: ErriezTM1637.h:170
uint8_t _clkPin
Clock pin.
Definition: ErriezTM1637.h:165
TM1637 class.
Definition: ErriezTM1637.h:141
virtual void writeDisplayControl()
Write display control register.
virtual void writeCommand(uint8_t cmd)
Write command to TM1637.
virtual void displayOff()
Turn display off.
virtual void clear()
Turn all LED&#39;s off.
virtual void stop()
Generate stop condition.
bool _displayOn
Display on and off status for display control register.
Definition: ErriezTM1637.h:169
TM1637(uint8_t clkPin, uint8_t dioPin, bool displayOn=true, uint8_t brightness=5)
TM1637 constructor.
virtual void setBrightness(uint8_t brightness)
Set brightness LED&#39;s.
virtual void displayOn()
Turn Display on.
virtual void begin()
Initialize TM1637 controller.