Skip to content

Commit

Permalink
I2C encoder mini update
Browse files Browse the repository at this point in the history
  • Loading branch information
Saimon committed Mar 16, 2020
1 parent 3b565de commit c3e8a58
Show file tree
Hide file tree
Showing 7 changed files with 672 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Currently supported boards:

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



Expand Down
101 changes: 101 additions & 0 deletions examples/I2CEncoderMini/Basic_with_Callbacks/Basic_with_Callbacks.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include <Wire.h>
#include <i2cEncoderMiniLib.h>

/*This is a basic example for using the I2C Encoder mini
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
i2cEncoderMiniLib Encoder(0x20); /* A0 is soldered */

//Callback when the CVAL is incremented
void encoder_increment(i2cEncoderMiniLib* obj) {
Serial.print("Increment: ");
Serial.println(Encoder.readCounterByte());
}

//Callback when the CVAL is decremented
void encoder_decrement(i2cEncoderMiniLib* obj) {
Serial.print("Decrement: ");
Serial.println(Encoder.readCounterByte());
}

//Callback when CVAL reach MAX
void encoder_max(i2cEncoderMiniLib* obj) {
Serial.print("Maximum threshold: ");
Serial.println(Encoder.readCounterByte());
}

//Callback when CVAL reach MIN
void encoder_min(i2cEncoderMiniLib* obj) {
Serial.print("Minimum threshold: ");
Serial.println(Encoder.readCounterByte());
}

//Callback when the encoder is pushed
void encoder_push(i2cEncoderMiniLib* obj) {
Serial.println("Encoder is pushed!");
}

//Callback when the encoder is released
void encoder_released(i2cEncoderMiniLib* obj) {
Serial.println("Encoder is released");
}

//Callback when the encoder is double pushed
void encoder_double_push(i2cEncoderMiniLib* obj) {
Serial.println("Encoder is double pushed!");
}

//Callback when the encoder is long pushed
void encoder_long_push(i2cEncoderMiniLib* obj) {
Serial.println("Encoder is long pushed!");
}

void setup(void) {
pinMode(IntPin, INPUT);
Wire.begin();
Serial.begin(115200);
Serial.println("**** I2C Encoder Mini basic example ****");
Encoder.reset();
Encoder.begin(i2cEncoderMiniLib::WRAP_DISABLE
| i2cEncoderMiniLib::DIRE_LEFT | i2cEncoderMiniLib::IPUP_ENABLE
| i2cEncoderMiniLib::RMOD_X1 );

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.writeDoublePushPeriod(50); /*Set a period for the double push of 500ms */

// Definition of the events
Encoder.onIncrement = encoder_increment;
Encoder.onDecrement = encoder_decrement;
Encoder.onMax = encoder_max;
Encoder.onMin = encoder_min;
Encoder.onButtonPush = encoder_push;
Encoder.onButtonRelease = encoder_released;
Encoder.onButtonDoublePush = encoder_double_push;
Encoder.onButtonLongPush = encoder_long_push;

/* Enable the I2C Encoder V2 interrupts according to the previus attached callback */
Encoder.autoconfigInterrupt();

}

void loop() {
if (digitalRead(IntPin) == LOW) {
/* Check the status of the encoder and call the callback */
Encoder.updateStatus();
}
}
215 changes: 215 additions & 0 deletions examples/I2CEncoderMini/Change_Address/Change_Address.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
#include <Wire.h>
#include <i2cEncoderMiniLib.h>

/* This example is used for change the address of the I2C Encoder Mini
Connections with Arduino UNO:
- -> GND
+ -> 5V
SDA -> A4
SCL -> A5
The COM boudrate is 115200.
You can use the command 'S' for find the I2C encoder mini on the I2C bus.
Or you can write directly the address in decimal or hexadecimal.
After after you need to write the new address in the same way.
Example:
**** I2C Encoder Address changer! ****
All the devices must have different address!
Avaiable commands:
S: for searching the I2C encoder mini
0xXX or XXX: Address of the target device in hex or decimal
S
Scanning....
I2C Encoder mini found at 33 ( 0x21 ) !!
All the devices must have different address!
Avaiable commands:
S: for searching the I2C encoder mini
0xXX or XXX: Address of the target device in hex or decimal
0x21
Insert the desired address in hex or decimal:
24
Changing address from 33 ( 0x21 ) to 24 ( 0x18 )
Address Changed!
*/

