Skip to content

Commit c3e8a58

Browse files
author
Saimon
committed
I2C encoder mini update
1 parent 3b565de commit c3e8a58

File tree

7 files changed

+672
-11
lines changed

7 files changed

+672
-11
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Currently supported boards:
1010

1111
- [I2C Encoder V2](https://github.com/Fattoresaimon/I2CEncoderV2) Library description [here](https://github.com/Fattoresaimon/ArduinoDuPPaLib/blob/master/examples/I2CEncoderV2/README.md)
1212
- [I2C NavKey](https://github.com/Fattoresaimon/I2CNavKey) Library description [here](https://github.com/Fattoresaimon/ArduinoDuPPaLib/blob/master/examples/I2CNavKey/README.md)
13+
- [I2C Encoder Mini](https://github.com/Fattoresaimon/I2CEncoderMini) Library description [here](https://github.com/Fattoresaimon/ArduinoDuPPaLib/blob/master/examples/I2CEncoderMini/README.md)
1314

1415

1516

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#include <Wire.h>
2+
#include <i2cEncoderMiniLib.h>
3+
4+
/*This is a basic example for using the I2C Encoder mini
5+
The counter is set to work between +10 to -10, at every encoder click the counter value is printed on the terminal.
6+
It's also printet when the push button is pressed, or released or double pushed.
7+
The callback are used instead of the switch case
8+
9+
Connections with Arduino UNO:
10+
- -> GND
11+
+ -> 5V
12+
SDA -> A4
13+
SCL -> A5
14+
INT -> A3
15+
*/
16+
17+
const int IntPin = A3; /* Definition of the interrupt pin. You can change according to your board */
18+
//Class initialization with the I2C addresses
19+
i2cEncoderMiniLib Encoder(0x20); /* A0 is soldered */
20+
21+
//Callback when the CVAL is incremented
22+
void encoder_increment(i2cEncoderMiniLib* obj) {
23+
Serial.print("Increment: ");
24+
Serial.println(Encoder.readCounterByte());
25+
}
26+
27+
//Callback when the CVAL is decremented
28+
void encoder_decrement(i2cEncoderMiniLib* obj) {
29+
Serial.print("Decrement: ");
30+
Serial.println(Encoder.readCounterByte());
31+
}
32+
33+
//Callback when CVAL reach MAX
34+
void encoder_max(i2cEncoderMiniLib* obj) {
35+
Serial.print("Maximum threshold: ");
36+
Serial.println(Encoder.readCounterByte());
37+
}
38+
39+
//Callback when CVAL reach MIN
40+
void encoder_min(i2cEncoderMiniLib* obj) {
41+
Serial.print("Minimum threshold: ");
42+
Serial.println(Encoder.readCounterByte());
43+
}
44+
45+
//Callback when the encoder is pushed
46+
void encoder_push(i2cEncoderMiniLib* obj) {
47+
Serial.println("Encoder is pushed!");
48+
}
49+
50+
//Callback when the encoder is released
51+
void encoder_released(i2cEncoderMiniLib* obj) {
52+
Serial.println("Encoder is released");
53+
}
54+
55+
//Callback when the encoder is double pushed
56+
void encoder_double_push(i2cEncoderMiniLib* obj) {
57+
Serial.println("Encoder is double pushed!");
58+
}
59+
60+
//Callback when the encoder is long pushed
61+
void encoder_long_push(i2cEncoderMiniLib* obj) {
62+
Serial.println("Encoder is long pushed!");
63+
}
64+
65+
void setup(void) {
66+
pinMode(IntPin, INPUT);
67+
Wire.begin();
68+
Serial.begin(115200);
69+
Serial.println("**** I2C Encoder Mini basic example ****");
70+
Encoder.reset();
71+
Encoder.begin(i2cEncoderMiniLib::WRAP_DISABLE
72+
| i2cEncoderMiniLib::DIRE_LEFT | i2cEncoderMiniLib::IPUP_ENABLE
73+
| i2cEncoderMiniLib::RMOD_X1 );
74+
75+
Encoder.writeCounter((int32_t) 0); /* Reset the counter value */
76+
Encoder.writeMax((int32_t) 10); /* Set the maximum threshold*/
77+
Encoder.writeMin((int32_t) - 10); /* Set the minimum threshold */
78+
Encoder.writeStep((int32_t) 1); /* Set the step to 1*/
79+
Encoder.writeDoublePushPeriod(50); /*Set a period for the double push of 500ms */
80+
81+
// Definition of the events
82+
Encoder.onIncrement = encoder_increment;
83+
Encoder.onDecrement = encoder_decrement;
84+
Encoder.onMax = encoder_max;
85+
Encoder.onMin = encoder_min;
86+
Encoder.onButtonPush = encoder_push;
87+
Encoder.onButtonRelease = encoder_released;
88+
Encoder.onButtonDoublePush = encoder_double_push;
89+
Encoder.onButtonLongPush = encoder_long_push;
90+
91+
/* Enable the I2C Encoder V2 interrupts according to the previus attached callback */
92+
Encoder.autoconfigInterrupt();
93+
94+
}
95+
96+
void loop() {
97+
if (digitalRead(IntPin) == LOW) {
98+
/* Check the status of the encoder and call the callback */
99+
Encoder.updateStatus();
100+
}
101+
}
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
#include <Wire.h>
2+
#include <i2cEncoderMiniLib.h>
3+
4+
/* This example is used for change the address of the I2C Encoder Mini
5+
Connections with Arduino UNO:
6+
- -> GND
7+
+ -> 5V
8+
SDA -> A4
9+
SCL -> A5
10+
11+
The COM boudrate is 115200.
12+
You can use the command 'S' for find the I2C encoder mini on the I2C bus.
13+
Or you can write directly the address in decimal or hexadecimal.
14+
After after you need to write the new address in the same way.
15+
Example:
16+
17+
**** I2C Encoder Address changer! ****
18+
All the devices must have different address!
19+
Avaiable commands:
20+
S: for searching the I2C encoder mini
21+
0xXX or XXX: Address of the target device in hex or decimal
22+
23+
S
24+
25+
Scanning....
26+
I2C Encoder mini found at 33 ( 0x21 ) !!
27+
28+
29+
All the devices must have different address!
30+
Avaiable commands:
31+
S: for searching the I2C encoder mini
32+
0xXX or XXX: Address of the target device in hex or decimal
33+
34+
0x21
35+
36+
Insert the desired address in hex or decimal:
37+
38+
24
39+
40+
Changing address from 33 ( 0x21 ) to 24 ( 0x18 )
41+
Address Changed!
42+
43+
*/
44+
45+
void ChangeAddress(uint8_t add);
46+
int8_t NewAdd(void);
47+
int8_t CommadParser(void);
48+
int8_t NumberParser(char buff[]);
49+
bool CommandRead(void);
50+
void Search(void);
51+
void PrintHEX(uint8_t i);
52+
53+
i2cEncoderMiniLib Encoder(0x20);
54+
55+
56+
char c_buff[5] = {0};
57+
uint8_t c_buff_cnt = 0;
58+
59+
int8_t t_add;
60+
61+
62+
void setup(void) {
63+
Wire.begin();
64+
Serial.begin(115200);
65+
Serial.println("\n\n**** I2C Encoder Address changer! ****");
66+
}
67+
68+
void loop() {
69+
Serial.println("All the devices must have different address!");
70+
Serial.println("Avaiable commands:");
71+
Serial.println(" S: for searching the I2C encoder mini");
72+
Serial.println(" 0xXX or XXX: Address of the target device in hex or decimal\n");
73+
74+
t_add = CommadParser();
75+
if (t_add <= 0) {
76+
switch (t_add) {
77+
case -1:
78+
Serial.println("Wrong address range!");
79+
break;
80+
81+
case -2:
82+
Serial.println("Wrong command!");
83+
break;
84+
85+
case -3:
86+
Search();
87+
break;
88+
}
89+
} else {
90+
Encoder.address = t_add;
91+
if (Encoder.readIDCode() == 0x39) {
92+
Serial.println("Insert the desired address in hex or decimal: ");
93+
t_add = NewAdd();
94+
if (t_add <= 0) {
95+
Serial.println("Incorrect Address!");
96+
} else {
97+
ChangeAddress(t_add);
98+
}
99+
} else {
100+
Serial.println("No device at that address!");
101+
}
102+
}
103+
Serial.print("\n\n");
104+
}
105+
106+
107+
void ChangeAddress(uint8_t add) {
108+
Serial.print("Changing address from ");
109+
Serial.print(Encoder.address);
110+
Serial.print(" ( ");
111+
PrintHEX(Encoder.address);
112+
Serial.print(" ) to ");
113+
Serial.print(add);
114+
Serial.print(" ( ");
115+
PrintHEX(add);
116+
Serial.println(" )");
117+
Encoder.ChangeI2CAddress(add);
118+
// delay(1000);
119+
Encoder.reset();
120+
Encoder.address = add;
121+
if (Encoder.readIDCode() == 0x39) {
122+
Serial.println("Address Changed!");
123+
} else {
124+
Serial.println("Error in changing address!");
125+
}
126+
}
127+
128+
129+
int8_t NewAdd(void) {
130+
131+
if (CommandRead() == true) {
132+
return (NumberParser(c_buff));
133+
} else {
134+
return -1;
135+
}
136+
}
137+
138+
139+
int8_t CommadParser(void) {
140+
141+
if (CommandRead() == true) {
142+
if ( c_buff[0] == 'S' && c_buff[1] == '\n') {
143+
return -3;
144+
}
145+
return (NumberParser(c_buff));
146+
} else {
147+
return -2;
148+
}
149+
}
150+
151+
152+
int8_t NumberParser(char buff[]) {
153+
int8_t temp;
154+
if ( buff[0] == '0' && buff[1] == 'x') {
155+
temp = (int8_t)strtol(buff, NULL, 16);
156+
} else {
157+
temp = (int8_t)strtol(buff, NULL, 10);
158+
}
159+
160+
if (temp <= 0) {
161+
return -1;
162+
} else {
163+
return temp;
164+
}
165+
166+
}
167+
168+
169+
bool CommandRead(void) {
170+
c_buff_cnt = 0;
171+
while(Serial. read() >= 0);
172+
while (c_buff_cnt < 5) {
173+
if (Serial.available()) {
174+
char inChar = (char)Serial.read();
175+
c_buff[c_buff_cnt] = inChar;
176+
c_buff_cnt++;
177+
if (inChar == '\n')
178+
return true;
179+
}
180+
}
181+
return false;
182+
}
183+
184+
185+
void Search(void) {
186+
uint8_t i, error;
187+
Serial.println("Scanning....");
188+
for (i = 0; i < 0x80; i++ )
189+
{
190+
Wire.beginTransmission(i);
191+
error = Wire.endTransmission();
192+
193+
if (error == 0)
194+
{
195+
Encoder.address = i;
196+
if (Encoder.readIDCode() == 0x39) {
197+
Serial.print("I2C Encoder mini found at ");
198+
Serial.print(Encoder.address);
199+
Serial.print(" ( ");
200+
PrintHEX(Encoder.address);
201+
Serial.print(" )");
202+
Serial.println(" !!");
203+
}
204+
}
205+
206+
}
207+
}
208+
209+
210+
void PrintHEX(uint8_t i) {
211+
Serial.print("0x");
212+
if (i < 16)
213+
Serial.print("0");
214+
Serial.print(i, HEX);
215+
}

0 commit comments

Comments
 (0)