Skip to content

Commit 196efd5

Browse files
committed
Add Angle example
1 parent 12a5df1 commit 196efd5

File tree

3 files changed

+263
-0
lines changed

3 files changed

+263
-0
lines changed

examples/Unit/Angle8/Angle8.ino

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#include <M5Stack.h>
2+
#include <M5GFX.h>
3+
#include "M5_ANGLE8.h"
4+
5+
M5GFX display;
6+
M5Canvas canvas(&display);
7+
M5_ANGLE8 angle8;
8+
9+
uint32_t rgb_c = 0;
10+
11+
void print_adc_val(uint8_t i, uint16_t adc_v) {
12+
canvas.drawRect(0, i * 20, 200, 15, 1);
13+
canvas.fillRect(0, i * 20, map(adc_v, 0, 4096, 0, 200), 15, 1);
14+
canvas.setCursor(215, i * 20);
15+
canvas.setTextSize(1);
16+
canvas.printf("CH:%d ADC: %d", i, adc_v);
17+
}
18+
19+
// ADC 12 Bit
20+
void TaskADC12(uint16_t delay_t) {
21+
canvas.createSprite(display.width(), 160);
22+
canvas.fillSprite(0);
23+
uint16_t adc_v = 0;
24+
for (uint8_t i = 0; i < ANGLE8_TOTAL_ADC; i++) {
25+
adc_v = angle8.getAnalogInput(i, _12bit);
26+
print_adc_val(i, adc_v);
27+
}
28+
canvas.pushSprite(0, 45);
29+
vTaskDelay(delay_t);
30+
}
31+
32+
// Breathing RGBLED
33+
void TaskRGBLED_1(uint8_t br, uint16_t delay_t) {
34+
canvas.createSprite(display.width(), 35);
35+
rgb_c = 0;
36+
canvas.fillSprite(0);
37+
for (uint8_t m = 0; m < 12; m++) {
38+
rgb_c = 0x03 << (m * 2) | rgb_c;
39+
for (uint8_t i = 0; i < ANGLE8_TOTAL_LED; i++) {
40+
angle8.setLEDColor(i, rgb_c, br);
41+
vTaskDelay(25);
42+
}
43+
canvas.setCursor(0, 0);
44+
canvas.printf("COLOR: 0x%X", rgb_c);
45+
canvas.pushSprite(0, 205);
46+
vTaskDelay(delay_t);
47+
}
48+
}
49+
50+
void TaskRGBLED_2(uint8_t br, uint16_t delay_t) {
51+
canvas.createSprite(display.width(), 35);
52+
rgb_c = 0;
53+
canvas.fillSprite(0);
54+
for (uint8_t m = 0; m < 6; m++) {
55+
rgb_c = 0xF0 << (m * 4);
56+
for (uint8_t i = 0; i < ANGLE8_TOTAL_LED; i++) {
57+
angle8.setLEDColor(i, rgb_c, br);
58+
vTaskDelay(25);
59+
}
60+
canvas.setCursor(0, 0);
61+
canvas.printf("COLOR: 0x%X", rgb_c);
62+
canvas.pushSprite(0, 205);
63+
vTaskDelay(delay_t);
64+
}
65+
}
66+
67+
void TaskRGBLED_3(uint32_t color, uint8_t br, uint16_t delay_t) {
68+
canvas.createSprite(display.width(), 35);
69+
rgb_c = 0;
70+
canvas.fillSprite(0);
71+
for (uint8_t i = 0; i < ANGLE8_TOTAL_LED; i++) {
72+
angle8.setLEDColor(i, color, br);
73+
vTaskDelay(delay_t);
74+
}
75+
canvas.setCursor(0, 0);
76+
canvas.printf("COLOR: 0x%X", rgb_c);
77+
canvas.pushSprite(0, 205);
78+
}
79+
80+
void setup() {
81+
M5.begin();
82+
display.begin();
83+
canvas.setColorDepth(1); // mono color
84+
canvas.setFont(&fonts::efontCN_14);
85+
while (!angle8.begin(ANGLE8_I2C_ADDR)) {
86+
Serial.println("angle8 Connect Error");
87+
M5.Lcd.print("angle8 Connect Error");
88+
delay(100);
89+
}
90+
canvas.createSprite(display.width(), 45);
91+
canvas.setPaletteColor(1, GREENYELLOW);
92+
canvas.fillSprite(0);
93+
canvas.setTextSize(1);
94+
canvas.drawString("UNIT ANGLE8", 120, 5);
95+
canvas.drawString("FW VERSION: " + String(angle8.getVersion()), 10, 25);
96+
canvas.pushSprite(0, 0);
97+
}
98+
99+
void loop() {
100+
M5.update();
101+
if (M5.BtnA.wasReleased()) {
102+
TaskRGBLED_3(0xffffff, 100, 2);
103+
} else if (M5.BtnB.wasReleased()) {
104+
TaskRGBLED_3(0xffffff, 50, 2);
105+
} else if (M5.BtnC.wasReleased()) {
106+
TaskRGBLED_3(0xffffff, 10, 2);
107+
}
108+
if (angle8.getDigitalInput())
109+
TaskADC12(100);
110+
else
111+
TaskRGBLED_2(5, 100);
112+
}