void ChangeAddress(uint8_t add);
int8_t NewAdd(void);
int8_t CommadParser(void);
int8_t NumberParser(char buff[]);
bool CommandRead(void);
void Search(void);
void PrintHEX(uint8_t i);

i2cEncoderMiniLib Encoder(0x20);


char c_buff[5] = {0};
uint8_t c_buff_cnt = 0;

int8_t t_add;


void setup(void) {
Wire.begin();
Serial.begin(115200);
Serial.println("\n\n**** I2C Encoder Address changer! ****");
}

void loop() {
Serial.println("All the devices must have different address!");
Serial.println("Avaiable commands:");
Serial.println(" S: for searching the I2C encoder mini");
Serial.println(" 0xXX or XXX: Address of the target device in hex or decimal\n");

t_add = CommadParser();
if (t_add <= 0) {
switch (t_add) {
case -1:
Serial.println("Wrong address range!");
break;

case -2:
Serial.println("Wrong command!");
break;

case -3:
Search();
break;
}
} else {
Encoder.address = t_add;
if (Encoder.readIDCode() == 0x39) {
Serial.println("Insert the desired address in hex or decimal: ");
t_add = NewAdd();
if (t_add <= 0) {
Serial.println("Incorrect Address!");
} else {
ChangeAddress(t_add);
}
} else {
Serial.println("No device at that address!");
}
}
Serial.print("\n\n");
}


void ChangeAddress(uint8_t add) {
Serial.print("Changing address from ");
Serial.print(Encoder.address);
Serial.print(" ( ");
PrintHEX(Encoder.address);
Serial.print(" ) to ");
Serial.print(add);
Serial.print(" ( ");
PrintHEX(add);
Serial.println(" )");
Encoder.ChangeI2CAddress(add);
// delay(1000);
Encoder.reset();
Encoder.address = add;
if (Encoder.readIDCode() == 0x39) {
Serial.println("Address Changed!");
} else {
Serial.println("Error in changing address!");
}
}


int8_t NewAdd(void) {

if (CommandRead() == true) {
return (NumberParser(c_buff));
} else {
return -1;
}
}


int8_t CommadParser(void) {

if (CommandRead() == true) {
if ( c_buff[0] == 'S' && c_buff[1] == '\n') {
return -3;
}
return (NumberParser(c_buff));
} else {
return -2;
}
}


int8_t NumberParser(char buff[]) {
int8_t temp;
if ( buff[0] == '0' && buff[1] == 'x') {
temp = (int8_t)strtol(buff, NULL, 16);
} else {
temp = (int8_t)strtol(buff, NULL, 10);
}

if (temp <= 0) {
return -1;
} else {
return temp;
}

}


bool CommandRead(void) {
c_buff_cnt = 0;
while(Serial. read() >= 0);
while (c_buff_cnt < 5) {
if (Serial.available()) {
char inChar = (char)Serial.read();
c_buff[c_buff_cnt] = inChar;
c_buff_cnt++;
if (inChar == '\n')
return true;
}
}
return false;
}


void Search(void) {
uint8_t i, error;
Serial.println("Scanning....");
for (i = 0; i < 0x80; i++ )
{
Wire.beginTransmission(i);
error = Wire.endTransmission();

if (error == 0)
{
Encoder.address = i;
if (Encoder.readIDCode() == 0x39) {
Serial.print("I2C Encoder mini found at ");
Serial.print(Encoder.address);
Serial.print(" ( ");
PrintHEX(Encoder.address);
Serial.print(" )");
Serial.println(" !!");
}
}

}
}


void PrintHEX(uint8_t i) {
Serial.print("0x");
if (i < 16)
Serial.print("0");
Serial.print(i, HEX);
}
Loading

0 comments on commit c3e8a58

Please sign in to comment.