From 9631e6212c5849abb2a8686ea7d38b43d48363b2 Mon Sep 17 00:00:00 2001 From: FattoreSaimon Date: Sat, 2 Mar 2019 21:12:14 +0100 Subject: [PATCH] examples update --- examples/I2CEncoderV2/Basic/Basic.ino | 42 +++--- .../Basic_with_Callbacks.ino | 120 ++++++++++++++++++ examples/I2CEncoderV2/GPs_ADC/GPs_ADC.ino | 35 ++--- .../WEMOS_OLED_Menu_Callback_EEPROM.ino | 19 +-- 4 files changed, 170 insertions(+), 46 deletions(-) create mode 100644 examples/I2CEncoderV2/Basic_with_Callbacks/Basic_with_Callbacks.ino diff --git a/examples/I2CEncoderV2/Basic/Basic.ino b/examples/I2CEncoderV2/Basic/Basic.ino index efbd9e0..d9c2d84 100644 --- a/examples/I2CEncoderV2/Basic/Basic.ino +++ b/examples/I2CEncoderV2/Basic/Basic.ino @@ -15,8 +15,8 @@ */ const int IntPin = A3; /* Definition of the interrupt pin. You can change according to your board /* -//Class initialization with the I2C addresses -i2cEncoderLibV2 Encoder(0x01); /* A0 is solderedA*/ + //Class initialization with the I2C addresses*/ +i2cEncoderLibV2 Encoder(0x61); /* A0 is solderedA */ void setup(void) @@ -33,11 +33,11 @@ void setup(void) 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.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.writeMin((int32_t) - 10); /* Set the minimum threshold */ @@ -48,7 +48,7 @@ void setup(void) } void loop() { - uint8_t enc_cnt; + if (digitalRead(IntPin) == LOW) { if ( Encoder.updateStatus()) { if ( Encoder.readStatus(RINC)) { @@ -56,52 +56,52 @@ void loop() { Serial.println( Encoder.readCounterByte()); /* Write here your code */ - + } if ( Encoder.readStatus(RDEC)) { Serial.print("Decrement: "); Serial.println( Encoder.readCounterByte()); - + /* Write here your code */ - + } if ( Encoder.readStatus(RMAX)) { Serial.print("Maximum threshold: "); Serial.println( Encoder.readCounterByte()); - + /* Write here your code */ - + } if ( Encoder.readStatus(RMIN)) { Serial.print("Minimum threshold: "); Serial.println( Encoder.readCounterByte()); - + /* Write here your code */ - + } if ( Encoder.readStatus(PUSHR)) { Serial.println("Push button Released"); - + /* Write here your code */ - + } if ( Encoder.readStatus(PUSHP)) { Serial.println("Push button Pressed"); - + /* Write here your code */ - + } if ( Encoder.readStatus(PUSHD)) { Serial.println("Double push!"); - + /* Write here your code */ - + } } } -} \ No newline at end of file +} diff --git a/examples/I2CEncoderV2/Basic_with_Callbacks/Basic_with_Callbacks.ino b/examples/I2CEncoderV2/Basic_with_Callbacks/Basic_with_Callbacks.ino new file mode 100644 index 0000000..d3054bf --- /dev/null +++ b/examples/I2CEncoderV2/Basic_with_Callbacks/Basic_with_Callbacks.ino @@ -0,0 +1,120 @@ +#include +#include + +/*This is a basic example for using the I2C Encoder V2 + The counter is set to work between +10 to -10, at every encoder click the counter value is printed on the terminal. + It's also printet when the push button is pressed, or released or double pushed. + The callback are used instead of the switch case + + Connections with Arduino UNO: + - -> GND + + -> 5V + SDA -> A4 + SCL -> A5 + INT -> A3 +*/ + +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 */ + + +//Callback when the CVAL is incremented +void encoder_increment(i2cEncoderLibV2* obj, SourceInt e) { + Serial.print("Increment: "); + Serial.println( Encoder.readCounterByte()); +} + + + +//Callback when the CVAL is decremented +void encoder_decrement(i2cEncoderLibV2* obj, SourceInt e) { + Serial.print("Decrement: "); + Serial.println( Encoder.readCounterByte()); +} + + + +//Callback when CVAL reach MAX +void encoder_max(i2cEncoderLibV2* obj, SourceInt e) { + Serial.print("Maximum threshold: "); + Serial.println( Encoder.readCounterByte()); +} + + + + +//Callback when CVAL reach MIN +void encoder_min(i2cEncoderLibV2* obj, SourceInt e) { + Serial.print("Minimum threshold: "); + Serial.println( Encoder.readCounterByte()); +} + + + + +//Callback when the encoder is pushed +void encoder_push(i2cEncoderLibV2* obj, SourceInt e) { + Serial.println("Encoder is pushed!"); +} + + + +//Callback when the encoder is released +void encoder_released(i2cEncoderLibV2* obj, SourceInt e) { + Serial.println("Encoder is released"); +} + + + +//Callback when the encoder is double pushed +void encoder_double_push(i2cEncoderLibV2* obj, SourceInt e) { + Serial.println("Encoder is double pushed!"); +} + + + + +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. + */ + 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.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.attachInterrupt(encoder_increment, ENCODER_INCREMENT); + Encoder.attachInterrupt(encoder_decrement, ENCODER_DECREMENT); + Encoder.attachInterrupt(encoder_max, ENCODER_MAX); + Encoder.attachInterrupt(encoder_min, ENCODER_MIN); + Encoder.attachInterrupt(encoder_push, BUTTON_PUSH); + Encoder.attachInterrupt(encoder_released, BUTTON_RELEASE); + Encoder.attachInterrupt(encoder_double_push, BUTTON_DOUBLE_PUSH); + Encoder.autoconfigInterrupt(); /* Enable all the attached interrupt */ + +} + +void loop() { + if (digitalRead(IntPin) == LOW) { + /* Check the status of the encoder and call the callback */ + Encoder.updateStatus(); + } +} diff --git a/examples/I2CEncoderV2/GPs_ADC/GPs_ADC.ino b/examples/I2CEncoderV2/GPs_ADC/GPs_ADC.ino index fad0466..ba142d0 100644 --- a/examples/I2CEncoderV2/GPs_ADC/GPs_ADC.ino +++ b/examples/I2CEncoderV2/GPs_ADC/GPs_ADC.ino @@ -2,10 +2,10 @@ #include /*In this example a standard encoder is used. - * The GP pins are configured as analog and - * the ADC vale is printed on the serial port - * as well as the counter value - * + The GP pins are configured as analog and + the ADC vale is printed on the serial port + as well as the counter value + Connections with Arduino UNO: - -> GND @@ -14,7 +14,7 @@ SCL -> A5 INT -> A3 */ -unsigned long previousMillis = 0; +unsigned long previousMillis = 0; const long interval = 1000; // interval between ADC reading of the GP pins const int IntPin = A3; // Change according to your board configuration @@ -28,22 +28,23 @@ void setup(void) { // Reset the two encoder Wire.begin(); - STDEncoder.reset(); + // Initialize the GPIO and the display pinMode(IntPin, INPUT); pinMode(LED_BUILTIN, OUTPUT); - - //Initialize the Serial port + + //Initialize the Serial port Serial.begin(115200); Serial.print("Encoder Test!!\n"); //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_DI | GP_INT_DI); // Configure the GP pins in analog mode - STDEncoder.writeGP2conf(GP_AN | GP_PULL_DI | GP_INT_DI); // Configure the GP pins in analog mode - STDEncoder.writeGP3conf(GP_IN | GP_PULL_DI | GP_INT_DI); // Configure the GP pins in analog mode + 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.writeCounter((int32_t) 0); STDEncoder.writeMax((int32_t) 10); STDEncoder.writeMin((int32_t) 0); @@ -62,10 +63,10 @@ void loop() { previousMillis = currentMillis; Serial.print("GP1: "); - Serial.println(STDEncoder.readGP1()); //read the analog value of the GP1 - Serial.print("GP2: "); - Serial.println(STDEncoder.readGP2()); //read the analog value of the GP2 - Serial.print("GP3: "); + Serial.print(STDEncoder.readGP1()); //read the analog value of the GP1 + Serial.print("\tGP2: "); + Serial.print(STDEncoder.readGP2()); //read the analog value of the GP2 + Serial.print("\tGP3: "); Serial.println(STDEncoder.readGP3()); //read the analog value of the GP3 } @@ -74,10 +75,10 @@ void loop() { if (digitalRead(IntPin) == LOW) { //Check for the interrupt if (STDEncoder.updateStatus()) { //Check if the normal encoder is moved - + //Print the counter value of the normal encoder Serial.print("Encoder: "); Serial.println(STDEncoder.readCounterInt()); } } -} \ No newline at end of file +} 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 a16c4ff..8de1053 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 @@ -11,7 +11,7 @@ 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. + The LED return GREEN and again it's possbile to select another item. Connections with WEMOS board: @@ -74,6 +74,12 @@ void encoder_Double_pushed(i2cEncoderLibV2* obj, SourceInt e) { } +// Interurpt function when the INT pin goes low +void encoder_interrupt(void) { + if ( Encoder.updateStatus()) { + menu(); + } +} void setup(void) { @@ -82,6 +88,8 @@ void setup(void) 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 + /* INT_DATA= The register are considered integer. WRAP_ENABLE= The WRAP option is enabled @@ -115,7 +123,7 @@ void setup(void) /* initialize the item values to the minimum value */ /* Initialie also the EEPROM contenet */ for (uint8_t i = 0; i < 4; i++) { - + val[i] = Encoder.readEEPROM(EEPROM_START_ADD + i); if ( (val[i] > max_val[i]) || (val[i] < min_val[i])) { @@ -129,12 +137,7 @@ void setup(void) void loop() { - - if (digitalRead(IntPin) == LOW) { - if ( Encoder.updateStatus()) { - menu(); - } - } + //Nothin in the loop, }