From 1862fc3c610dfd87ba5162a43c1b24a32dada3b3 Mon Sep 17 00:00:00 2001 From: FattoreSaimon Date: Mon, 25 Mar 2019 15:52:17 +0100 Subject: [PATCH] I2CEncoderV2 enum redefinition Moved the enum inside of the I2CEncoderV2 class. --- examples/I2CEncoderV2/Basic/Basic.ino | 63 +-- .../Basic_with_Callbacks.ino | 72 +-- .../ESP32_RGB_encoder/ESP32_RGB_encoder.ino | 10 +- examples/I2CEncoderV2/GPs_ADC/GPs_ADC.ino | 10 +- .../LCD_with_RGB_encoder.ino | 78 ++- .../Matrix_3x3_RGB_encoders.ino | 51 +- examples/I2CEncoderV2/README.md | 55 +- .../Teensy_RGB_encoder/Teensy_RGB_encoder.ino | 50 +- .../I2CEncoderV2/WEMOS_OLED/WEMOS_OLED.ino | 69 +-- .../WEMOS_OLED_Menu/WEMOS_OLED_Menu.ino | 86 +-- .../WEMOS_OLED_Menu_Callback_EEPROM.ino | 92 ++-- src/i2cEncoderLibV2.cpp | 165 +++--- src/i2cEncoderLibV2.h | 510 +++++++++--------- 13 files changed, 643 insertions(+), 668 deletions(-) diff --git a/examples/I2CEncoderV2/Basic/Basic.ino b/examples/I2CEncoderV2/Basic/Basic.ino index d9c2d84..1138d81 100644 --- a/examples/I2CEncoderV2/Basic/Basic.ino +++ b/examples/I2CEncoderV2/Basic/Basic.ino @@ -16,87 +16,88 @@ const int IntPin = A3; /* Definition of the interrupt pin. You can change according to your board /* //Class initialization with the I2C addresses*/ -i2cEncoderLibV2 Encoder(0x61); /* A0 is solderedA */ +i2cEncoderLibV2 Encoder(0x01); /* A0 is soldered */ - -void setup(void) -{ +void setup(void) { pinMode(IntPin, INPUT); Wire.begin(); Serial.begin(115200); Serial.println("**** I2C Encoder V2 basic example ****"); /* - INT_DATA= The register are considered integer. - WRAP_DISABLE= The WRAP option is disabled - DIRE_LEFT= Encoder left direction increase the value - IPUP_ENABLE= INT pin have the pull-up enabled. - RMOD_X1= Encoder configured as X1. - RGB_ENCODER= type of encoder is RGB, change to STD_ENCODER in case you are using a normal rotary encoder. + INT_DATA= The register are considered integer. + WRAP_DISABLE= The WRAP option is disabled + DIRE_LEFT= Encoder left direction increase the value + IPUP_ENABLE= INT pin have the pull-up enabled. + RMOD_X1= Encoder configured as X1. + RGB_ENCODER= type of encoder is RGB, change to STD_ENCODER in case you are using a normal rotary encoder. */ Encoder.reset(); - Encoder.begin(INT_DATA | WRAP_DISABLE | DIRE_LEFT | IPUP_ENABLE | RMOD_X1 | RGB_ENCODER); - // Encoder.begin(INT_DATA | WRAP_DISABLE | DIRE_LEFT | IPUP_ENABLE | RMOD_X1 | STD_ENCODER); // try also this! - // Encoder.begin(INT_DATA | WRAP_ENABLE | DIRE_LEFT | IPUP_ENABLE | RMOD_X1 | RGB_ENCODER); // try also this! - - Encoder.writeCounter((int32_t)0); /* Reset the counter value */ - Encoder.writeMax((int32_t)10); /* Set the maximum threshold*/ + Encoder.begin( + i2cEncoderLibV2::INT_DATA | i2cEncoderLibV2::WRAP_DISABLE + | i2cEncoderLibV2::DIRE_LEFT | i2cEncoderLibV2::IPUP_ENABLE + | i2cEncoderLibV2::RMOD_X1 | i2cEncoderLibV2::RGB_ENCODER); + // Encoder.begin(i2cEncoderLibV2::INT_DATA | i2cEncoderLibV2::WRAP_DISABLE | i2cEncoderLibV2::DIRE_LEFT | i2cEncoderLibV2::IPUP_ENABLE | RMOD_X1 | STD_ENCODER); // try also this! + // Encoder.begin(i2cEncoderLibV2::INT_DATA | i2cEncoderLibV2::WRAP_ENABLE | i2cEncoderLibV2::DIRE_LEFT | i2cEncoderLibV2::IPUP_ENABLE | RMOD_X1 | RGB_ENCODER); // try also this! + + Encoder.writeCounter((int32_t) 0); /* Reset the counter value */ + Encoder.writeMax((int32_t) 10); /* Set the maximum threshold*/ Encoder.writeMin((int32_t) - 10); /* Set the minimum threshold */ - Encoder.writeStep((int32_t)1); /* Set the step to 1*/ + Encoder.writeStep((int32_t) 1); /* Set the step to 1*/ Encoder.writeInterruptConfig(0xff); /* Enable all the interrupt */ - Encoder.writeAntibouncingPeriod(20); /* Set an anti-bouncing of 200ms */ - Encoder.writeDoublePushPeriod(50); /*Set a period for the double push of 500ms */ + Encoder.writeAntibouncingPeriod(20); /* Set an anti-bouncing of 200ms */ + Encoder.writeDoublePushPeriod(50); /*Set a period for the double push of 500ms */ } void loop() { if (digitalRead(IntPin) == LOW) { - if ( Encoder.updateStatus()) { - if ( Encoder.readStatus(RINC)) { + if (Encoder.updateStatus()) { + if (Encoder.readStatus(i2cEncoderLibV2::RINC)) { Serial.print("Increment: "); - Serial.println( Encoder.readCounterByte()); + Serial.println(Encoder.readCounterByte()); /* Write here your code */ } - if ( Encoder.readStatus(RDEC)) { + if (Encoder.readStatus(i2cEncoderLibV2::RDEC)) { Serial.print("Decrement: "); - Serial.println( Encoder.readCounterByte()); + Serial.println(Encoder.readCounterByte()); /* Write here your code */ } - if ( Encoder.readStatus(RMAX)) { + if (Encoder.readStatus(i2cEncoderLibV2::RMAX)) { Serial.print("Maximum threshold: "); - Serial.println( Encoder.readCounterByte()); + Serial.println(Encoder.readCounterByte()); /* Write here your code */ } - if ( Encoder.readStatus(RMIN)) { + if (Encoder.readStatus(i2cEncoderLibV2::RMIN)) { Serial.print("Minimum threshold: "); - Serial.println( Encoder.readCounterByte()); + Serial.println(Encoder.readCounterByte()); /* Write here your code */ } - if ( Encoder.readStatus(PUSHR)) { + if (Encoder.readStatus(i2cEncoderLibV2::PUSHR)) { Serial.println("Push button Released"); /* Write here your code */ } - if ( Encoder.readStatus(PUSHP)) { + if (Encoder.readStatus(i2cEncoderLibV2::PUSHP)) { Serial.println("Push button Pressed"); /* Write here your code */ } - if ( Encoder.readStatus(PUSHD)) { + if (Encoder.readStatus(i2cEncoderLibV2::PUSHD)) { Serial.println("Double push!"); /* Write here your code */ diff --git a/examples/I2CEncoderV2/Basic_with_Callbacks/Basic_with_Callbacks.ino b/examples/I2CEncoderV2/Basic_with_Callbacks/Basic_with_Callbacks.ino index 805833b..f875537 100644 --- a/examples/I2CEncoderV2/Basic_with_Callbacks/Basic_with_Callbacks.ino +++ b/examples/I2CEncoderV2/Basic_with_Callbacks/Basic_with_Callbacks.ino @@ -16,91 +16,75 @@ const int IntPin = A3; /* Definition of the interrupt pin. You can change according to your board */ //Class initialization with the I2C addresses -i2cEncoderLibV2 Encoder(0x61); /* A0 is soldered */ - +i2cEncoderLibV2 Encoder(0x01); /* A0 is soldered */ //Callback when the CVAL is incremented void encoder_increment(i2cEncoderLibV2* obj) { Serial.print("Increment: "); - Serial.println( Encoder.readCounterByte()); + Serial.println(Encoder.readCounterByte()); } - - //Callback when the CVAL is decremented void encoder_decrement(i2cEncoderLibV2* obj) { Serial.print("Decrement: "); - Serial.println( Encoder.readCounterByte()); + Serial.println(Encoder.readCounterByte()); } - - //Callback when CVAL reach MAX void encoder_max(i2cEncoderLibV2* obj) { Serial.print("Maximum threshold: "); - Serial.println( Encoder.readCounterByte()); + Serial.println(Encoder.readCounterByte()); } - - - //Callback when CVAL reach MIN void encoder_min(i2cEncoderLibV2* obj) { Serial.print("Minimum threshold: "); - Serial.println( Encoder.readCounterByte()); + Serial.println(Encoder.readCounterByte()); } - - - //Callback when the encoder is pushed void encoder_push(i2cEncoderLibV2* obj) { Serial.println("Encoder is pushed!"); } - - //Callback when the encoder is released void encoder_released(i2cEncoderLibV2* obj) { Serial.println("Encoder is released"); } - - //Callback when the encoder is double pushed void encoder_double_push(i2cEncoderLibV2* obj) { Serial.println("Encoder is double pushed!"); } - - - -void setup(void) -{ +void setup(void) { pinMode(IntPin, INPUT); Wire.begin(); Serial.begin(115200); Serial.println("**** I2C Encoder V2 basic example ****"); /* - INT_DATA= The register are considered integer. - WRAP_DISABLE= The WRAP option is disabled - DIRE_LEFT= Encoder left direction increase the value - IPUP_ENABLE= INT pin have the pull-up enabled. - RMOD_X1= Encoder configured as X1. - RGB_ENCODER= type of encoder is RGB, change to STD_ENCODER in case you are using a normal rotary encoder. + INT_DATA= The register are considered integer. + WRAP_DISABLE= The WRAP option is disabled + DIRE_LEFT= Encoder left direction increase the value + IPUP_ENABLE= INT pin have the pull-up enabled. + RMOD_X1= Encoder configured as X1. + RGB_ENCODER= type of encoder is RGB, change to STD_ENCODER in case you are using a normal rotary encoder. */ Encoder.reset(); - Encoder.begin(INT_DATA | WRAP_DISABLE | DIRE_LEFT | IPUP_ENABLE | RMOD_X1 | RGB_ENCODER); - // Encoder.begin(INT_DATA | WRAP_DISABLE | DIRE_LEFT | IPUP_ENABLE | RMOD_X1 | STD_ENCODER); // try also this! - // Encoder.begin(INT_DATA | WRAP_ENABLE | DIRE_LEFT | IPUP_ENABLE | RMOD_X1 | RGB_ENCODER); // try also this! - - Encoder.writeCounter((int32_t)0); /* Reset the counter value */ - Encoder.writeMax((int32_t)10); /* Set the maximum threshold*/ + Encoder.begin( + i2cEncoderLibV2::INT_DATA | i2cEncoderLibV2::WRAP_DISABLE + | i2cEncoderLibV2::DIRE_LEFT | i2cEncoderLibV2::IPUP_ENABLE + | i2cEncoderLibV2::RMOD_X1 | i2cEncoderLibV2::RGB_ENCODER); + // Encoder.begin(i2cEncoderLibV2::INT_DATA | i2cEncoderLibV2::WRAP_DISABLE | i2cEncoderLibV2::DIRE_LEFT | i2cEncoderLibV2::IPUP_ENABLE | RMOD_X1 | STD_ENCODER); // try also this! + // Encoder.begin(i2cEncoderLibV2::INT_DATA | i2cEncoderLibV2::WRAP_ENABLE | i2cEncoderLibV2::DIRE_LEFT | i2cEncoderLibV2::IPUP_ENABLE | RMOD_X1 | RGB_ENCODER); // try also this! + + Encoder.writeCounter((int32_t) 0); /* Reset the counter value */ + Encoder.writeMax((int32_t) 10); /* Set the maximum threshold*/ Encoder.writeMin((int32_t) - 10); /* Set the minimum threshold */ - Encoder.writeStep((int32_t)1); /* Set the step to 1*/ - Encoder.writeAntibouncingPeriod(20); /* Set an anti-bouncing of 200ms */ - Encoder.writeDoublePushPeriod(50); /*Set a period for the double push of 500ms */ - + Encoder.writeStep((int32_t) 1); /* Set the step to 1*/ + Encoder.writeAntibouncingPeriod(20); /* Set an anti-bouncing of 200ms */ + Encoder.writeDoublePushPeriod(50); /*Set a period for the double push of 500ms */ + // Definition of the events Encoder.onIncrement = encoder_increment; Encoder.onDecrement = encoder_decrement; @@ -109,9 +93,9 @@ void setup(void) Encoder.onButtonPush = encoder_push; Encoder.onButtonRelease = encoder_released; Encoder.onButtonDoublePush = encoder_double_push; - + /* Enable the I2C Encoder V2 interrupts according to the previus attached callback */ - Encoder.autoconfigInterrupt(); + Encoder.autoconfigInterrupt(); } @@ -120,4 +104,4 @@ void loop() { /* Check the status of the encoder and call the callback */ Encoder.updateStatus(); } -} \ No newline at end of file +} diff --git a/examples/I2CEncoderV2/ESP32_RGB_encoder/ESP32_RGB_encoder.ino b/examples/I2CEncoderV2/ESP32_RGB_encoder/ESP32_RGB_encoder.ino index 41e4f65..e4cea8b 100644 --- a/examples/I2CEncoderV2/ESP32_RGB_encoder/ESP32_RGB_encoder.ino +++ b/examples/I2CEncoderV2/ESP32_RGB_encoder/ESP32_RGB_encoder.ino @@ -22,7 +22,7 @@ i2cEncoderLibV2 Encoder(0b1100001); /* For make the address 0x61 only the jumper //Callback when the encoder is rotated void encoder_rotated(i2cEncoderLibV2* obj) { - if ( obj->readStatus( RINC)) + if ( obj->readStatus( i2cEncoderLibV2::RINC)) Serial.print("Increment: "); else Serial.print("Decrement: "); @@ -40,7 +40,7 @@ void encoder_click(i2cEncoderLibV2* obj) { //Callback when the encoder reach the max or min void encoder_thresholds(i2cEncoderLibV2* obj) { - if ( obj->readStatus( RMAX)) + if ( obj->readStatus( i2cEncoderLibV2::RMAX)) Serial.println("Max!"); else Serial.println("Min!"); @@ -72,9 +72,9 @@ void setup(void) Wire.begin(); Encoder.reset(); - Encoder.begin(INT_DATA | WRAP_DISABLE | DIRE_LEFT | IPUP_ENABLE | RMOD_X1 | RGB_ENCODER); - // Encoder.begin(INT_DATA | WRAP_DISABLE | DIRE_LEFT | IPUP_ENABLE | RMOD_X1 | STD_ENCODER); // try also this! - // Encoder.begin(INT_DATA | WRAP_ENABLE | DIRE_LEFT | IPUP_ENABLE | RMOD_X1 | RGB_ENCODER); // try also this! + Encoder.begin(i2cEncoderLibV2::INT_DATA |i2cEncoderLibV2:: WRAP_DISABLE | i2cEncoderLibV2::DIRE_LEFT | i2cEncoderLibV2::IPUP_ENABLE | i2cEncoderLibV2::RMOD_X1 | i2cEncoderLibV2::RGB_ENCODER); + // Encoder.begin(i2cEncoderLibV2::INT_DATA | i2cEncoderLibV2::WRAP_DISABLE | i2cEncoderLibV2::DIRE_LEFT | i2cEncoderLibV2::IPUP_ENABLE | i2cEncoderLibV2::RMOD_X1 | i2cEncoderLibV2::STD_ENCODER); // try also this! + // Encoder.begin(i2cEncoderLibV2::INT_DATA | i2cEncoderLibV2::WRAP_ENABLE | i2cEncoderLibV2::DIRE_LEFT | i2cEncoderLibV2::IPUP_ENABLE | i2cEncoderLibV2::RMOD_X1 | i2cEncoderLibV2::RGB_ENCODER); // try also this! Encoder.writeCounter((int32_t)0); /* Reset the counter value */ Encoder.writeMax((int32_t)10); /* Set the maximum threshold*/ diff --git a/examples/I2CEncoderV2/GPs_ADC/GPs_ADC.ino b/examples/I2CEncoderV2/GPs_ADC/GPs_ADC.ino index ba142d0..812d1c3 100644 --- a/examples/I2CEncoderV2/GPs_ADC/GPs_ADC.ino +++ b/examples/I2CEncoderV2/GPs_ADC/GPs_ADC.ino @@ -41,15 +41,15 @@ void setup(void) //Configure the Standard Encoder STDEncoder.reset(); - STDEncoder.begin(INT_DATA | WRAP_DISABLE | DIRE_LEFT | IPUP_ENABLE | RMOD_X1 | STD_ENCODER); - STDEncoder.writeGP1conf(GP_AN | GP_PULL_EN | GP_INT_DI); // Configure the GP pins in analog mode - STDEncoder.writeGP2conf(GP_AN | GP_PULL_EN | GP_INT_DI); // Configure the GP pins in analog mode - STDEncoder.writeGP3conf(GP_AN | GP_PULL_EN | GP_INT_DI); // Configure the GP pins in analog mode + STDEncoder.begin(i2cEncoderLibV2::INT_DATA | i2cEncoderLibV2::WRAP_DISABLE | i2cEncoderLibV2::DIRE_LEFT | i2cEncoderLibV2::IPUP_ENABLE | i2cEncoderLibV2::RMOD_X1 | i2cEncoderLibV2::STD_ENCODER); + STDEncoder.writeGP1conf(i2cEncoderLibV2::GP_AN | i2cEncoderLibV2::GP_PULL_EN | i2cEncoderLibV2::GP_INT_DI); // Configure the GP pins in analog mode + STDEncoder.writeGP2conf(i2cEncoderLibV2::GP_AN | i2cEncoderLibV2::GP_PULL_EN | i2cEncoderLibV2::GP_INT_DI); // Configure the GP pins in analog mode + STDEncoder.writeGP3conf(i2cEncoderLibV2::GP_AN | i2cEncoderLibV2::GP_PULL_EN | i2cEncoderLibV2::GP_INT_DI); // Configure the GP pins in analog mode STDEncoder.writeCounter((int32_t) 0); STDEncoder.writeMax((int32_t) 10); STDEncoder.writeMin((int32_t) 0); STDEncoder.writeStep((int32_t) 1); - STDEncoder.writeInterruptConfig(INT2 | RINC | RDEC | RMAX | RMIN); //Enable all the interrupts + STDEncoder.writeInterruptConfig(i2cEncoderLibV2::INT_2 | i2cEncoderLibV2::RINC | i2cEncoderLibV2::RDEC | i2cEncoderLibV2::RMAX | i2cEncoderLibV2::RMIN); //Enable all the interrupts STDEncoder.writeAntibouncingPeriod(20); /* Set an anti-bouncing of 200ms */ STDEncoder.updateStatus(); diff --git a/examples/I2CEncoderV2/LCD_with_RGB_encoder/LCD_with_RGB_encoder.ino b/examples/I2CEncoderV2/LCD_with_RGB_encoder/LCD_with_RGB_encoder.ino index 8bf9caa..eb02fa7 100644 --- a/examples/I2CEncoderV2/LCD_with_RGB_encoder/LCD_with_RGB_encoder.ino +++ b/examples/I2CEncoderV2/LCD_with_RGB_encoder/LCD_with_RGB_encoder.ino @@ -17,8 +17,8 @@ unsigned long previousMillis = 0; const long interval = 100; LiquidCrystal lcd(8, 9, 4, 5, 6, 7); -int lcd_key = 0; -int adc_key_in = 0; +int lcd_key = 0; +int adc_key_in = 0; #define btnRIGHT 0 #define btnUP 1 #define btnDOWN 2 @@ -29,30 +29,31 @@ int adc_key_in = 0; const int IntPin = A3; //Class initialization with the I2C addresses -i2cEncoderLibV2 RGBEncoder(0b1100001); - +i2cEncoderLibV2 RGBEncoder(0b1100001); /* For make the address 0x61 only the jumpers A0, A5 and A6 are soldered.*/ // read the buttons -int read_LCD_buttons() -{ +int read_LCD_buttons() { adc_key_in = analogRead(0); // read the value from the sensor // my buttons when read are centered at these valies: 0, 144, 329, 504, 741 // we add approx 50 to those values and check to see if we are close - if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result + if (adc_key_in > 1000) + return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result // For V1.1 us this threshold - if (adc_key_in < 50) return btnRIGHT; - if (adc_key_in < 250) return btnUP; - if (adc_key_in < 450) return btnDOWN; - if (adc_key_in < 650) return btnLEFT; - if (adc_key_in < 850) return btnSELECT; + if (adc_key_in < 50) + return btnRIGHT; + if (adc_key_in < 250) + return btnUP; + if (adc_key_in < 450) + return btnDOWN; + if (adc_key_in < 650) + return btnLEFT; + if (adc_key_in < 850) + return btnSELECT; return btnNONE; // when all others fail, return this... } - - uint8_t encoder_status, i; -void setup(void) -{ +void setup(void) { Wire.begin(); Wire.setClock(400000); RGBEncoder.reset(); @@ -66,96 +67,93 @@ void setup(void) Serial.begin(115200); Serial.print("Encoder Test!!\n"); - RGBEncoder.begin(FLOAT_DATA | WRAP_DISABLE | DIRE_LEFT | IPUP_ENABLE | RMOD_X1 | RGB_ENCODER); + RGBEncoder.begin( + i2cEncoderLibV2::FLOAT_DATA | i2cEncoderLibV2::WRAP_DISABLE | i2cEncoderLibV2::DIRE_LEFT | i2cEncoderLibV2::IPUP_ENABLE | i2cEncoderLibV2::RMOD_X1 + | i2cEncoderLibV2::RGB_ENCODER); - - RGBEncoder.writeCounter((float)0); - RGBEncoder.writeMax((float)10); + RGBEncoder.writeCounter((float) 0); + RGBEncoder.writeMax((float) 10); RGBEncoder.writeMin((float) - 10); - RGBEncoder.writeStep((float)0.5); + RGBEncoder.writeStep((float) 0.5); RGBEncoder.writeInterruptConfig(0xff); RGBEncoder.writeDoublePushPeriod(50); RGBEncoder.writeFadeRGB(1); RGBEncoder.updateStatus(); RGBEncoder.writeAntibouncingPeriod(25); - RGBEncoder.writeGP1conf(GP_PWM | GP_PULL_EN | GP_INT_DI); - RGBEncoder.writeGP2conf(GP_PWM | GP_PULL_EN | GP_INT_DI); + RGBEncoder.writeGP1conf(i2cEncoderLibV2::GP_PWM | i2cEncoderLibV2::GP_PULL_EN | i2cEncoderLibV2::GP_INT_DI); + RGBEncoder.writeGP2conf(i2cEncoderLibV2::GP_PWM | i2cEncoderLibV2::GP_PULL_EN | i2cEncoderLibV2::GP_INT_DI); RGBEncoder.writeFadeRGB(2); - } void loop() { - - - if (digitalRead(IntPin) == LOW) { Serial.println("Pin LOW "); /////////////////////////////////////////////////////////////////// if (RGBEncoder.updateStatus()) { - - - if (RGBEncoder.readStatus(RINC)) { + if (RGBEncoder.readStatus(i2cEncoderLibV2::RINC)) { lcd.clear(); lcd.setCursor(0, 0); lcd.print("Inc: "); lcd.print(RGBEncoder.readCounterFloat(), 1); lcd.print(" "); - if ((RGBEncoder.readStatus(RMIN) == 0) && (RGBEncoder.readStatus(RMAX) == 0)) + if ((RGBEncoder.readStatus(i2cEncoderLibV2::RMIN) == 0) + && (RGBEncoder.readStatus(i2cEncoderLibV2::RMAX) == 0)) RGBEncoder.writeLEDG(0xFF); } - if (RGBEncoder.readStatus(RDEC)) { + if (RGBEncoder.readStatus(i2cEncoderLibV2::RDEC)) { lcd.clear(); lcd.setCursor(0, 0); lcd.print("Dec: "); lcd.print(RGBEncoder.readCounterFloat(), 1); lcd.print(" "); - if ((RGBEncoder.readStatus(RMIN) == 0) && (RGBEncoder.readStatus(RMAX) == 0)) + if ((RGBEncoder.readStatus(i2cEncoderLibV2::RMIN) == 0) + && (RGBEncoder.readStatus(i2cEncoderLibV2::RMAX) == 0)) RGBEncoder.writeLEDG(0xFF); } - if (RGBEncoder.readStatus(RMAX)) { + if (RGBEncoder.readStatus(i2cEncoderLibV2::RMAX)) { lcd.setCursor(0, 1); lcd.print("Counter MAX "); RGBEncoder.writeLEDR(0xFF); } - if (RGBEncoder.readStatus(RMIN)) { + if (RGBEncoder.readStatus(i2cEncoderLibV2::RMIN)) { lcd.setCursor(0, 1); lcd.print("Counter MIN "); RGBEncoder.writeLEDR(0xFF); } - if (RGBEncoder.readStatus(PUSHR)) { + if (RGBEncoder.readStatus(i2cEncoderLibV2::PUSHR)) { lcd.setCursor(0, 1); lcd.print("B. Released "); RGBEncoder.writeRGBCode(0x0000FF); } - if (RGBEncoder.readStatus(PUSHP)) { + if (RGBEncoder.readStatus(i2cEncoderLibV2::PUSHP)) { lcd.setCursor(0, 1); lcd.print("B. Pushed "); RGBEncoder.writeRGBCode(0x0000FF); } - if (RGBEncoder.readStatus(PUSHD)) { + if (RGBEncoder.readStatus(i2cEncoderLibV2::PUSHD)) { lcd.setCursor(0, 1); lcd.print("Double Push "); RGBEncoder.writeRGBCode(0x800080); } - if (RGBEncoder.readInt2(FADE_INT)) { //Check if the fade proccess finished, if yes turn off the LEDs + if (RGBEncoder.readInt2(i2cEncoderLibV2::FADE_INT)) { //Check if the fade proccess finished, if yes turn off the LEDs RGBEncoder.writeRGBCode(0x0); } } } -} \ No newline at end of file +} diff --git a/examples/I2CEncoderV2/Matrix_3x3_RGB_encoders/Matrix_3x3_RGB_encoders.ino b/examples/I2CEncoderV2/Matrix_3x3_RGB_encoders/Matrix_3x3_RGB_encoders.ino index 5dd9227..81ba001 100644 --- a/examples/I2CEncoderV2/Matrix_3x3_RGB_encoders/Matrix_3x3_RGB_encoders.ino +++ b/examples/I2CEncoderV2/Matrix_3x3_RGB_encoders/Matrix_3x3_RGB_encoders.ino @@ -2,10 +2,10 @@ #include /*In this example there are 9 I2C Encoder V2 boards with the RGB LED connected in a matrix 3x3 - There is also the Arduino Serial KeyPad Shield attached. + There is also the Arduino Serial KeyPad Shield attached. - Every time of one encoder is moved it's value is showed on the display - In this example the data type are float + Every time of one encoder is moved it's value is showed on the display + In this example the data type are float Connections with Arduino UNO: - -> GND @@ -20,28 +20,22 @@ const int IntPin = A3; // INT pins, change according to your board //Class initialization with the I2C addresses -i2cEncoderLibV2 RGBEncoder[ENCODER_N] = { - i2cEncoderLibV2(0x40), - i2cEncoderLibV2(0x20), - i2cEncoderLibV2(0x60), - i2cEncoderLibV2(0x10), - i2cEncoderLibV2(0x50), - i2cEncoderLibV2(0x30), - i2cEncoderLibV2(0x70), - i2cEncoderLibV2(0x04), - i2cEncoderLibV2(0x44), -}; +i2cEncoderLibV2 RGBEncoder[ENCODER_N] = { i2cEncoderLibV2(0x40), + i2cEncoderLibV2(0x20), i2cEncoderLibV2(0x60), i2cEncoderLibV2(0x10), + i2cEncoderLibV2(0x50), i2cEncoderLibV2(0x30), i2cEncoderLibV2(0x70), + i2cEncoderLibV2(0x04), i2cEncoderLibV2(0x44), + }; uint8_t encoder_status, i; void encoder_rotated(i2cEncoderLibV2* obj) { - if ( obj->readStatus( RINC)) + if (obj->readStatus(i2cEncoderLibV2::RINC)) Serial.print("Increment "); else Serial.print("Decrement "); Serial.print(obj->id); Serial.print(": "); - Serial.println( obj->readCounterInt()); + Serial.println(obj->readCounterInt()); obj->writeRGBCode(0x00FF00); } @@ -52,7 +46,7 @@ void encoder_click(i2cEncoderLibV2* obj) { } void encoder_thresholds(i2cEncoderLibV2* obj) { - if ( obj->readStatus( RMAX)) + if (obj->readStatus(i2cEncoderLibV2::RMAX)) Serial.print("Max: "); else Serial.print("Min: "); @@ -64,9 +58,7 @@ void encoder_fade(i2cEncoderLibV2* obj) { obj->writeRGBCode(0x000000); } - -void setup(void) -{ +void setup(void) { uint8_t enc_cnt; Wire.begin(); @@ -75,20 +67,23 @@ void setup(void) RGBEncoder[enc_cnt].reset(); } - pinMode(IntPin, INPUT); Serial.begin(115200); Serial.print("Encoder Test!!\n"); - // Initialization of the encoders for (enc_cnt = 0; enc_cnt < ENCODER_N; enc_cnt++) { - RGBEncoder[enc_cnt].begin(INT_DATA | WRAP_DISABLE | DIRE_RIGHT | IPUP_ENABLE | RMOD_X1 | RGB_ENCODER); - RGBEncoder[enc_cnt].writeCounter((int32_t)0); //Reset of the CVAL register - RGBEncoder[enc_cnt].writeMax((int32_t)50); //Set the maximum threshold to 50 + RGBEncoder[enc_cnt].begin( + i2cEncoderLibV2::INT_DATA | i2cEncoderLibV2::WRAP_DISABLE + | i2cEncoderLibV2::DIRE_RIGHT + | i2cEncoderLibV2::IPUP_ENABLE + | i2cEncoderLibV2::RMOD_X1 + | i2cEncoderLibV2::RGB_ENCODER); + RGBEncoder[enc_cnt].writeCounter((int32_t) 0); //Reset of the CVAL register + RGBEncoder[enc_cnt].writeMax((int32_t) 50); //Set the maximum threshold to 50 RGBEncoder[enc_cnt].writeMin((int32_t) 0); //Set the minimum threshold to 0 - RGBEncoder[enc_cnt].writeStep((int32_t)1); //The step at every encoder click is 1 + RGBEncoder[enc_cnt].writeStep((int32_t) 1); //The step at every encoder click is 1 RGBEncoder[enc_cnt].writeRGBCode(0); RGBEncoder[enc_cnt].writeFadeRGB(3); //Fade enabled with 3ms step RGBEncoder[enc_cnt].writeAntibouncingPeriod(25); //250ms of debouncing @@ -99,7 +94,7 @@ void setup(void) RGBEncoder[enc_cnt].onButtonRelease = encoder_click; RGBEncoder[enc_cnt].onMinMax = encoder_thresholds; RGBEncoder[enc_cnt].onFadeProcess = encoder_fade; - + /* Enable the I2C Encoder V2 interrupts according to the previus attached callback */ RGBEncoder[enc_cnt].autoconfigInterrupt(); RGBEncoder[enc_cnt].id = enc_cnt; @@ -118,4 +113,4 @@ void loop() { RGBEncoder[enc_cnt].updateStatus(); } } -} \ No newline at end of file +} diff --git a/examples/I2CEncoderV2/README.md b/examples/I2CEncoderV2/README.md index 0735e18..cecb8fd 100644 --- a/examples/I2CEncoderV2/README.md +++ b/examples/I2CEncoderV2/README.md @@ -147,10 +147,13 @@ The possible parameters are the following: ###### Examples: ```C++ -encoder.begin(INT_DATA | WRAP_DISABLE | DIRE_LEFT | IPUP_ENABLE | RMOD_X1 | STD_ENCODER); +encoder.begin(i2cEncoderLibV2::INT_DATA | i2cEncoderLibV2::WRAP_DISABLE | i2cEncoderLibV2::DIRE_LEFT | i2cEncoderLibV2::IPUP_ENABLE | i2cEncoderLibV2::RMOD_X1 | i2cEncoderLibV2::STD_ENCODER); ``` +Please remember to add the class name **i2cEncoderLibV2::** before the parameter! + #### void reset(void) + Reset of the board. In this command there is 10ms delay in order to make the board correctly restart. @@ -185,9 +188,11 @@ The interrupt configurations are used only when the pin is configured as digital ###### Examples: ```C++ -encoder.writeGP1conf(GP_AN | GP_PULL_DI | GP_INT_DI); //Configure the GP1 as analog input with the pull-up and the interrupt disable +encoder.writeGP1conf(i2cEncoderLibV2::GP_AN | i2cEncoderLibV2::GP_PULL_DI | i2cEncoderLibV2::GP_INT_DI); //Configure the GP1 as analog input with the pull-up and the interrupt disable ``` +Please remember to add the class name **i2cEncoderLibV2::** before the parameter! + ##### void writeInterruptConfig(uint8_t interrupt) This method is used for enabling or disabling the interrupt source selectively. When an interrupt event occurs, the INT pin goes low and the event is stored in the status register. @@ -201,9 +206,18 @@ This method is used for enabling or disabling the interrupt source selectively. | RDEC | Encoder is rotated in the decrement direction | | RMAX | Maximum threshold is reached | | RMIN | Minimum threshold is reached | -| INT2 | An event on the interrupt 2 register occurs | +| INT_2 | An event on the interrupt 2 register occurs | + +###### Examples: + +```C++ + Encoder.writeInterruptConfig(i2cEncoderLibV2::INT_2 | i2cEncoderLibV2::RMIN | i2cEncoderLibV2::RMAX | i2cEncoderLibV2::RDEC | i2cEncoderLibV2::RINC | i2cEncoderLibV2::PUSHR); +``` + +Please remember to add the class name **i2cEncoderLibV2::** before the parameter! ##### void autoconfigInterrupt(void) + This method auto configures the **INTCONF** register according to the attached callback. **For the proper use, must be called after the definition of the last event property.** @@ -227,7 +241,7 @@ The I2C encoder V2 will multiplies this value by 10 (value x10). ###### Examples: -```C++ +​```C++ encoder.writeAntibouncingPeriod(20); //Set an anti-bouncing of 200ms ``` @@ -300,33 +314,34 @@ Possible parameters are: ###### Example: ```C++ if ( Encoder.updateStatus() == true) { - if ( Encoder.readStatus(RINC)) { + if ( Encoder.readStatus(i2cEncoderLibV2::RINC)) { Serial.print("Increment "); } - if ( Encoder.readStatus(RDEC)) { + if ( Encoder.readStatus(i2cEncoderLibV2::RDEC)) { Serial.print("Decrement "); } - if ( Encoder.readStatus(RMAX)) { + if ( Encoder.readStatus(i2cEncoderLibV2::RMAX)) { Serial.print("Maximum threshold: "); } - if ( Encoder.readStatus(RMIN)) { + if ( Encoder.readStatus(i2cEncoderLibV2::RMIN)) { Serial.print("Minimum threshold: "); } - if ( Encoder.readStatus(PUSHR)) { + if ( Encoder.readStatus(i2cEncoderLibV2::PUSHR)) { Serial.println("Push button Released"); } - if ( Encoder.readStatus(PUSHP)) { + if ( Encoder.readStatus(i2cEncoderLibV2::PUSHP)) { } - if ( Encoder.readStatus(PUSHD)) { + if ( Encoder.readStatus(i2cEncoderLibV2::PUSHD)) { Serial.println("Double push!"); } ``` +Please remember to add the class name **i2cEncoderLibV2::** before the parameter! ##### uint8_t readStatus(void) @@ -352,34 +367,36 @@ Possible parameters are: ###### Example: ```C++ if ( Encoder.updateStatus() == true) { - if ( Encoder.readInt2(GP1_POS)) { + if ( Encoder.readInt2(i2cEncoderLibV2::GP1_POS)) { Serial.print("GP1 positive edge"); } - if ( Encoder.readInt2(GP1_NEG)) { + if ( Encoder.readInt2(i2cEncoderLibV2::GP1_NEG)) { Serial.print("GP1 negative edge "); } - if ( Encoder.readInt2(GP2_POS)) { + if ( Encoder.readInt2(i2cEncoderLibV2::GP2_POS)) { Serial.print("GP2 positive edge"); } - if ( Encoder.readInt2(GP2_NEG)) { + if ( Encoder.readInt2(i2cEncoderLibV2::GP2_NEG)) { Serial.print("GP2 negative edge "); } - if ( Encoder.readInt2(GP3_POS)) { + if ( Encoder.readInt2(i2cEncoderLibV2::GP3_POS)) { Serial.print("GP3 positive edge"); } - if ( Encoder.readInt2(GP3_NEG)) { + if ( Encoder.readInt2(i2cEncoderLibV2::GP3_NEG)) { Serial.print("GP3 negative edge "); } - if ( Encoder.readInt2(FADE_INT)) { + if ( Encoder.readInt2(i2cEncoderLibV2::FADE_INT)) { Serial.println("Fade process finished"); } ``` +Please remember to add the class name **i2cEncoderLibV2::** before the parameter! + ##### uint8_t readInt2(void) Return the status of the register **I2STATUS** @@ -400,7 +417,7 @@ This function return **true** when the fade running, otherwise return **false** | FADE_GP2 | Fade process status of the GP2 | | FADE_GP3 | Fade process status of the GP3 | - +Please remember to add the class name **i2cEncoderLibV2::** before the parameter! ##### uint8_t readFadeStatus(void) diff --git a/examples/I2CEncoderV2/Teensy_RGB_encoder/Teensy_RGB_encoder.ino b/examples/I2CEncoderV2/Teensy_RGB_encoder/Teensy_RGB_encoder.ino index 62ea1ee..bfa08b5 100644 --- a/examples/I2CEncoderV2/Teensy_RGB_encoder/Teensy_RGB_encoder.ino +++ b/examples/I2CEncoderV2/Teensy_RGB_encoder/Teensy_RGB_encoder.ino @@ -22,25 +22,23 @@ i2cEncoderLibV2 Encoder(0b1100001); /* For make the address 0x61 only the jumper //Callback when the encoder is rotated void encoder_rotated(i2cEncoderLibV2* obj) { - if ( obj->readStatus( RINC)) + if (obj->readStatus(i2cEncoderLibV2::RINC)) Serial.print("Increment: "); else Serial.print("Decrement: "); - Serial.println( obj->readCounterInt()); + Serial.println(obj->readCounterInt()); obj->writeRGBCode(0x00FF00); } - //Callback when the encoder is pushed void encoder_click(i2cEncoderLibV2* obj) { Serial.println("Push: "); obj->writeRGBCode(0x0000FF); } - //Callback when the encoder reach the max or min void encoder_thresholds(i2cEncoderLibV2* obj) { - if ( obj->readStatus( RMAX)) + if (obj->readStatus(i2cEncoderLibV2::RMAX)) Serial.println("Max!"); else Serial.println("Min!"); @@ -48,38 +46,38 @@ void encoder_thresholds(i2cEncoderLibV2* obj) { obj->writeRGBCode(0xFF0000); } - //Callback when the fading process finish and set the RGB led off void encoder_fade(i2cEncoderLibV2* obj) { obj->writeRGBCode(0x000000); } - -void setup(void) -{ +void setup(void) { pinMode(IntPin, INPUT); Serial.begin(115200); Serial.println("**** I2C Encoder V2 basic example ****"); /* - INT_DATA= The register are considered integer. - WRAP_DISABLE= The WRAP option is disabled - DIRE_LEFT= Encoder left direction increase the value - IPUP_ENABLE= INT pin have the pull-up enabled. - RMOD_X1= Encoder configured as X1. - RGB_ENCODER= type of encoder is RGB, change to STD_ENCODER in case you are using a normal rotary encoder. + INT_DATA= The register are considered integer. + WRAP_DISABLE= The WRAP option is disabled + DIRE_LEFT= Encoder left direction increase the value + IPUP_ENABLE= INT pin have the pull-up enabled. + RMOD_X1= Encoder configured as X1. + RGB_ENCODER= type of encoder is RGB, change to STD_ENCODER in case you are using a normal rotary encoder. */ Wire.begin(); Encoder.reset(); - Encoder.begin(INT_DATA | WRAP_DISABLE | DIRE_LEFT | IPUP_ENABLE | RMOD_X1 | RGB_ENCODER); - // Encoder.begin(INT_DATA | WRAP_DISABLE | DIRE_LEFT | IPUP_ENABLE | RMOD_X1 | STD_ENCODER); // try also this! - // Encoder.begin(INT_DATA | WRAP_ENABLE | DIRE_LEFT | IPUP_ENABLE | RMOD_X1 | RGB_ENCODER); // try also this! + Encoder.begin( + i2cEncoderLibV2::INT_DATA | i2cEncoderLibV2::WRAP_DISABLE + | i2cEncoderLibV2::DIRE_LEFT | i2cEncoderLibV2::IPUP_ENABLE + | i2cEncoderLibV2::RMOD_X1 | i2cEncoderLibV2::RGB_ENCODER); + // Encoder.begin(i2cEncoderLibV2::INT_DATA | i2cEncoderLibV2::WRAP_DISABLE | i2cEncoderLibV2::DIRE_LEFT | i2cEncoderLibV2::IPUP_ENABLE | i2cEncoderLibV2::RMOD_X1 | i2cEncoderLibV2::STD_ENCODER); // try also this! + // Encoder.begin(i2cEncoderLibV2::INT_DATA |i2cEncoderLibV2::WRAP_ENABLE | i2cEncoderLibV2::DIRE_LEFT | i2cEncoderLibV2::IPUP_ENABLE | i2cEncoderLibV2::RMOD_X1 | i2cEncoderLibV2::RGB_ENCODER); // try also this! - Encoder.writeCounter((int32_t)0); /* Reset the counter value */ - Encoder.writeMax((int32_t)10); /* Set the maximum threshold*/ + Encoder.writeCounter((int32_t) 0); /* Reset the counter value */ + Encoder.writeMax((int32_t) 10); /* Set the maximum threshold*/ Encoder.writeMin((int32_t) - 10); /* Set the minimum threshold */ - Encoder.writeStep((int32_t)1); /* Set the step to 1*/ + Encoder.writeStep((int32_t) 1); /* Set the step to 1*/ /* Configure the events */ Encoder.onChange = encoder_rotated; @@ -90,7 +88,7 @@ void setup(void) /* Enable the I2C Encoder V2 interrupts according to the previus attached callback */ Encoder.autoconfigInterrupt(); - Encoder.writeAntibouncingPeriod(20); /* Set an anti-bouncing of 200ms */ + Encoder.writeAntibouncingPeriod(20); /* Set an anti-bouncing of 200ms */ /* blink the RGB LED */ Encoder.writeRGBCode(0xFF0000); @@ -100,16 +98,16 @@ void setup(void) Encoder.writeRGBCode(0x0000FF); delay(250); Encoder.writeRGBCode(0x000000); - + Encoder.writeFadeRGB(3); //Fade enabled with 3ms step } void loop() { - /* Waith when the INT pin goes low */ + /* Waith when the INT pin goes low */ if (digitalRead(IntPin) == LOW) { - /* Check the status of the encoder and call the callback */ + /* Check the status of the encoder and call the callback */ Encoder.updateStatus(); } -} \ No newline at end of file +} diff --git a/examples/I2CEncoderV2/WEMOS_OLED/WEMOS_OLED.ino b/examples/I2CEncoderV2/WEMOS_OLED/WEMOS_OLED.ino index fdd2563..3ce8004 100644 --- a/examples/I2CEncoderV2/WEMOS_OLED/WEMOS_OLED.ino +++ b/examples/I2CEncoderV2/WEMOS_OLED/WEMOS_OLED.ino @@ -19,7 +19,6 @@ INT -> D3 */ - const int IntPin = 0; /* Definition of the interrupt pin*/ //Class initialization with the I2C addresses i2cEncoderLibV2 Encoder(0x61); /* For make the address 0x61 only the jumpers A0, A5 and A6 are soldered.*/ @@ -27,31 +26,36 @@ i2cEncoderLibV2 Encoder(0x61); /* For make the address 0x61 only the jumpers A0, #define OLED_RESET -1 Adafruit_SSD1306 display(OLED_RESET); -void setup(void) -{ +void setup(void) { pinMode(IntPin, INPUT); /* - INT_DATA= The register are considered integer. - WRAP_DISABLE= The WRAP option is disabled - DIRE_LEFT= Encoder left direction increase the value - IPUP_ENABLE= INT pin have the pull-up enabled. - RMOD_X1= Encoder configured as X1. - RGB_ENCODER= type of encoder is RGB, change to STD_ENCODER in case you are using a normal rotary encoder. + INT_DATA= The register are considered integer. + WRAP_DISABLE= The WRAP option is disabled + DIRE_LEFT= Encoder left direction increase the value + IPUP_ENABLE= INT pin have the pull-up enabled. + RMOD_X1= Encoder configured as X1. + RGB_ENCODER= type of encoder is RGB, change to STD_ENCODER in case you are using a normal rotary encoder. */ Wire.begin(); Encoder.reset(); /* Reset the I2C encoder V2 and wait 100ms */ - - Encoder.begin(INT_DATA | WRAP_DISABLE | DIRE_LEFT | IPUP_ENABLE | RMOD_X1 | RGB_ENCODER); - // Encoder.begin(INT_DATA | WRAP_DISABLE | DIRE_LEFT | IPUP_ENABLE | RMOD_X1 | STD_ENCODER); // try also this! - // Encoder.begin(INT_DATA | WRAP_ENABLE | DIRE_LEFT | IPUP_ENABLE | RMOD_X1 | RGB_ENCODER); // try also this! - Encoder.writeCounter((int32_t)0); /* Reset the counter value */ - Encoder.writeMax((int32_t)10); /* Set the maximum threshold*/ + Encoder.begin( + i2cEncoderLibV2::INT_DATA | i2cEncoderLibV2::WRAP_DISABLE + | i2cEncoderLibV2::DIRE_LEFT | i2cEncoderLibV2::IPUP_ENABLE + | i2cEncoderLibV2::RMOD_X1 | i2cEncoderLibV2::RGB_ENCODER); + // Encoder.begin(i2cEncoderLibV2::INT_DATA | i2cEncoderLibV2::WRAP_DISABLE | i2cEncoderLibV2::DIRE_LEFT | i2cEncoderLibV2::PUP_ENABLE | i2cEncoderLibV2::RMOD_X1 | i2cEncoderLibV2::STD_ENCODER); // try also this! + // Encoder.begin(i2cEncoderLibV2::INT_DATA | i2cEncoderLibV2::WRAP_ENABLE | i2cEncoderLibV2::DIRE_LEFT | i2cEncoderLibV2::IPUP_ENABLE | i2cEncoderLibV2::RMOD_X1 | i2cEncoderLibV2::RGB_ENCODER); // try also this! + + Encoder.writeCounter((int32_t) 0); /* Reset the counter value */ + Encoder.writeMax((int32_t) 10); /* Set the maximum threshold*/ Encoder.writeMin((int32_t) - 10); /* Set the minimum threshold */ - Encoder.writeStep((int32_t)1); /* Set the step to 1*/ - Encoder.writeInterruptConfig(INT2 | RMIN | RMAX | RDEC | RINC | PUSHR ); /* Enable all the interrupt */ - Encoder.writeAntibouncingPeriod(20); /* Set an anti-bouncing of 300ms */ - Encoder.writeDoublePushPeriod(0); /*Set a period for the double push of 500ms */ + Encoder.writeStep((int32_t) 1); /* Set the step to 1*/ + Encoder.writeInterruptConfig( + i2cEncoderLibV2::INT_2 | i2cEncoderLibV2::RMIN + | i2cEncoderLibV2::RMAX | i2cEncoderLibV2::RDEC + | i2cEncoderLibV2::RINC | i2cEncoderLibV2::PUSHR); /* Enable all the interrupt */ + Encoder.writeAntibouncingPeriod(20); /* Set an anti-bouncing of 300ms */ + Encoder.writeDoublePushPeriod(0); /*Set a period for the double push of 500ms */ display.begin(SSD1306_SWITCHCAPVCC, 0x3C); @@ -67,14 +71,14 @@ void setup(void) void loop() { uint8_t enc_cnt; if (digitalRead(IntPin) == LOW) { - if ( Encoder.updateStatus()) { + if (Encoder.updateStatus()) { display.clearDisplay(); - if ( Encoder.readStatus(RINC)) { + if (Encoder.readStatus(i2cEncoderLibV2::RINC)) { display.setCursor(0, 0); display.setTextSize(3); - display.print( Encoder.readCounterByte()); + display.print(Encoder.readCounterByte()); display.setCursor(0, 25); display.setTextSize(1); display.print("Increment"); @@ -85,10 +89,10 @@ void loop() { /* Write here your code */ } - if ( Encoder.readStatus(RDEC)) { + if (Encoder.readStatus(i2cEncoderLibV2::RDEC)) { display.setCursor(0, 0); display.setTextSize(3); - display.print( Encoder.readCounterByte()); + display.print(Encoder.readCounterByte()); display.setCursor(0, 25); display.setTextSize(1); display.print("Decrement"); @@ -100,10 +104,10 @@ void loop() { } - if ( Encoder.readStatus(RMAX)) { + if (Encoder.readStatus(i2cEncoderLibV2::RMAX)) { display.setCursor(0, 0); display.setTextSize(3); - display.println( Encoder.readCounterByte()); + display.println(Encoder.readCounterByte()); display.setCursor(0, 35); display.setTextSize(1); display.print("Maximum"); @@ -111,24 +115,23 @@ void loop() { } - if ( Encoder.readStatus(RMIN)) { + if (Encoder.readStatus(i2cEncoderLibV2::RMIN)) { display.setCursor(0, 0); display.setTextSize(3); - display.print( Encoder.readCounterByte()); + display.print(Encoder.readCounterByte()); display.setCursor(0, 35); display.setTextSize(1); display.print("Minimum"); - /* Write here your code */ } - if ( Encoder.readStatus(PUSHR)) { - + if (Encoder.readStatus(i2cEncoderLibV2::PUSHR)) { + display.setCursor(0, 0); display.setTextSize(3); - display.print( Encoder.readCounterByte()); + display.print(Encoder.readCounterByte()); display.setCursor(0, 25); display.setTextSize(1); display.print("Button pressed!"); @@ -138,4 +141,4 @@ void loop() { display.display(); } } -} \ No newline at end of file +} diff --git a/examples/I2CEncoderV2/WEMOS_OLED_Menu/WEMOS_OLED_Menu.ino b/examples/I2CEncoderV2/WEMOS_OLED_Menu/WEMOS_OLED_Menu.ino index 8562c37..93122f0 100644 --- a/examples/I2CEncoderV2/WEMOS_OLED_Menu/WEMOS_OLED_Menu.ino +++ b/examples/I2CEncoderV2/WEMOS_OLED_Menu/WEMOS_OLED_Menu.ino @@ -5,12 +5,12 @@ #include /*This is an example of using the I2C Encoder V2 for scrolling a simple menu'. - For this example is required the WEMOS and the OLED shield. - There are 4 items: A, B, C, D. Each items has it's own variable. - With the Encoder is possible to select the items and change it's own variable. - When the encoder LED is GREEN it's possible to select the item. - By clicking the encoder the LED became BLUE and it's possible to change the value of the item. - With a long press (>300ms) the LED return GREEN and it's possbile to select another items. + For this example is required the WEMOS and the OLED shield. + There are 4 items: A, B, C, D. Each items has it's own variable. + With the Encoder is possible to select the items and change it's own variable. + When the encoder LED is GREEN it's possible to select the item. + By clicking the encoder the LED became BLUE and it's possible to change the value of the item. + With a long press (>300ms) the LED return GREEN and it's possbile to select another items. Connections with WEMOS board: @@ -21,7 +21,6 @@ INT -> D3 */ - const int IntPin = 0; /* Definition of the interrupt pin*/ //Class initialization with the I2C addresses i2cEncoderLibV2 Encoder(0x61); /* For make the address 0x61 only the jumpers A0, A5 and A6 are soldered.*/ @@ -30,35 +29,37 @@ i2cEncoderLibV2 Encoder(0x61); /* For make the address 0x61 only the jumpers A0, Adafruit_SSD1306 display(OLED_RESET); uint8_t m_pos = 0; -int8_t val[4] = {0}; /* array where it's possbile to store the 4 value of the items */ -int8_t max_val[4] = {10, 5, 20, 11}; /* array where is store the max value of each items. Customize according to your necessity */ -int8_t min_val[4] = { -3, 0, 3, -11}; /* array where is store the min value of each items. Customize according to your necessity */ +int8_t val[4] = { 0 }; /* array where it's possbile to store the 4 value of the items */ +int8_t max_val[4] = { 10, 5, 20, 11 }; /* array where is store the max value of each items. Customize according to your necessity */ +int8_t min_val[4] = { -3, 0, 3, -11 }; /* array where is store the min value of each items. Customize according to your necessity */ -bool insert = false; /* if it's false is the item value selection, when it's true is the item selection */ +bool insert = false; /* if it's false is the item value selection, when it's true is the item selection */ -void setup(void) -{ +void setup(void) { Wire.begin(); Encoder.reset(); /* Reset the I2C encoder V2 and wait 100ms */ pinMode(IntPin, INPUT); /* - INT_DATA= The register are considered integer. - WRAP_ENABLE= The WRAP option is enabled - DIRE_RIGHT= Encoder right direction increase the value - IPUP_ENABLE= INT pin have the pull-up enabled. - RMOD_X1= Encoder configured as X1. - RGB_ENCODER= type of encoder is RGB, change to STD_ENCODER in case you are using a normal rotary encoder. + INT_DATA= The register are considered integer. + WRAP_ENABLE= The WRAP option is enabled + DIRE_RIGHT= Encoder right direction increase the value + IPUP_ENABLE= INT pin have the pull-up enabled. + RMOD_X1= Encoder configured as X1. + RGB_ENCODER= type of encoder is RGB, change to STD_ENCODER in case you are using a normal rotary encoder. */ - Encoder.begin(INT_DATA | WRAP_ENABLE | DIRE_RIGHT | IPUP_ENABLE | RMOD_X1 | RGB_ENCODER); - Encoder.writeCounter((int32_t)0); /* Reset the counter value */ - Encoder.writeMax((int32_t)3); /* Set the maximum threshold*/ - Encoder.writeMin((int32_t)0); /* Set the minimum threshold */ - Encoder.writeStep((int32_t)1); /* Set the step to 1*/ + Encoder.begin( + i2cEncoderLibV2::INT_DATA | i2cEncoderLibV2::WRAP_ENABLE + | i2cEncoderLibV2::DIRE_RIGHT | i2cEncoderLibV2::IPUP_ENABLE + | i2cEncoderLibV2::RMOD_X1 | i2cEncoderLibV2::RGB_ENCODER); + Encoder.writeCounter((int32_t) 0); /* Reset the counter value */ + Encoder.writeMax((int32_t) 3); /* Set the maximum threshold*/ + Encoder.writeMin((int32_t) 0); /* Set the minimum threshold */ + Encoder.writeStep((int32_t) 1); /* Set the step to 1*/ Encoder.writeInterruptConfig(0xff); /* Enable all the interrupt */ - Encoder.writeAntibouncingPeriod(30); /* Set an anti-bouncing of 300ms */ - Encoder.writeDoublePushPeriod(30); /*Set a period for the double push of 300ms */ + Encoder.writeAntibouncingPeriod(30); /* Set an anti-bouncing of 300ms */ + Encoder.writeDoublePushPeriod(30); /*Set a period for the double push of 300ms */ Encoder.writeFadeRGB(1); /*Set fade to 1ms step */ Encoder.writeRGBCode(0x00ff00); /* Turn ON the green LED */ @@ -74,15 +75,15 @@ void setup(void) } - void loop() { if (digitalRead(IntPin) == LOW) { - if ( Encoder.updateStatus()) { + if (Encoder.updateStatus()) { - if ( Encoder.readStatus(RINC) || Encoder.readStatus(RDEC)) { + if (Encoder.readStatus(i2cEncoderLibV2::RINC) + || Encoder.readStatus(i2cEncoderLibV2::RDEC)) { if (insert == false) { - m_pos = Encoder.readCounterByte(); /* change the item */ + m_pos = Encoder.readCounterByte(); /* change the item */ } if (insert == true) { val[m_pos] = Encoder.readCounterByte(); /* change the item value */ @@ -90,23 +91,24 @@ void loop() { } /* fast push, switch to the item value change mode */ - if ( Encoder.readStatus(PUSHP) && Encoder.readStatus(PUSHR)) { + if (Encoder.readStatus(i2cEncoderLibV2::PUSHP) + && Encoder.readStatus(i2cEncoderLibV2::PUSHR)) { insert = true; - Encoder.writeCounter((int32_t)val[m_pos]); /* Reset the counter value */ - Encoder.writeMax((int32_t)max_val[m_pos] ); /* Set the maximum threshold*/ - Encoder.writeMin((int32_t)min_val[m_pos]); /* Set the minimum threshold */ - Encoder.writeStep((int32_t)1); /* Set the step to 1*/ + Encoder.writeCounter((int32_t) val[m_pos]); /* Reset the counter value */ + Encoder.writeMax((int32_t) max_val[m_pos]); /* Set the maximum threshold*/ + Encoder.writeMin((int32_t) min_val[m_pos]); /* Set the minimum threshold */ + Encoder.writeStep((int32_t) 1); /* Set the step to 1*/ Encoder.writeRGBCode(0x0000FF); } - /* long push, switch to the selection mode */ - if ( Encoder.readStatus(PUSHR) && (Encoder.readStatus(PUSHP) == false)) { + if (Encoder.readStatus(i2cEncoderLibV2::PUSHR) + && (Encoder.readStatus(i2cEncoderLibV2::PUSHP) == false)) { insert = false; - Encoder.writeCounter((int32_t)0); /* Reset the counter value */ - Encoder.writeMax((int32_t)3); /* Set the maximum threshold*/ - Encoder.writeMin((int32_t)0); /* Set the minimum threshold */ - Encoder.writeStep((int32_t)1); /* Set the step to 1*/ + Encoder.writeCounter((int32_t) 0); /* Reset the counter value */ + Encoder.writeMax((int32_t) 3); /* Set the maximum threshold*/ + Encoder.writeMin((int32_t) 0); /* Set the minimum threshold */ + Encoder.writeStep((int32_t) 1); /* Set the step to 1*/ Encoder.writeRGBCode(0x00ff00); } @@ -116,7 +118,6 @@ void loop() { } } - /* fuction that print the menu' */ void menu(void) { display.clearDisplay(); @@ -142,5 +143,4 @@ void menu(void) { display.display(); - } diff --git a/examples/I2CEncoderV2/WEMOS_OLED_Menu_Callback_EEPROM/WEMOS_OLED_Menu_Callback_EEPROM.ino b/examples/I2CEncoderV2/WEMOS_OLED_Menu_Callback_EEPROM/WEMOS_OLED_Menu_Callback_EEPROM.ino index 43d1116..7977dda 100644 --- a/examples/I2CEncoderV2/WEMOS_OLED_Menu_Callback_EEPROM/WEMOS_OLED_Menu_Callback_EEPROM.ino +++ b/examples/I2CEncoderV2/WEMOS_OLED_Menu_Callback_EEPROM/WEMOS_OLED_Menu_Callback_EEPROM.ino @@ -5,13 +5,13 @@ #include /*This is an example of using the I2C Encoder V2 for scrolling a simple menu'. - For this example is required the WEMOS and the OLED shield. - There are 4 items: A, B, C, D. Each items has it's own variable. - With the Encoder is possible to select the items and change it's own variable. - When the encoder LED is GREEN it's possible to select the item. - By clicking the encoder the LED became BLUE and it's possible to change the value of the item. - With a double push the item values is store in the EEPROM of the encoder. - The LED return GREEN and again it's possbile to select another item. + For this example is required the WEMOS and the OLED shield. + There are 4 items: A, B, C, D. Each items has it's own variable. + With the Encoder is possible to select the items and change it's own variable. + When the encoder LED is GREEN it's possible to select the item. + By clicking the encoder the LED became BLUE and it's possible to change the value of the item. + With a double push the item values is store in the EEPROM of the encoder. + The LED return GREEN and again it's possbile to select another item. Connections with WEMOS board: @@ -22,7 +22,6 @@ INT -> D3 */ - const int IntPin = 0; /* Definition of the interrupt pin*/ //Class initialization with the I2C addresses i2cEncoderLibV2 Encoder(0x61); /* For make the address 0x61 only the jumpers A0, A5 and A6 are soldered.*/ @@ -33,16 +32,15 @@ i2cEncoderLibV2 Encoder(0x61); /* For make the address 0x61 only the jumpers A0, Adafruit_SSD1306 display(OLED_RESET); uint8_t m_pos = 0; -int8_t val[4] = {0}; /* array where it's possbile to store the 4 value of the items */ -int8_t max_val[4] = {10, 5, 20, 11}; /* array where is store the max value of each items. Customize according to your necessity */ -int8_t min_val[4] = { -3, 0, 3, -11}; /* array where is store the min value of each items. Customize according to your necessity */ - -bool insert = false; /* if it's false is the item value selection, when it's true is the item selection */ +int8_t val[4] = { 0 }; /* array where it's possbile to store the 4 value of the items */ +int8_t max_val[4] = { 10, 5, 20, 11 }; /* array where is store the max value of each items. Customize according to your necessity */ +int8_t min_val[4] = { -3, 0, 3, -11 }; /* array where is store the min value of each items. Customize according to your necessity */ +bool insert = false; /* if it's false is the item value selection, when it's true is the item selection */ void encoder_rotated(i2cEncoderLibV2* obj) { if (insert == false) { - m_pos = obj->readCounterByte(); /* change the item */ + m_pos = obj->readCounterByte(); /* change the item */ Serial.println(m_pos); } if (insert == true) { @@ -53,59 +51,61 @@ void encoder_rotated(i2cEncoderLibV2* obj) { void encoder_click(i2cEncoderLibV2* obj) { insert = true; - obj->writeCounter((int32_t)val[m_pos]); /* Reset the counter value */ - obj->writeMax((int32_t)max_val[m_pos] ); /* Set the maximum threshold*/ - obj->writeMin((int32_t)min_val[m_pos]); /* Set the minimum threshold */ - obj->writeStep((int32_t)1); /* Set the step to 1*/ + obj->writeCounter((int32_t) val[m_pos]); /* Reset the counter value */ + obj->writeMax((int32_t) max_val[m_pos]); /* Set the maximum threshold*/ + obj->writeMin((int32_t) min_val[m_pos]); /* Set the minimum threshold */ + obj->writeStep((int32_t) 1); /* Set the step to 1*/ obj->writeRGBCode(0x0000FF); obj->onButtonDoublePush = encoder_Double_push; // Enable the double push interrupt } void encoder_Double_push(i2cEncoderLibV2* obj) { insert = false; - obj->writeCounter((int32_t)0); /* Reset the counter value */ - obj->writeMax((int32_t)3); /* Set the maximum threshold*/ - obj->writeMin((int32_t)0); /* Set the minimum threshold */ - obj->writeStep((int32_t)1); /* Set the step to 1*/ + obj->writeCounter((int32_t) 0); /* Reset the counter value */ + obj->writeMax((int32_t) 3); /* Set the maximum threshold*/ + obj->writeMin((int32_t) 0); /* Set the minimum threshold */ + obj->writeStep((int32_t) 1); /* Set the step to 1*/ obj->writeRGBCode(0x00ff00); - obj->writeEEPROM(EEPROM_START_ADD + m_pos, (uint8_t)val[m_pos]); + obj->writeEEPROM(EEPROM_START_ADD + m_pos, (uint8_t) val[m_pos]); obj->onButtonDoublePush = NULL; // Disable the double push interrupt } // Interurpt function when the INT pin goes low void encoder_interrupt(void) { - if ( Encoder.updateStatus()) { + if (Encoder.updateStatus()) { menu(); } } -void setup(void) -{ +void setup(void) { Serial.begin(115200); Wire.begin(); Encoder.reset(); /* Reset the I2C encoder V2 and wait 100ms */ pinMode(IntPin, INPUT); - attachInterrupt(digitalPinToInterrupt(IntPin), encoder_interrupt, FALLING ); // Enable the interrupt on the INT pin + attachInterrupt(digitalPinToInterrupt(IntPin), encoder_interrupt, FALLING); // Enable the interrupt on the INT pin /* - INT_DATA= The register are considered integer. - WRAP_ENABLE= The WRAP option is enabled - DIRE_RIGHT= Encoder right direction increase the value - IPUP_ENABLE= INT pin have the pull-up enabled. - RMOD_X1= Encoder configured as X1. - RGB_ENCODER= type of encoder is RGB, change to STD_ENCODER in case you are using a normal rotary encoder. + INT_DATA= The register are considered integer. + WRAP_ENABLE= The WRAP option is enabled + DIRE_RIGHT= Encoder right direction increase the value + IPUP_ENABLE= INT pin have the pull-up enabled. + RMOD_X1= Encoder configured as X1. + RGB_ENCODER= type of encoder is RGB, change to STD_ENCODER in case you are using a normal rotary encoder. */ - Encoder.begin(INT_DATA | WRAP_ENABLE | DIRE_RIGHT | IPUP_ENABLE | RMOD_X1 | RGB_ENCODER); - Encoder.writeCounter((int32_t)0); /* Reset the counter value */ - Encoder.writeMax((int32_t)3); /* Set the maximum threshold*/ - Encoder.writeMin((int32_t)0); /* Set the minimum threshold */ - Encoder.writeStep((int32_t)1); /* Set the step to 1*/ - Encoder.writeAntibouncingPeriod(30); /* Set an anti-bouncing of 300ms */ - Encoder.writeDoublePushPeriod(50); /*Set a period for the double push of 300ms */ + Encoder.begin( + i2cEncoderLibV2::INT_DATA | i2cEncoderLibV2::WRAP_ENABLE + | i2cEncoderLibV2::DIRE_RIGHT | i2cEncoderLibV2::IPUP_ENABLE + | i2cEncoderLibV2::RMOD_X1 | i2cEncoderLibV2::RGB_ENCODER); + Encoder.writeCounter((int32_t) 0); /* Reset the counter value */ + Encoder.writeMax((int32_t) 3); /* Set the maximum threshold*/ + Encoder.writeMin((int32_t) 0); /* Set the minimum threshold */ + Encoder.writeStep((int32_t) 1); /* Set the step to 1*/ + Encoder.writeAntibouncingPeriod(30); /* Set an anti-bouncing of 300ms */ + Encoder.writeDoublePushPeriod(50); /*Set a period for the double push of 300ms */ Encoder.writeFadeRGB(1); /*Set fade to 1ms step */ Encoder.writeRGBCode(0x00ff00); /* Turn ON the green LED */ @@ -116,9 +116,6 @@ void setup(void) /* Enable the I2C Encoder V2 interrupts according to the previus attached callback */ Encoder.autoconfigInterrupt(); - - - display.begin(SSD1306_SWITCHCAPVCC, 0x3C); /* initialize the item values to the minimum value */ @@ -127,21 +124,19 @@ void setup(void) val[i] = Encoder.readEEPROM(EEPROM_START_ADD + i); - if ( (val[i] > max_val[i]) || (val[i] < min_val[i])) { + if ((val[i] > max_val[i]) || (val[i] < min_val[i])) { val[i] = min_val[i]; - Encoder.writeEEPROM(EEPROM_START_ADD + i, (uint8_t)val[i]); + Encoder.writeEEPROM(EEPROM_START_ADD + i, (uint8_t) val[i]); } } menu(); } - void loop() { //Nothin in the loop, } - /* fuction that print the menu' */ void menu(void) { display.clearDisplay(); @@ -167,5 +162,4 @@ void menu(void) { display.display(); - -} \ No newline at end of file +} diff --git a/src/i2cEncoderLibV2.cpp b/src/i2cEncoderLibV2.cpp index 03710ee..1cba948 100644 --- a/src/i2cEncoderLibV2.cpp +++ b/src/i2cEncoderLibV2.cpp @@ -32,89 +32,9 @@ void i2cEncoderLibV2::reset(void) { writeEncoder(REG_GCONF, (uint8_t) 0x80); delay(10); } -/*********************************** Read functions *************************************/ - -/** Return the GP1 Configuration**/ -uint8_t i2cEncoderLibV2::readGP1conf(void) { - return (readEncoderByte(REG_GP1CONF)); -} - -/** Return the GP1 Configuration**/ -uint8_t i2cEncoderLibV2::readGP2conf(void) { - return (readEncoderByte(REG_GP2CONF)); -} - -/** Return the GP1 Configuration**/ -uint8_t i2cEncoderLibV2::readGP3conf(void) { - return (readEncoderByte(REG_GP3CONF)); -} - -/** Return the INT pin configuration**/ -uint8_t i2cEncoderLibV2::readInterruptConfig(void) { - return (readEncoderByte(REG_INTCONF)); -} - -/** Check if there is some attached callbacke and enable the corresponding interrupt **/ -void i2cEncoderLibV2::autoconfigInterrupt(void) { - uint8_t reg; - - if (onButtonRelease != NULL) - reg |= PUSHR; - - if (onButtonPush != NULL) - reg |= PUSHP; - - if (onButtonDoublePush != NULL) - reg |= PUSHD; - - if (onIncrement != NULL) - reg |= RINC; - - if (onDecrement != NULL) - reg |= RDEC; - - if (onChange != NULL) { - reg |= RINC; - reg |= RDEC; - } - - if (onMax != NULL) - reg |= RMAX; - - if (onMin != NULL) - reg |= RMIN; - - if (onMinMax != NULL) { - reg |= RMAX; - reg |= RMIN; - } - - if (onGP1Rise != NULL) - reg |= INT2; - - if (onGP1Fall != NULL) - reg |= INT2; - - if (onGP2Rise != NULL) - reg |= INT2; - - if (onGP2Fall != NULL) - reg |= INT2; - - if (onGP3Rise != NULL) - reg |= INT2; - - if (onGP3Fall != NULL) - reg |= INT2; - - if (onFadeProcess != NULL) - reg |= INT2; - - writeEncoder(REG_INTCONF, (uint8_t) reg); -} /** Call che attached callaback if it is defined. It's a prive function only **/ -void i2cEncoderLibV2::eventCaller(InterruptFunction *event) { +void i2cEncoderLibV2::eventCaller(Callback *event) { if (*event != NULL) (*event)(this); } @@ -155,7 +75,7 @@ bool i2cEncoderLibV2::updateStatus(void) { eventCaller (&onMinMax); } - if ((_stat & INT2) != 0) { + if ((_stat & INT_2) != 0) { _stat2 = readEncoderByte(REG_I2STATUS); if (_stat2 == 0) { return true; @@ -187,6 +107,28 @@ bool i2cEncoderLibV2::updateStatus(void) { return true; } +/*********************************** Read functions *************************************/ + +/** Return the GP1 Configuration**/ +uint8_t i2cEncoderLibV2::readGP1conf(void) { + return (readEncoderByte(REG_GP1CONF)); +} + +/** Return the GP1 Configuration**/ +uint8_t i2cEncoderLibV2::readGP2conf(void) { + return (readEncoderByte(REG_GP2CONF)); +} + +/** Return the GP1 Configuration**/ +uint8_t i2cEncoderLibV2::readGP3conf(void) { + return (readEncoderByte(REG_GP3CONF)); +} + +/** Return the INT pin configuration**/ +uint8_t i2cEncoderLibV2::readInterruptConfig(void) { + return (readEncoderByte(REG_INTCONF)); +} + /** Check if a particular status match, return true is match otherwise false. Before require updateStatus() **/ bool i2cEncoderLibV2::readStatus(Int_Status s) { if ((_stat & s) != 0) { @@ -366,6 +308,65 @@ void i2cEncoderLibV2::writeInterruptConfig(uint8_t interrupt) { writeEncoder(REG_INTCONF, (uint8_t) interrupt); } +/** Check if there is some attached callback and enable the corresponding interrupt **/ +void i2cEncoderLibV2::autoconfigInterrupt(void) { + uint8_t reg; + + if (onButtonRelease != NULL) + reg |= PUSHR; + + if (onButtonPush != NULL) + reg |= PUSHP; + + if (onButtonDoublePush != NULL) + reg |= PUSHD; + + if (onIncrement != NULL) + reg |= RINC; + + if (onDecrement != NULL) + reg |= RDEC; + + if (onChange != NULL) { + reg |= RINC; + reg |= RDEC; + } + + if (onMax != NULL) + reg |= RMAX; + + if (onMin != NULL) + reg |= RMIN; + + if (onMinMax != NULL) { + reg |= RMAX; + reg |= RMIN; + } + + if (onGP1Rise != NULL) + reg |= INT_2; + + if (onGP1Fall != NULL) + reg |= INT_2; + + if (onGP2Rise != NULL) + reg |= INT_2; + + if (onGP2Fall != NULL) + reg |= INT_2; + + if (onGP3Rise != NULL) + reg |= INT_2; + + if (onGP3Fall != NULL) + reg |= INT_2; + + if (onFadeProcess != NULL) + reg |= INT_2; + + writeEncoder(REG_INTCONF, (uint8_t) reg); +} + /** Write the counter value **/ void i2cEncoderLibV2::writeCounter(int32_t value) { writeEncoder(REG_CVALB4, value); diff --git a/src/i2cEncoderLibV2.h b/src/i2cEncoderLibV2.h index 50e92af..2053d7e 100644 --- a/src/i2cEncoderLibV2.h +++ b/src/i2cEncoderLibV2.h @@ -21,271 +21,255 @@ #include #endif -/*Encoder register definition*/ -enum I2C_Register { - REG_GCONF = 0x00, - REG_GP1CONF = 0x01, - REG_GP2CONF = 0x02, - REG_GP3CONF = 0x03, - REG_INTCONF = 0x04, - REG_ESTATUS = 0x05, - REG_I2STATUS = 0x06, - REG_FSTATUS = 0x07, - REG_CVALB4 = 0x08, - REG_CVALB3 = 0x09, - REG_CVALB2 = 0x0A, - REG_CVALB1 = 0x0B, - REG_CMAXB4 = 0x0C, - REG_CMAXB3 = 0x0D, - REG_CMAXB2 = 0x0E, - REG_CMAXB1 = 0x0F, - REG_CMINB4 = 0x10, - REG_CMINB3 = 0x11, - REG_CMINB2 = 0x12, - REG_CMINB1 = 0x13, - REG_ISTEPB4 = 0x14, - REG_ISTEPB3 = 0x15, - REG_ISTEPB2 = 0x16, - REG_ISTEPB1 = 0x17, - REG_RLED = 0x18, - REG_GLED = 0x19, - REG_BLED = 0x1A, - REG_GP1REG = 0x1B, - REG_GP2REG = 0x1C, - REG_GP3REG = 0x1D, - REG_ANTBOUNC = 0x1E, - REG_DPPERIOD = 0x1F, - REG_FADERGB = 0x20, - REG_FADEGP = 0x21, - REG_EEPROMS = 0x80, -}; - -/* Encoder configuration bit. Use with GCONF */ -enum GCONF_PARAMETER { - FLOAT_DATA = 0x01, - INT_DATA = 0x00, - - WRAP_ENABLE = 0x02, - WRAP_DISABLE = 0x00, - - DIRE_LEFT = 0x04, - DIRE_RIGHT = 0x00, - - IPUP_DISABLE = 0x08, - IPUP_ENABLE = 0x00, - - RMOD_X2 = 0x10, - RMOD_X1 = 0x00, - - RGB_ENCODER = 0x20, - STD_ENCODER = 0x00, - - EEPROM_BANK1 = 0x40, - EEPROM_BANK2 = 0x00, - - RESET = 0x80, -}; - -/* Encoder status bits and setting. Use with: INTCONF for set and with ESTATUS for read the bits */ -enum Int_Status { - PUSHR = 0x01, - PUSHP = 0x02, - PUSHD = 0x04, - RINC = 0x08, - RDEC = 0x10, - RMAX = 0x20, - RMIN = 0x40, - INT2 = 0x80, -}; - -/* Encoder Int2 bits. Use to read the bits of I2STATUS */ -enum Int2_Status { - GP1_POS = 0x01, - GP1_NEG = 0x02, - GP2_POS = 0x04, - GP2_NEG = 0x08, - GP3_POS = 0x10, - GP3_NEG = 0x20, - FADE_INT = 0x40, -}; - -/* Encoder Fade status bits. Use to read the bits of FSTATUS */ -enum Fade_Status { - FADE_R = 0x01, - FADE_G = 0x02, - FADE_B = 0x04, - FADES_GP1 = 0x08, - FADES_GP2 = 0x10, - FADES_GP3 = 0x20, -}; - -/* GPIO Configuration. Use with GP1CONF,GP2CONF,GP3CONF */ -enum GP_PARAMETER { - GP_PWM = 0x00, - GP_OUT = 0x01, - GP_AN = 0x02, - GP_IN = 0x03, - - GP_PULL_EN = 0x04, - GP_PULL_DI = 0x00, - - GP_INT_DI = 0x00, - GP_INT_PE = 0x08, - GP_INT_NE = 0x10, - GP_INT_BE = 0x18, -}; - - -union Data_v { - float fval; - int32_t val; - uint8_t bval[4]; -}; +class i2cEncoderLibV2 { +public: + + /*Encoder register definition*/ + enum I2C_Register { + REG_GCONF = 0x00, + REG_GP1CONF = 0x01, + REG_GP2CONF = 0x02, + REG_GP3CONF = 0x03, + REG_INTCONF = 0x04, + REG_ESTATUS = 0x05, + REG_I2STATUS = 0x06, + REG_FSTATUS = 0x07, + REG_CVALB4 = 0x08, + REG_CVALB3 = 0x09, + REG_CVALB2 = 0x0A, + REG_CVALB1 = 0x0B, + REG_CMAXB4 = 0x0C, + REG_CMAXB3 = 0x0D, + REG_CMAXB2 = 0x0E, + REG_CMAXB1 = 0x0F, + REG_CMINB4 = 0x10, + REG_CMINB3 = 0x11, + REG_CMINB2 = 0x12, + REG_CMINB1 = 0x13, + REG_ISTEPB4 = 0x14, + REG_ISTEPB3 = 0x15, + REG_ISTEPB2 = 0x16, + REG_ISTEPB1 = 0x17, + REG_RLED = 0x18, + REG_GLED = 0x19, + REG_BLED = 0x1A, + REG_GP1REG = 0x1B, + REG_GP2REG = 0x1C, + REG_GP3REG = 0x1D, + REG_ANTBOUNC = 0x1E, + REG_DPPERIOD = 0x1F, + REG_FADERGB = 0x20, + REG_FADEGP = 0x21, + REG_EEPROMS = 0x80, + }; + + /* Encoder configuration bit. Use with GCONF */ + enum GCONF_PARAMETER { + FLOAT_DATA = 0x01, + INT_DATA = 0x00, + WRAP_ENABLE = 0x02, + WRAP_DISABLE = 0x00, + DIRE_LEFT = 0x04, + DIRE_RIGHT = 0x00, + IPUP_DISABLE = 0x08, + IPUP_ENABLE = 0x00, + RMOD_X2 = 0x10, + RMOD_X1 = 0x00, + RGB_ENCODER = 0x20, + STD_ENCODER = 0x00, + EEPROM_BANK1 = 0x40, + EEPROM_BANK2 = 0x00, + RESET = 0x80, + }; + + /* Encoder status bits and setting. Use with: INTCONF for set and with ESTATUS for read the bits */ + enum Int_Status { + PUSHR = 0x01, + PUSHP = 0x02, + PUSHD = 0x04, + RINC = 0x08, + RDEC = 0x10, + RMAX = 0x20, + RMIN = 0x40, + INT_2 = 0x80, + + }; + + /* Encoder Int2 bits. Use to read the bits of I2STATUS */ + typedef enum { + GP1_POS = 0x01, + GP1_NEG = 0x02, + GP2_POS = 0x04, + GP2_NEG = 0x08, + GP3_POS = 0x10, + GP3_NEG = 0x20, + FADE_INT = 0x40, + } Int2_Status; + + /* Encoder Fade status bits. Use to read the bits of FSTATUS */ + typedef enum { + FADE_R = 0x01, + FADE_G = 0x02, + FADE_B = 0x04, + FADES_GP1 = 0x08, + FADES_GP2 = 0x10, + FADES_GP3 = 0x20, + } Fade_Status; + + /* GPIO Configuration. Use with GP1CONF,GP2CONF,GP3CONF */ + enum GP_PARAMETER { + GP_PWM = 0x00, + GP_OUT = 0x01, + GP_AN = 0x02, + GP_IN = 0x03, + GP_PULL_EN = 0x04, + GP_PULL_DI = 0x00, + GP_INT_DI = 0x00, + GP_INT_PE = 0x08, + GP_INT_NE = 0x10, + GP_INT_BE = 0x18, + }; + + union Data_v { + float fval; + int32_t val; + uint8_t bval[4]; + }; + + uint8_t id = 0x00; + typedef void (*Callback)(i2cEncoderLibV2*); -class i2cEncoderLibV2 -{ - public: - - uint8_t id = 0x00; - typedef void (*InterruptFunction) (i2cEncoderLibV2*); - /* Event */ - InterruptFunction onButtonRelease = NULL; - InterruptFunction onButtonPush = NULL; - InterruptFunction onButtonDoublePush = NULL; - InterruptFunction onIncrement = NULL; - InterruptFunction onDecrement = NULL; - InterruptFunction onChange = NULL; - InterruptFunction onMax = NULL; - InterruptFunction onMin = NULL; - InterruptFunction onMinMax = NULL; - InterruptFunction onGP1Rise = NULL; - InterruptFunction onGP1Fall = NULL; - InterruptFunction onGP2Rise = NULL; - InterruptFunction onGP2Fall = NULL; - InterruptFunction onGP3Rise = NULL; - InterruptFunction onGP3Fall = NULL; - InterruptFunction onFadeProcess = NULL; - - /** Configuration function **/ - i2cEncoderLibV2(uint8_t add); - void begin( uint8_t conf); - void reset(void); - - /** Configuration of callback **/ - void autoconfigInterrupt(void); - - /** Read functions **/ - uint8_t readGP1conf(void); - uint8_t readGP2conf(void); - uint8_t readGP3conf(void); - uint8_t readInterruptConfig(void); - - /** Status function **/ - bool updateStatus(void); - bool readStatus(Int_Status s); - uint8_t readStatus(void); - - bool readInt2(Int2_Status s); - uint8_t readInt2(void); - - bool readFadeStatus(Fade_Status s); - uint8_t readFadeStatus(void); - - /** Encoder functions **/ - float readCounterFloat(void); - int32_t readCounterLong(void); - int16_t readCounterInt(void); - int8_t readCounterByte(void); - - int32_t readMax(void); - float readMaxFloat(void); - - int32_t readMin(void); - float readMinFloat(void); - - int32_t readStep(void); - float readStepFloat(void); - - /** RGB LED Functions **/ - uint8_t readLEDR(void); - uint8_t readLEDG(void); - uint8_t readLEDB(void); - - /** GP LED Functions **/ - uint8_t readGP1(void); - uint8_t readGP2(void); - uint8_t readGP3(void); - - /** Timing registers **/ - uint8_t readAntibouncingPeriod(void); - uint8_t readDoublePushPeriod(void); - uint8_t readFadeRGB(void); - uint8_t readFadeGP(void); - - /** EEPROM register **/ - uint8_t readEEPROM(uint8_t add); - - - - - /****** Write functions ********/ - void writeGP1conf(uint8_t gp1); - void writeGP2conf(uint8_t gp2); - void writeGP3conf(uint8_t gp3); - void writeInterruptConfig(uint8_t interrupt); - - /** Encoder functions **/ - void writeCounter(int32_t counter); - void writeCounter(float counter); - - void writeMax(int32_t max); - void writeMax(float max); - - void writeMin(int32_t min); - void writeMin(float min); - - void writeStep(int32_t step); - void writeStep(float step); - - /** RGB LED Functions **/ - void writeLEDR(uint8_t rled); - void writeLEDG(uint8_t gled); - void writeLEDB(uint8_t bled); - void writeRGBCode(uint32_t rgb); - - /** GP LED Functions **/ - void writeGP1(uint8_t gp1); - void writeGP2(uint8_t gp2); - void writeGP3(uint8_t gp3); - - /** Timing registers **/ - void writeAntibouncingPeriod(uint8_t bounc); - void writeDoublePushPeriod(uint8_t dperiod); - void writeFadeRGB(uint8_t fade); - void writeFadeGP(uint8_t fade); - - /** EEPROM register **/ - void writeEEPROM(uint8_t add, uint8_t data); - - - private: - - uint8_t _add = 0x00; - uint8_t _stat = 0x00; - uint8_t _stat2 = 0x00; - uint8_t _gconf = 0x00; - union Data_v _tem_data; - - void eventCaller(InterruptFunction *event); - uint8_t readEncoderByte(uint8_t reg); - int16_t readEncoderInt(uint8_t reg); - int32_t readEncoderLong(uint8_t reg); - float readEncoderFloat(uint8_t reg); - void writeEncoder(uint8_t reg, uint8_t data); - void writeEncoder(uint8_t reg, int32_t data); - void writeEncoder(uint8_t reg, float data); - void writeEncoder24bit(uint8_t reg, uint32_t data); + Callback onButtonRelease = NULL; + Callback onButtonPush = NULL; + Callback onButtonDoublePush = NULL; + Callback onIncrement = NULL; + Callback onDecrement = NULL; + Callback onChange = NULL; + Callback onMax = NULL; + Callback onMin = NULL; + Callback onMinMax = NULL; + Callback onGP1Rise = NULL; + Callback onGP1Fall = NULL; + Callback onGP2Rise = NULL; + Callback onGP2Fall = NULL; + Callback onGP3Rise = NULL; + Callback onGP3Fall = NULL; + Callback onFadeProcess = NULL; + + /** Configuration methods **/ + i2cEncoderLibV2(uint8_t add); + void begin(uint8_t conf); + void reset(void); + void autoconfigInterrupt(void); + + /** Read functions **/ + uint8_t readGP1conf(void); + uint8_t readGP2conf(void); + uint8_t readGP3conf(void); + uint8_t readInterruptConfig(void); + + /** Status function **/ + bool updateStatus(void); + bool readStatus(Int_Status s); + uint8_t readStatus(void); + + bool readInt2(Int2_Status s); + uint8_t readInt2(void); + + bool readFadeStatus(Fade_Status s); + uint8_t readFadeStatus(void); + + /** Encoder functions **/ + float readCounterFloat(void); + int32_t readCounterLong(void); + int16_t readCounterInt(void); + int8_t readCounterByte(void); + + int32_t readMax(void); + float readMaxFloat(void); + + int32_t readMin(void); + float readMinFloat(void); + + int32_t readStep(void); + float readStepFloat(void); + + /** RGB LED Functions **/ + uint8_t readLEDR(void); + uint8_t readLEDG(void); + uint8_t readLEDB(void); + + /** GP LED Functions **/ + uint8_t readGP1(void); + uint8_t readGP2(void); + uint8_t readGP3(void); + + /** Timing registers **/ + uint8_t readAntibouncingPeriod(void); + uint8_t readDoublePushPeriod(void); + uint8_t readFadeRGB(void); + uint8_t readFadeGP(void); + + /** EEPROM register **/ + uint8_t readEEPROM(uint8_t add); + + /****** Write functions ********/ + void writeGP1conf(uint8_t gp1); + void writeGP2conf(uint8_t gp2); + void writeGP3conf(uint8_t gp3); + void writeInterruptConfig(uint8_t interrupt); + + /** Encoder functions **/ + void writeCounter(int32_t counter); + void writeCounter(float counter); + + void writeMax(int32_t max); + void writeMax(float max); + + void writeMin(int32_t min); + void writeMin(float min); + + void writeStep(int32_t step); + void writeStep(float step); + + /** RGB LED Functions **/ + void writeLEDR(uint8_t rled); + void writeLEDG(uint8_t gled); + void writeLEDB(uint8_t bled); + void writeRGBCode(uint32_t rgb); + + /** GP LED Functions **/ + void writeGP1(uint8_t gp1); + void writeGP2(uint8_t gp2); + void writeGP3(uint8_t gp3); + + /** Timing registers **/ + void writeAntibouncingPeriod(uint8_t bounc); + void writeDoublePushPeriod(uint8_t dperiod); + void writeFadeRGB(uint8_t fade); + void writeFadeGP(uint8_t fade); + + /** EEPROM register **/ + void writeEEPROM(uint8_t add, uint8_t data); + +private: + + uint8_t _add = 0x00; + uint8_t _stat = 0x00; + uint8_t _stat2 = 0x00; + uint8_t _gconf = 0x00; + union Data_v _tem_data; + + void eventCaller(Callback *event); + uint8_t readEncoderByte(uint8_t reg); + int16_t readEncoderInt(uint8_t reg); + int32_t readEncoderLong(uint8_t reg); + float readEncoderFloat(uint8_t reg); + void writeEncoder(uint8_t reg, uint8_t data); + void writeEncoder(uint8_t reg, int32_t data); + void writeEncoder(uint8_t reg, float data); + void writeEncoder24bit(uint8_t reg, uint32_t data); };