8000 Tue Mar 7 23:29:15 CST 2017 · esp32vn/esp32-snippets@0160861 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0160861

Browse files
author
kolban
committed
Tue Mar 7 23:29:15 CST 2017
1 parent 3591e65 commit 0160861

File tree

15 files changed

+662
-61
lines changed

15 files changed

+662
-61
lines changed

cpp_utils/I2C.cpp

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,19 @@
1111
#include <sys/types.h>
1212
#include "I2C.h"
1313
#include "sdkconfig.h"
14+
#include <esp_log.h>
1415

16+
static char tag[] = "I2C.cpp";
17+
18+
static bool driverInstalled = false;
1519

1620
/**
1721
* @brief Create an instance of an %I2C object.
1822
*
1923
* @param[in] sdaPin The pin number used for the SDA line.
2024
* @param[in] sclPin The pin number used for the SCL line.
2125
*/
22-
I2C::I2C(int sdaPin, int sclPin) {
23-
i2c_config_t conf;
24-
conf.mode = I2C_MODE_MASTER;
25-
conf.sda_io_num = (gpio_num_t)sdaPin;
26-
conf.scl_io_num = (gpio_num_t)sclPin;
27-
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
28-
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
29-
conf.master.clk_speed = 100000;
30-
ESP_ERROR_CHECK(i2c_param_config(I2C_NUM_0, &conf));
31-
ESP_ERROR_CHECK(i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0));
26+
I2C::I2C() {
3227
directionKnown = false;
3328
address = 0;
3429
cmd = 0;
@@ -132,3 +127,19 @@ void I2C::read(uint8_t* bytes, size_t length, bool ack) {
132127
void I2C::stop() {
133128
ESP_ERROR_CHECK(i2c_master_stop(cmd));
134129
}
130+
131+
void I2C::init(gpio_num_t sdaPin, gpio_num_t sclPin) {
132+
ESP_LOGD(tag, ">> I2c::init");
133+
i2c_config_t conf;
134+
conf.mode = I2C_MODE_MASTER;
135+
conf.sda_io_num = sdaPin;
136+
conf.scl_io_num = sclPin;
137+
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
138+
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
139+
conf.master.clk_speed = 100000;
140+
ESP_ERROR_CHECK(i2c_param_config(I2C_NUM_0, &conf));
141+
if (!driverInstalled) {
142+
ESP_ERROR_CHECK(i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0));
143+
driverInstalled = true;
144+
}
145+
}

cpp_utils/I2C.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <stdint.h>
1111
#include <sys/types.h>
1212
#include <driver/i2c.h>
13+
#include <driver/gpio.h>
1314

1415

1516
/**
@@ -22,7 +23,7 @@ class I2C {
2223
bool directionKnown;
2324

2425
public:
25-
I2C(int sdaPin, int sclPin);
26+
I2C();
2627
void beginTransaction();
2728
void endTransaction();
2829
/**
@@ -35,6 +36,8 @@ class I2C {
3536
return address;
3637
}
3738

39+
void init(gpio_num_t sdaPin = DEFAULT_SDA_PIN, gpio_num_t sclPin = DEFAULT_CLK_PIN);
40+
3841
void read(uint8_t *bytes, size_t length, bool ack=true);
3942
void readByte(uint8_t *byte, bool ack=true);
4043

@@ -51,6 +54,8 @@ class I2C {
5154
void stop();
5255
void write(uint8_t byte, bool ack=true);
5356
void write(uint8_t *bytes, size_t length, bool ack=true);
57+
static const gpio_num_t DEFAULT_SDA_PIN = GPIO_NUM_25;
58+
static const gpio_num_t DEFAULT_CLK_PIN = GPIO_NUM_26;
5459
};
5560

5661
#endif /* MAIN_I2C_H_ */

cpp_utils/MAX7219.cpp

Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
1+
/*
2+
* MAX7219.cpp - A library for controling Leds with a MAX7219/MAX7221
3+
* Copyright (c) 2007 Eberhard Fahle
4+
*
5+
* Permission is hereby granted, free of charge, to any person
6+
* obtaining a copy of this software and associated documentation
7+
* files (the "Software"), to deal in the Software without
8+
* restriction, including without limitation the rights to use,
9+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the
11+
* Software is furnished to do so, subject to the following
12+
* conditions:
13+
*
14+
* This permission notice shall be included in all copies or
15+
* substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24+
* OTHER DEALINGS IN THE SOFTWARE.
25+
*/
26+
27+
/**
2 10000 8+
* Modified by Neil Kolban (2017) for ESP32 support.
29+
*/
30+
31+
#include "MAX7219.h"
32+
#include <math.h>
33+
34+
//the opcodes for the MAX7221 and MAX7219
35+
#define OP_NOOP 0x00
36+
#define OP_DIGIT0 0x01
37+
#define OP_DIGIT1 0x02
38+
#define OP_DIGIT2 0x03
39+
#define OP_DIGIT3 0x04
40+
#define OP_DIGIT4 0x05
41+
#define OP_DIGIT5 0x06
42+
#define OP_DIGIT6 0x07
43+
#define OP_DIGIT7 0x08
44+
#define OP_DECODEMODE 0x09
45+
#define OP_INTENSITY 0x0a
46+
#define OP_SCANLIMIT 0x0b
47+
#define OP_SHUTDOWN 0x0c
48+
#define OP_DISPLAYTEST 0x0f
49+
50+
/*
51+
* Segments to be switched on for characters and digits on
52+
* 7-Segment Displays
53+
*/
54+
const static uint8_t charTable[] = {
55+
0b01111110, 0b00110000, 0b01101101,
56+
0b01111001, 0b00110011, 0b01011011, 0b01011111, 0b01110000, 0b01111111,
57+
0b01111011, 0b01110111, 0b00011111, 0b00001101, 0b00111101, 0b01001111,
58+
0b01000111, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
59+
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
60+
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
61+
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
62+
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b10000000,
63+
0b00000001, 0b10000000, 0b00000000, 0b01111110, 0b00110000, 0b01101101,
64+
0b01111001, 0b00110011, 0b01011011, 0b01011111, 0b01110000, 0b01111111,
65+
0b01111011, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
66+
0b00000000, 0b00000000, 0b01110111, 0b00011111, 0b00001101, 0b00111101,
67+
0b01001111, 0b01000111, 0b00000000, 0b00110111, 0b00000000, 0b00000000,
68+
0b00000000, 0b00001110, 0b00000000, 0b00000000, 0b00000000, 0b01100111,
69+
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
70+
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
71+
0b00000000, 0b00000000, 0b00001000, 0b00000000, 0b01110111, 0b00011111,
72+
0b00001101, 0b00111101, 0b01001111, 0b01000111, 0b00000000, 0b00110111,
73+
0b00000000, 0b00000000, 0b00000000, 0b00001110, 0b00000000, 0b00010101,
74+
0b00011101, 0b01100111, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
75+
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
76+
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000 };
77+
78+
79+
MAX7219::MAX7219(SPI *spi, int numDevices) {
80+
assert(spi != nullptr);
81+
this->spi = spi;
82+
if (numDevices <= 0 || numDevices > 8) {
83+
numDevices = 8;
84+
}
85+
maxDevices = numDevices;
86+
87+
for (int i = 0; i < 64; i++) {
88+
status[i] = 0x00;
89+
}
90+
for (int i = 0; i < maxDevices; i++) {
91+
spiTransfer(i, OP_DISPLAYTEST, 0);
92+
//scanlimit is set to max on startup
93+
setScanLimit(7, i);
94+
//decode is done in source
95+
spiTransfer(i, OP_DECODEMODE, 0);
96+
clearDisplay(i);
97+
//we go into shutdown-mode on startup
98+
shutdown(true, i);
99+
}
100+
}
101+
102+
103+
int MAX7219::getDeviceCount() {
104+
return maxDevices;
105+
}
106+
107+
108+
void MAX7219::shutdown(bool b, int addr) {
109+
if (addr < 0 || addr >= maxDevices)
110+
return;
111+
if (b) {
112+
spiTransfer(addr, OP_SHUTDOWN, 0);
113+
}
114+
else {
115+
spiTransfer(addr, OP_SHUTDOWN, 1);
116+
}
117+
}
118+
119+
120+
void MAX7219::setScanLimit(int limit, int addr) {
121+
if (addr < 0 || addr >= maxDevices) {
122+
return;
123+
}
124+
if (limit >= 0 && limit < 8) {
125+
spiTransfer(addr, OP_SCANLIMIT, limit);
126+
}
127+
}
128+
129+
130+
void MAX7219::setIntensity(int intensity, int addr) {
131+
if (addr < 0 || addr >= maxDevices) {
132+
return;
133+
}
134+
if (intensity >= 0 && intensity < 16) {
135+
spiTransfer(addr, OP_INTENSITY, intensity);
136+
}
137+
}
138+
139+
140+
void MAX7219::clearDisplay(int addr) {
141+
int offset;
142+
143+
if (addr < 0 || addr >= maxDevices) {
144+
return;
145+
}
146+
offset = addr * 8;
147+
for (int i = 0; i < 8; i++) {
148+
status[offset + i] = 0;
149+
spiTransfer(addr, i + 1, status[offset + i]);
150+
}
151+
}
152+
153+
154+
void MAX7219::setLed(int row, int column, bool state, int addr) {
155+
int offset;
156+
uint8_t val = 0x00;
157+
158+
if (addr < 0 || addr >= maxDevices) {
159+
return;
160+
}
161+
if (row < 0 || row > 7 || column < 0 || column > 7) {
162+
return;
163+
}
164+
offset = addr * 8;
165+
val = 0b10000000 >> column;
166+
if (state) {
167+
status[offset + row] = status[offset + row] | val;
168+
}
169+
else {
170+
val = ~val;
171+
status[offset + row] = status[offset + row] & val;
172+
}
173+
spiTransfer(addr, row + 1, status[offset + row]);
174+
}
175+
176+
177+
void MAX7219::setRow(int row, uint8_t value, int addr) {
178+
int offset;
179+
if (addr < 0 || addr >= maxDevices) {
180+
return;
181+
}
182+
if (row < 0 || row > 7) {
183+
return;
184+
}
185+
offset = addr * 8;
186+
status[offset + row] = value;
187+
spiTransfer(addr, row + 1, status[offset + row]);
188+
}
189+
190+
191+
void MAX7219::setColumn(int col, uint8_t value, int addr) {
192+
uint8_t val;
193+
194+
if (addr < 0 || addr >= maxDevices) {
195+
return;
196+
}
197+
if (col < 0 || col > 7) {
198+
return;
199+
}
200+
for (int row = 0; row < 8; row++) {
201+
val = value >> (7 - row);
202+
val = val & 0x01;
203+
setLed(row, col, val, addr);
204+
}
205+
}
206+
207+
208+
void MAX7219::setDigit(int digit, uint8_t value, bool dp, int addr) {
209+
int offset;
210+
uint8_t v;
211+
212+
if (addr < 0 || addr >= maxDevices) {
213+
return;
214+
}
215+
if (digit < 0 || digit > 7 || value > 15) {
216+
return;
217+
}
218+
offset = addr * 8;
219+
v = charTable[value];
220+
if (dp) {
221+
v |= 0b10000000;
222+
}
223+
status[offset + digit] = v;
224+
spiTransfer(addr, digit + 1, v);
225+
}
226+
227+
228+
void MAX7219::setChar(int digit, char value, bool dp, int addr) {
229+
int offset;
230+
uint8_t index, v;
231+
232+
if (addr < 0 || addr >= maxDevices) {
233+
return;
234+
}
235+
if (digit < 0 || digit > 7) {
236+
return;
237+
}
238+
offset = addr * 8;
239+
index = (uint8_t) value;
240+
if (index > 127) {
241+
//no defined beyond index 127, so we use the space char
242+
index = 32;
243+
}
244+
v = charTable[index];
245+
if (dp) {
246+
v |= 0b10000000;
247+
}
248+
status[offset + digit] = v;
249+
spiTransfer(addr, digit + 1, v);
250+
}
251+
252+
253+
void MAX7219::spiTransfer(int addr, volatile uint8_t opcode, volatile uint8_t data) {
254+
//Create an array with the data to shift out
255+
int offset = addr * 2;
256+
int maxbytes = maxDevices * 2;
257+
258+
/* The array for shifting the data to the devices */
259+
uint8_t spidata[16];
260+
261+
for (int i = 0; i < maxbytes; i++) {
262+
spidata[i] = (uint8_t) 0;
263+
}
264+
//put our device data into the array
265+
spidata[offset] = opcode;
266+
spidata[offset + 1] = data;
267+
spi->transfer(spidata, maxbytes);
268+
}
269+
270+
void MAX7219::setNumber(uint32_t number, int addr) {
271+
//number = number % (uint32_t)pow(10, maxDevices);
272+
for (auto i=0; i<8; i++) {
273+
if (number == 0 && i > 0){
274+
setChar(i, ' ', addr);
275+
} else {
276+
setDigit(i, number%10, addr);
277+
number = number/10;
278+
}
279+
}
280+
}

0 commit comments

Comments
 (0)
0