Files

99 lines
2.5 KiB
Arduino
Raw Permalink Normal View History

2019-01-04 13:38:39 +08:00
#include <M5Stack.h>
#define Faces_Encoder_I2C_ADDR 0X5E
2019-01-04 13:43:33 +08:00
int encoder_increment;//positive: clockwise nagtive: anti-clockwise
2019-01-04 13:38:39 +08:00
int encoder_value=0;
2019-01-04 15:29:47 +08:00
uint8_t direction;//0: clockwise 1: anti-clockwise
uint8_t last_button, cur_button;
2019-01-04 13:38:39 +08:00
void GetValue(void){
int temp_encoder_increment;
2019-01-04 15:29:47 +08:00
Wire.requestFrom(Faces_Encoder_I2C_ADDR, 3);
2019-01-04 13:38:39 +08:00
if(Wire.available()){
temp_encoder_increment = Wire.read();
2019-01-04 15:29:47 +08:00
cur_button = Wire.read();
2019-01-04 13:38:39 +08:00
}
if(temp_encoder_increment > 127){//anti-clockwise
2019-01-04 15:29:47 +08:00
direction = 1;
2019-01-04 13:38:39 +08:00
encoder_increment = 256 - temp_encoder_increment;
}
else{
2019-01-04 15:29:47 +08:00
direction = 0;
2019-01-04 13:38:39 +08:00
encoder_increment = temp_encoder_increment;
}
}
void Led(int i, int r, int g, int b){
2019-01-04 15:29:47 +08:00
Wire.beginTransmission(Faces_Encoder_I2C_ADDR);
2019-01-04 13:38:39 +08:00
Wire.write(i);
Wire.write(r);
Wire.write(g);
Wire.write(b);
Wire.endTransmission();
}
void setup()
{
M5.begin();
Wire.begin();
M5.Lcd.setTextFont(2);
M5.Lcd.println("FACES ENCODER I2C Read Example");
Serial.println("FACES ENCODER I2C Read Example");
for(int i=0;i<12;i++)
{
Led(i, 0, 0xff, 0);
delay(10);
}
for(int i=0;i<12;i++)
{
Led(i, 0, 0, 0);
delay(10);
}
}
void loop()
{
int i;
2019-01-04 15:29:47 +08:00
M5.Lcd.setCursor(0,40); M5.Lcd.print("Encoder Value: ");
2019-01-04 13:38:39 +08:00
M5.Lcd.setCursor(0,60); M5.Lcd.print(" Key State : ");
M5.Lcd.setCursor(0,80); M5.Lcd.print(" Key Value : ");
GetValue();
2019-01-04 15:29:47 +08:00
if(last_button != cur_button){
2019-01-04 13:38:39 +08:00
M5.Lcd.fillRect(100,60,100,25,BLACK);
M5.Lcd.fillRect(100,80,100,25,BLACK);
2019-01-04 15:29:47 +08:00
last_button = cur_button;
2019-01-04 13:38:39 +08:00
}
2019-01-04 15:29:47 +08:00
if(cur_button){
2019-01-04 13:38:39 +08:00
M5.Lcd.setCursor(100,60); M5.Lcd.print("released");
M5.Lcd.setCursor(100,80); M5.Lcd.print("1");
2019-01-04 15:29:47 +08:00
for(i=0;i<12;i++){
2019-01-04 13:38:39 +08:00
Led(i, 0, 0, 0);
}
}
2019-01-04 15:29:47 +08:00
else{
M5.Lcd.setCursor(100,60); M5.Lcd.print("pressed");
M5.Lcd.setCursor(100,80); M5.Lcd.print("0");
for(i=0;i<12;i++){
Led(i, 255, 255, 255);
}
}
2019-01-04 13:38:39 +08:00
M5.Lcd.fillRect(100,40,50,25,BLACK);
2019-01-04 15:29:47 +08:00
if(direction){
2019-01-04 13:38:39 +08:00
encoder_value -= encoder_increment;
M5.Lcd.setCursor(100,40); M5.Lcd.print("-");
M5.Lcd.setCursor(100,40); M5.Lcd.print(encoder_value);
Serial.print("encoder_value: "); Serial.print("-"); Serial.print(encoder_value);
}
else{
encoder_value += encoder_increment;
M5.Lcd.setCursor(100,40); M5.Lcd.print(encoder_value);
Serial.print("encoder_value: "); Serial.print(encoder_value);
}
2019-01-04 15:29:47 +08:00
Serial.print(" button_state: "); Serial.println(cur_button);
2019-01-04 13:38:39 +08:00
}