Skip to content

Commit b0443d4

Browse files
authored
Merge pull request #4 from huamiaoXY/main
Adapt to the M5Unified
2 parents 9c6eaa1 + 4e5888a commit b0443d4

6 files changed

Lines changed: 81 additions & 74 deletions

File tree

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1-
# MiniEncoderC
1+
# M5Hat-Mini-EncoderC
22

33
## Overview
44

5-
Contains case programs of M5Stack MiniEncoderC.
5+
### SKU:U157
6+
7+
Arduino library to control M5Stack Hat-Mini-EncoderC.
8+
9+
## Required Libraries
10+
11+
- [Hat Mini EncoderC](https://docs.m5stack.com/en/hat/MiniEncoderC%20Hat)
12+
- [M5Unified (optional)](https://github.com/m5stack/M5Unified)
613

714
## Related Link
815

9-
- [Docment - MiniEncoderC](https://docs.m5stack.com/en/hat/MiniEncoderC%20Hat)
1016
- [I2C Protocol](https://github.com/m5stack/M5Unit-MiniEncoderC/blob/main/docs/MiniEncoderC_I2C_Protocol.pdf)
1117

1218
## License
1319

14-
[MiniEncoderC - MIT](LICENSE)
20+
[M5HatMiniEncoderC - MIT](LICENSE)
21+

examples/getValue/getValue.ino

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,70 @@
11

2-
#include <M5StickCPlus.h>
3-
#include "Unit_MiniEncoderC.h"
2+
#include "M5Unified.h"
3+
#include "M5HatMiniEncoderC.h"
44

5-
TFT_eSprite tftSprite = TFT_eSprite(&M5.Lcd);
6-
UNIT_MINIENCODERC sensor;
5+
M5Canvas canvas(&M5.Lcd);
6+
M5HatMiniEncoderC encoder;
77

8-
uint16_t delay_time = 0;
9-
int32_t last_value = 0;
8+
int32_t last_value = 0;
109

1110
uint8_t i2c_address = 0;
1211

1312
void page_get_encoder(void) {
14-
int32_t encoder_value = sensor.getEncoderValue();
15-
bool btn_status = sensor.getButtonStatus();
13+
int32_t encoder_value = encoder.getEncoderValue();
14+
bool btn_status = encoder.getButtonStatus();
1615
Serial.println(encoder_value);
1716
if (last_value != encoder_value) {
1817
if (last_value > encoder_value) {
19-
sensor.setLEDColor(1, 0x000011);
18+
encoder.setLEDColor(0x000011); // Blue
2019
} else {
21-
sensor.setLEDColor(2, 0x110000);
20+
encoder.setLEDColor(0x110000); // Red
2221
}
2322
last_value = encoder_value;
2423
} else {
25-
// sensor.setLEDColor(0, 0x001100);
24+
// encoder.setLEDColor(0x001100);
2625
}
2726
if (!btn_status) {
28-
sensor.setLEDColor(0, 0x001100);
27+
encoder.setLEDColor(0x001100);
2928
}
30-
tftSprite.fillRect(0, 0, 135, 90, BLACK);
31-
tftSprite.setTextColor(WHITE);
32-
tftSprite.drawString("BTN:" + String(btn_status), 10, 10);
33-
tftSprite.drawString("ENCODER:", 10, 40);
34-
tftSprite.setCursor(10, 65);
35-
tftSprite.setTextColor(GREEN);
36-
tftSprite.printf("%d", encoder_value);
37-
tftSprite.setTextColor(WHITE);
38-
tftSprite.drawString("btnA:", 10, 120);
39-
tftSprite.setTextColor(ORANGE);
40-
tftSprite.drawString("get inc", 10, 150);
41-
tftSprite.setTextColor(WHITE);
42-
tftSprite.drawString("btnB:", 10, 180);
43-
tftSprite.setTextColor(ORANGE);
44-
tftSprite.drawString("reset", 10, 210);
45-
tftSprite.pushSprite(0, 0);
29+
canvas.fillRect(0, 0, 135, 90, BLACK);
30+
canvas.setTextColor(WHITE);
31+
canvas.drawString("BTN:" + String(btn_status), 10, 10);
32+
canvas.drawString("ENCODER:", 10, 40);
33+
canvas.setCursor(10, 65);
34+
canvas.setTextColor(GREEN);
35+
canvas.printf("%d", encoder_value);
36+
canvas.setTextColor(WHITE);
37+
canvas.drawString("btnA:", 10, 120);
38+
canvas.setTextColor(ORANGE);
39+
canvas.drawString("get inc", 10, 150);
40+
canvas.setTextColor(WHITE);
41+
canvas.drawString("btnB:", 10, 180);
42+
canvas.setTextColor(ORANGE);
43+
canvas.drawString("reset", 10, 210);
44+
canvas.pushSprite(0, 0);
4645
}
4746

4847
void get_inc_value(void) {
49-
int32_t inc_value = sensor.getIncrementValue();
48+
int32_t inc_value = encoder.getIncrementValue();
5049
Serial.printf("Inc:%d", inc_value);
5150
}
5251

5352
void setup() {
5453
M5.begin(); // Init M5Stack. 初始化M5Stack
55-
sensor.begin(&Wire, MINIENCODERC_ADDR, 0, 26, 100000UL);
56-
tftSprite.setRotation(1);
57-
tftSprite.createSprite(135, 240);
58-
tftSprite.setTextSize(2);
59-
delay_time = 20;
60-
i2c_address = sensor.getI2CAddress();
61-
// sensor.setEncoderValue(97900);
54+
encoder.begin(&Wire, MiniEncoderC_ADDR, 0, 26, 100000UL);
55+
canvas.setRotation(0);
56+
canvas.createSprite(135, 240);
57+
canvas.setTextSize(2);
58+
i2c_address = encoder.getI2CAddress();
59+
// encoder.setEncoderValue(0);
6260
}
6361

6462
void loop() {
6563
M5.update();
6664
if (M5.BtnA.wasPressed()) {
6765
get_inc_value();
6866
} else if (M5.BtnB.wasPressed()) {
69-
sensor.resetCounter();
67+
encoder.resetCounter();
7068
}
7169
page_get_encoder();
7270
}

library.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
{
2-
"name": "MiniEncoderC",
3-
"description": "Library for M5Stack MiniEncoderC",
4-
"keywords": "M5Stack MiniEncoderC",
2+
"name": "M5Hat-Mini-EncoderC",
3+
"description": "M5Stack Library of Hat Mini EncoderC",
4+
"keywords": "M5Stack, Hat, Mini EncoderC, MiniEncoderC",
55
"authors": {
66
"name": "M5Stack",
77
"url": "http://www.m5stack.com"
88
},
99
"repository": {
1010
"type": "git",
11-
"url": "https://github.com/m5stack/M5Unit-MiniEncoderC.git"
11+
"url": "https://github.com/m5stack/M5Hat-Mini-EncoderC.git"
1212
},
1313
"version": "0.0.1",
1414
"frameworks": "arduino",
15-
"platforms": "espressif32"
15+
"platforms": "espressif32",
16+
"headers": "M5HatMiniEncoderC.h"
1617
}

library.properties

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
name=MiniEncoderC
1+
name=M5Hat-Mini-EncoderC
22
version=0.0.1
33
author=M5Stack
44
maintainer=M5Stack
5-
sentence=Library for M5Stack UNIT MiniEncoderC
6-
paragraph=See more on http://M5Stack.com
5+
sentence=M5Stack Library of Hat Mini EncoderC
6+
paragraph=M5Stack, M5HatMiniEncoderC, See more on http://M5Stack.com
77
category=Device Control
8-
url=https://github.com/m5stack/M5Unit-MiniEncoderC
8+
url=https://github.com/m5stack/M5Hat-Mini-EncoderC
99
architectures=esp32
10-
includes=Unit_MiniEncoderC.h
10+
includes=M5HatMiniEncoderC.h
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#include "Unit_MiniEncoderC.h"
1+
#include "M5HatMiniEncoderC.h"
22

3-
void UNIT_MINIENCODERC::writeBytes(uint8_t addr, uint8_t reg, uint8_t *buffer,
3+
void M5HatMiniEncoderC::writeBytes(uint8_t addr, uint8_t reg, uint8_t *buffer,
44
uint8_t length) {
55
_wire->beginTransmission(addr);
66
_wire->write(reg);
@@ -10,7 +10,7 @@ void UNIT_MINIENCODERC::writeBytes(uint8_t addr, uint8_t reg, uint8_t *buffer,
1010
_wire->endTransmission();
1111
}
1212

13-
void UNIT_MINIENCODERC::readBytes(uint8_t addr, uint8_t reg, uint8_t *buffer,
13+
void M5HatMiniEncoderC::readBytes(uint8_t addr, uint8_t reg, uint8_t *buffer,
1414
uint8_t length) {
1515
uint8_t index = 0;
1616
_wire->beginTransmission(addr);
@@ -22,7 +22,7 @@ void UNIT_MINIENCODERC::readBytes(uint8_t addr, uint8_t reg, uint8_t *buffer,
2222
}
2323
}
2424

25-
bool UNIT_MINIENCODERC::begin(TwoWire *wire, uint8_t addr, uint8_t sda,
25+
bool M5HatMiniEncoderC::begin(TwoWire *wire, uint8_t addr, uint8_t sda,
2626
uint8_t scl, uint32_t speed) {
2727
_wire = wire;
2828
_addr = addr;
@@ -40,7 +40,7 @@ bool UNIT_MINIENCODERC::begin(TwoWire *wire, uint8_t addr, uint8_t sda,
4040
}
4141
}
4242

43-
void UNIT_MINIENCODERC::setEncoderValue(int32_t value) {
43+
void M5HatMiniEncoderC::setEncoderValue(int32_t value) {
4444
uint8_t data[4];
4545

4646
data[0] = value & 0xff;
@@ -50,43 +50,43 @@ void UNIT_MINIENCODERC::setEncoderValue(int32_t value) {
5050
writeBytes(_addr, ENCODER_REG, data, 4);
5151
}
5252

53-
int32_t UNIT_MINIENCODERC::getEncoderValue() {
53+
int32_t M5HatMiniEncoderC::getEncoderValue() {
5454
uint8_t data[4];
5555
readBytes(_addr, ENCODER_REG, data, 4);
5656
int32_t value =
5757
data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
5858
return value;
5959
}
6060

61-
uint32_t UNIT_MINIENCODERC::getIncrementValue() {
61+
uint32_t M5HatMiniEncoderC::getIncrementValue() {
6262
uint8_t data[4];
6363
readBytes(_addr, INCREMENT_REG, data, 4);
6464
uint32_t value =
6565
data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
6666
return value;
6767
}
6868

69-
bool UNIT_MINIENCODERC::getButtonStatus() {
69+
bool M5HatMiniEncoderC::getButtonStatus() {
7070
uint8_t data;
7171
readBytes(_addr, BUTTON_REG, &data, 1);
7272
return data;
7373
}
7474

75-
void UNIT_MINIENCODERC::setLEDColor(uint8_t index, uint32_t color) {
75+
void M5HatMiniEncoderC::setLEDColor(uint32_t rgb888color) {
7676
uint8_t data[4];
77-
data[2] = color & 0xff;
78-
data[1] = (color >> 8) & 0xff;
79-
data[0] = (color >> 16) & 0xff;
77+
data[2] = rgb888color & 0xff;
78+
data[1] = (rgb888color >> 8) & 0xff;
79+
data[0] = (rgb888color >> 16) & 0xff;
8080
writeBytes(_addr, RGB_LED_REG, data, 3);
8181
}
8282

83-
void UNIT_MINIENCODERC::resetCounter(void) {
83+
void M5HatMiniEncoderC::resetCounter(void) {
8484
uint8_t data[4];
8585
data[0] = 1;
8686
writeBytes(_addr, RESET_COUNTER_REG, data, 1);
8787
}
8888

89-
uint8_t UNIT_MINIENCODERC::setI2CAddress(uint8_t addr) {
89+
uint8_t M5HatMiniEncoderC::setI2CAddress(uint8_t addr) {
9090
_wire->beginTransmission(_addr);
9191
_wire->write(I2C_ADDRESS_REG);
9292
_wire->write(addr);
@@ -95,7 +95,7 @@ uint8_t UNIT_MINIENCODERC::setI2CAddress(uint8_t addr) {
9595
return _addr;
9696
}
9797

98-
uint8_t UNIT_MINIENCODERC::getI2CAddress(void) {
98+
uint8_t M5HatMiniEncoderC::getI2CAddress(void) {
9999
_wire->beginTransmission(_addr);
100100
_wire->write(I2C_ADDRESS_REG);
101101
_wire->endTransmission();
@@ -107,7 +107,7 @@ uint8_t UNIT_MINIENCODERC::getI2CAddress(void) {
107107
return RegValue;
108108
}
109109

110-
uint8_t UNIT_MINIENCODERC::getFirmwareVersion(void) {
110+
uint8_t M5HatMiniEncoderC::getFirmwareVersion(void) {
111111
_wire->beginTransmission(_addr);
112112
_wire->write(FIRMWARE_VERSION_REG);
113113
_wire->endTransmission();
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
#ifndef __UNIT_MINIENCODERC_H
2-
#define __UNIT_MINIENCODERC_H
1+
#ifndef __M5HATMINIENCODERC_H
2+
#define __M5HATMINIENCODERC_H
33

44
#include "Arduino.h"
55
#include "Wire.h"
66

7-
#define MINIENCODERC_ADDR 0x42
7+
#define MiniEncoderC_ADDR 0x42
88
#define ENCODER_REG 0x00
99
#define INCREMENT_REG 0x10
1010
#define BUTTON_REG 0x20
@@ -13,7 +13,7 @@
1313
#define FIRMWARE_VERSION_REG 0xFE
1414
#define I2C_ADDRESS_REG 0xFF
1515

16-
class UNIT_MINIENCODERC {
16+
class M5HatMiniEncoderC {
1717
private:
1818
uint8_t _addr;
1919
TwoWire* _wire;
@@ -24,13 +24,14 @@ class UNIT_MINIENCODERC {
2424
void readBytes(uint8_t addr, uint8_t reg, uint8_t* buffer, uint8_t length);
2525

2626
public:
27-
bool begin(TwoWire* wire = &Wire, uint8_t addr = MINIENCODERC_ADDR,
28-
uint8_t sda = 21, uint8_t scl = 22, uint32_t speed = 100000L);
27+
bool begin(TwoWire* wire = &Wire, uint8_t addr = MiniEncoderC_ADDR,
28+
uint8_t sda = 0, uint8_t scl = 26,
29+
uint32_t speed = 200000L); // Default StickC Plus
2930
int32_t getEncoderValue();
3031
void setEncoderValue(int32_t value);
3132
uint32_t getIncrementValue();
3233
bool getButtonStatus();
33-
void setLEDColor(uint8_t index, uint32_t color);
34+
void setLEDColor(uint32_t rgb888color);
3435
uint8_t setI2CAddress(uint8_t addr);
3536
uint8_t getI2CAddress(void);
3637
uint8_t getFirmwareVersion(void);

0 commit comments

Comments
 (0)