examples/Unit/Angle8/M5_ANGLE8.cpp

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#include "M5_ANGLE8.h"
2+
3+
/*! @brief Initialize the ANGLE8.
4+
@return True if the init was successful, otherwise false.. */
5+
bool M5_ANGLE8::begin(uint8_t addr) {
6+
_wire = &Wire;
7+
_addr = addr;
8+
_wire->begin();
9+
delay(10);
10+
_wire->beginTransmission(_addr);
11+
uint8_t error = _wire->endTransmission();
12+
if (error == 0) {
13+
return true;
14+
} else {
15+
return false;
16+
}
17+
}
18+
19+
/*! @brief Write a certain length of data to the specified register address.
20+
@return True if the write was successful, otherwise false.. */
21+
bool M5_ANGLE8::writeBytes(uint8_t addr, uint8_t reg, uint8_t *buffer,
22+
uint8_t length) {
23+
_wire->beginTransmission(addr);
24+
_wire->write(reg);
25+
for (uint8_t i = 0; i < length; i++) {
26+
_wire->write(*(buffer + i));
27+
}
28+
_wire->endTransmission();
29+
return true;
30+
// return false;
31+
}
32+
33+
/*! @brief Read a certain length of data to the specified register address.
34+
@return True if the read was successful, otherwise false.. */
35+
bool M5_ANGLE8::readBytes(uint8_t addr, uint8_t reg, uint8_t *buffer,
36+
uint8_t length) {
37+
uint8_t index = 0;
38+
_wire->beginTransmission(addr);
39+
_wire->write(reg);
40+
_wire->endTransmission();
41+
if (_wire->requestFrom(addr, length)) {
42+
for (uint8_t i = 0; i < length; i++) {
43+
buffer[index++] = _wire->read();
44+
}
45+
return true;
46+
}
47+
return false;
48+
}
49+
50+
/*! @brief Set the addr of device.
51+
@return True if the set was successful, otherwise false.. */
52+
bool M5_ANGLE8::setDeviceAddr(uint8_t addr) {
53+
if (writeBytes(_addr, ANGLE8_ADDRESS_REG, &addr, 1)) {
54+
_addr = addr;
55+
return true;
56+
} else {
57+
return false;
58+
}
59+
}
60+
61+
/*! @brief Get the Version of Firmware.
62+
@return Firmware version */
63+
uint8_t M5_ANGLE8::getVersion() {
64+
uint8_t data = 0;
65+
readBytes(_addr, ANGLE8_FW_VERSION_REG, &data, 1);
66+
return data;
67+
}
68+
69+
/*! @brief Set the color of led lights.
70+
@return True if the set was successful, otherwise false.. */
71+
bool M5_ANGLE8::setLEDColor(uint8_t ch, uint32_t color, uint8_t bright) {
72+
if (ch > ANGLE8_TOTAL_LED) return false;
73+
uint8_t data[4] = {0};
74+
data[0] = (color >> 16) & 0xff;
75+
data[1] = (color >> 8) & 0xff;
76+
data[2] = color & 0xff;
77+
data[3] = bright & 0xff;
78+
uint8_t reg = ch * 4 + ANGLE8_RGB_24B_REG;
79+
return writeBytes(_addr, reg, data, 4);
80+
}
81+
82+
/*! @brief Get digital singal input.
83+
@return True if the read was successful, otherwise false.. */
84+
bool M5_ANGLE8::getDigitalInput() {
85+
uint8_t data;
86+
uint8_t reg = ANGLE8_DIGITAL_INPUT_REG;
87+
if (readBytes(_addr, reg, &data, 1)) {
88+
return data;
89+
}
90+
return 0;
91+
}
92+
93+
/*! @brief Get analog singal input.
94+
@return True if the read was successful, otherwise false.. */
95+
uint16_t M5_ANGLE8::getAnalogInput(uint8_t ch, angle8_analog_read_mode_t bit) {
96+
if (bit == _8bit) {
97+
uint8_t data;
98+
uint8_t reg = ch + ANGLE8_ANALOG_INPUT_8B_REG;
99+
if (readBytes(_addr, reg, &data, 1)) {
100+
return data;
101+
}
102+
} else {
103+
uint8_t data[2];
104+
uint8_t reg = ch * 2 + ANGLE8_ANALOG_INPUT_12B_REG;
105+
if (readBytes(_addr, reg, data, 2)) {
106+
return (data[1] << 8) | data[0];
107+
}
108+
}
109+
return 0;
110+
}

