forked from bareboat-necessities/bbn_esp32_sensors_hub
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUnit_Encoder.h
97 lines (86 loc) · 2.79 KB
/
Unit_Encoder.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*!
@brief A rotary encoder expansion From M5Stack
@copyright Copyright (c) 2022 by M5Stack[https://m5stack.com]
@Links [Unit Encoder](https://docs.m5stack.com/en/unit/encoder)
@version V0.0.2
@date 2022-07-11
*/
#ifndef _UNIT_ENCODER_H_
#define _UNIT_ENCODER_H_
#define ENCODER_ADDR 0x40
#define ENCODER_REG 0x10
#define BUTTON_REG 0x20
#define RGB_LED_REG 0x30
class Unit_Encoder {
private:
uint8_t _addr;
TwoWire* _wire;
uint8_t _scl;
uint8_t _sda;
uint32_t _speed;
void writeBytes(uint8_t addr, uint8_t reg, uint8_t* buffer, uint8_t length);
void readBytes(uint8_t addr, uint8_t reg, uint8_t* buffer, uint8_t length);
public:
void begin(TwoWire* wire = &Wire, uint8_t addr = ENCODER_ADDR,
uint8_t sda = SDA, uint8_t scl = SCL, uint32_t speed = 200000L);
signed short int getEncoderValue();
bool getButtonStatus();
void setLEDColor(uint8_t index, uint32_t color);
};
/*! @brief Initialize the Encoder. */
void Unit_Encoder::begin(TwoWire *wire, uint8_t addr, uint8_t sda, uint8_t scl,
uint32_t speed) {
_wire = wire;
_addr = addr;
_sda = sda;
_scl = scl;
_speed = speed;
_wire->begin((int)_sda, (int)_scl, _speed);
}
/*! @brief Write a certain length of data to the specified register address. */
void Unit_Encoder::writeBytes(uint8_t addr, uint8_t reg, uint8_t *buffer,
uint8_t length) {
_wire->beginTransmission(addr);
_wire->write(reg);
for (int i = 0; i < length; i++) {
_wire->write(*(buffer + i));
}
_wire->endTransmission();
}
/*! @brief Read a certain length of data to the specified register address. */
void Unit_Encoder::readBytes(uint8_t addr, uint8_t reg, uint8_t *buffer,
uint8_t length) {
uint8_t index = 0;
_wire->beginTransmission(addr);
_wire->write(reg);
_wire->endTransmission();
_wire->requestFrom(addr, length);
for (int i = 0; i < length; i++) {
buffer[index++] = _wire->read();
}
}
/*! @brief Read the encoder value.
@return The value of the encoder that was read */
signed short int Unit_Encoder::getEncoderValue() {
uint8_t data[2];
readBytes(_addr, ENCODER_REG, data, 2);
signed short int value = (signed short int)((data[0]) | (data[1]) << 8);
return value;
}
/*! @brief Get the current status of the rotary encoder keys.
@return 1 if the set was press, otherwise 0.. */
bool Unit_Encoder::getButtonStatus() {
uint8_t data;
readBytes(_addr, BUTTON_REG, &data, 1);
return data;
}
/*! @brief Set the color of the LED (HEX). */
void Unit_Encoder::setLEDColor(uint8_t index, uint32_t color) {
uint8_t data[4];
data[3] = color & 0xff;
data[2] = (color >> 8) & 0xff;
data[1] = (color >> 16) & 0xff;
data[0] = index;
writeBytes(_addr, RGB_LED_REG, data, 4);
}
#endif