|
| 1 | +#include<Wire.h> |
| 2 | +#include <ESP8266WiFi.h> |
| 3 | +#include <Adafruit_GFX.h> |
| 4 | +#include <Adafruit_SSD1306.h> |
| 5 | +#include "i2cEncoderLibV2.h" |
| 6 | + |
| 7 | +/*This is an example of using the I2C Encoder V2 for scrolling a simple menu'. |
| 8 | + For this example is required the WEMOS and the OLED shield. |
| 9 | + There are 4 items: A, B, C, D. Each items has it's own variable. |
| 10 | + With the Encoder is possible to select the items and change it's own variable. |
| 11 | + When the encoder LED is GREEN it's possible to select the item. |
| 12 | + By clicking the encoder the LED became BLUE and it's possible to change the value of the item. |
| 13 | + With a long press (>300ms) the LED return GREEN and it's possbile to select another items. |
| 14 | +
|
| 15 | +
|
| 16 | + Connections with WEMOS board: |
| 17 | + - -> GND |
| 18 | + + -> 3.3V |
| 19 | + SDA -> D2 |
| 20 | + SCL -> D1 |
| 21 | + INT -> D0 |
| 22 | +*/ |
| 23 | + |
| 24 | + |
| 25 | +const int IntPin = 16; /* Definition of the interrupt pin*/ |
| 26 | +//Class initialization with the I2C addresses |
| 27 | +i2cEncoderLibV2 Encoder(0x61); /* For make the address 0x61 only the jumpers A0, A5 and A6 are soldered.*/ |
| 28 | + |
| 29 | +#define OLED_RESET -1 |
| 30 | +Adafruit_SSD1306 display(OLED_RESET); |
| 31 | + |
| 32 | +uint8_t m_pos = 0; |
| 33 | +int8_t val[4] = {0}; /* array where it's possbile to store the 4 value of the items */ |
| 34 | +int8_t max_val[4] = {10, 5, 20, 11}; /* array where is store the max value of each items. Customize according to your necessity */ |
| 35 | +int8_t min_val[4] = { -3, 0, 3, -11}; /* array where is store the min value of each items. Customize according to your necessity */ |
| 36 | + |
| 37 | +bool insert = false; /* if it's false is the item value selection, when it's true is the item selection */ |
| 38 | + |
| 39 | +void setup(void) |
| 40 | +{ |
| 41 | + |
| 42 | + Encoder.begin(RESET); /* Reset the I2C encoder V2 and wait 100ms */ |
| 43 | + delay(100); |
| 44 | + |
| 45 | + pinMode(IntPin, INPUT); |
| 46 | + /* |
| 47 | + INT_DATA= The register are considered integer. |
| 48 | + WRAP_ENABLE= The WRAP option is enabled |
| 49 | + DIRE_RIGHT= Encoder right direction increase the value |
| 50 | + IPUP_ENABLE= INT pin have the pull-up enabled. |
| 51 | + RMOD_X1= Encoder configured as X1. |
| 52 | + RGB_ENCODER= type of encoder is RGB, change to STD_ENCODER in case you are using a normal rotary encoder. |
| 53 | + */ |
| 54 | + |
| 55 | + Encoder.begin(INT_DATA | WRAP_ENABLE | DIRE_RIGHT | IPUP_ENABLE | RMOD_X1 | RGB_ENCODER); |
| 56 | + Encoder.writeCounter((int32_t)0); /* Reset the counter value */ |
| 57 | + Encoder.writeMax((int32_t)3); /* Set the maximum threshold*/ |
| 58 | + Encoder.writeMin((int32_t)0); /* Set the minimum threshold */ |
| 59 | + Encoder.writeStep((int32_t)1); /* Set the step to 1*/ |
| 60 | + Encoder.writeInterruptConfig(0xff); /* Enable all the interrupt */ |
| 61 | + Encoder.writeAntibouncingPeriod(30); /* Set an anti-bouncing of 300ms */ |
| 62 | + Encoder.writeDoublePushPeriod(30); /*Set a period for the double push of 300ms */ |
| 63 | + Encoder.writeFadeRGB(1); /*Set fade to 1ms step */ |
| 64 | + Encoder.writeRGBCode(0x00ff00); /* Turn ON the green LED */ |
| 65 | + |
| 66 | + display.begin(SSD1306_SWITCHCAPVCC, 0x3C); |
| 67 | + |
| 68 | + /* initialize the item values to the minimum value */ |
| 69 | + val[0] = min_val[0]; |
| 70 | + val[1] = min_val[1]; |
| 71 | + val[2] = min_val[2]; |
| 72 | + val[3] = min_val[3]; |
| 73 | + |
| 74 | + menu(); |
| 75 | + |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | +void loop() { |
| 80 | + |
| 81 | + if (digitalRead(IntPin) == LOW) { |
| 82 | + if ( Encoder.updateStatus()) { |
| 83 | + |
| 84 | + if ( Encoder.readStatus(RINC) || Encoder.readStatus(RDEC)) { |
| 85 | + if (insert == false) { |
| 86 | + m_pos = Encoder.readCounterByte(); /* change the item */ |
| 87 | + } |
| 88 | + if (insert == true) { |
| 89 | + val[m_pos] = Encoder.readCounterByte(); /* change the item value */ |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /* fast push, switch to the item value change mode */ |
| 94 | + if ( Encoder.readStatus(PUSHP) && Encoder.readStatus(PUSHR)) { |
| 95 | + insert = true; |
| 96 | + Encoder.writeCounter((int32_t)val[m_pos]); /* Reset the counter value */ |
| 97 | + Encoder.writeMax((int32_t)max_val[m_pos] ); /* Set the maximum threshold*/ |
| 98 | + Encoder.writeMin((int32_t)min_val[m_pos]); /* Set the minimum threshold */ |
| 99 | + Encoder.writeStep((int32_t)1); /* Set the step to 1*/ |
| 100 | + Encoder.writeRGBCode(0x0000FF); |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + /* long push, switch to the selection mode */ |
| 105 | + if ( Encoder.readStatus(PUSHR) && (Encoder.readStatus(PUSHP) == false)) { |
| 106 | + insert = false; |
| 107 | + Encoder.writeCounter((int32_t)0); /* Reset the counter value */ |
| 108 | + Encoder.writeMax((int32_t)3); /* Set the maximum threshold*/ |
| 109 | + Encoder.writeMin((int32_t)0); /* Set the minimum threshold */ |
| 110 | + Encoder.writeStep((int32_t)1); /* Set the step to 1*/ |
| 111 | + Encoder.writeRGBCode(0x00ff00); |
| 112 | + } |
| 113 | + |
| 114 | + menu(); |
| 115 | + |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | + |
| 121 | +/* fuction that print the menu' */ |
| 122 | +void menu(void) { |
| 123 | + display.clearDisplay(); |
| 124 | + display.setTextSize(1); |
| 125 | + display.setTextColor(WHITE); |
| 126 | + display.setCursor(0, 0); |
| 127 | + display.print("Menu':"); |
| 128 | + display.setCursor(10, 10); |
| 129 | + display.print("A: "); |
| 130 | + display.print(val[0]); |
| 131 | + display.setCursor(10, 20); |
| 132 | + display.print("B: "); |
| 133 | + display.print(val[1]); |
| 134 | + display.setCursor(10, 30); |
| 135 | + display.print("C: "); |
| 136 | + display.print(val[2]); |
| 137 | + display.setCursor(10, 40); |
| 138 | + display.print("D: "); |
| 139 | + display.print(val[3]); |
| 140 | + |
| 141 | + display.setCursor(0, 10 + (m_pos * 10)); |
| 142 | + display.print(">"); |
| 143 | + |
| 144 | + display.display(); |
| 145 | + |
| 146 | + |
| 147 | +} |
| 148 | + |
0 commit comments