examples/Unit/Angle8/M5_ANGLE8.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
#ifndef _M5_ANGLE8_H_
3+
#define _M5_ANGLE8_H_
4+
5+
#include <Arduino.h>
6+
#include <Wire.h>
7+
#include "pins_arduino.h"
8+
9+
#define ANGLE8_I2C_ADDR 0x43
10+
#define ANGLE8_ANALOG_INPUT_12B_REG 0x00
11+
#define ANGLE8_ANALOG_INPUT_8B_REG 0x10
12+
#define ANGLE8_DIGITAL_INPUT_REG 0x20
13+
#define ANGLE8_RGB_24B_REG 0x30
14+
15+
#define ANGLE8_FW_VERSION_REG 0xFE
16+
#define ANGLE8_ADDRESS_REG 0xFF
17+
18+
#define ANGLE8_TOTAL_LED 9
19+
#define ANGLE8_TOTAL_ADC 8
20+
21+
typedef enum { _8bit = 0, _12bit } angle8_analog_read_mode_t;
22+
23+
class M5_ANGLE8 {
24+
private:
25+
uint8_t _addr;
26+
TwoWire *_wire;
27+
uint8_t _sda;
28+
uint8_t _scl;
29+
bool writeBytes(uint8_t addr, uint8_t reg, uint8_t *buffer, uint8_t length);
30+
bool readBytes(uint8_t addr, uint8_t reg, uint8_t *buffer, uint8_t length);
31+
32+
public:
33+
bool begin(uint8_t addr = ANGLE8_I2C_ADDR);
34+
bool setDeviceAddr(uint8_t addr);
35+
bool setLEDColor(uint8_t ch, uint32_t color, uint8_t bright);
36+
bool getDigitalInput();
37+
uint16_t getAnalogInput(uint8_t ch, angle8_analog_read_mode_t bit = _8bit);
38+
uint8_t getVersion();
39+
};
40+
41+
#endif

0 commit comments

Comments
 (0)