Skip to content

Commit

Permalink
test board
Browse files Browse the repository at this point in the history
  • Loading branch information
joshua-8 committed May 8, 2024
1 parent 4fef02b commit 01b0c1f
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 40 deletions.
10 changes: 5 additions & 5 deletions examples/example/example.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@
/*
https://github.com/RCMgames/BSCD
*/
ByteSizedEncoderDecoder bsed = ByteSizedEncoderDecoder(&Wire, 14);
ByteSizedEncoderDecoder bsed = ByteSizedEncoderDecoder(&Wire1, 14);
void setup()
{
Serial.begin(115200);
Wire.begin();
Wire1.begin();
bsed.begin();
}
void loop()
{
bsed.run();
for (int i = 0; i < 8; i++) {
for (int i = 1; i <= 8; i++) {
if (bsed.isEncoderActive(i)) {
Serial.print(i);
Serial.print(": ");
Serial.print(bsed.getEncoderPosition(i));
Serial.print(bsed.getEncoderPositionWithOverflows(i));
Serial.print(", ");
}
}
Serial.println();
delay(100);
delay(10);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ volatile uint8_t lastPortCPinStates = PORTC_used_pins_mask;
const uint8_t PORTD_used_pins_mask = 0b11111100; // D7-D2
volatile uint8_t lastPortDPinStates = PORTD_used_pins_mask;

volatile int16_t encoderCount[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
volatile uint16_t encoderCount[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };

volatile uint8_t requestNumber = 255;

Expand All @@ -32,36 +32,36 @@ void onReceive(int numBytes)
void onRequest()
{
if (bitRead(requestNumber, 7)) {
Wire.write(encoderCount[0] >> 8);
Wire.write(encoderCount[0] & 0xFF);
Wire.write((uint8_t)(encoderCount[0] >> 8));
Wire.write((uint8_t)(encoderCount[0] & 0xFF));
}
if (bitRead(requestNumber, 6)) {
Wire.write(encoderCount[1] >> 8);
Wire.write(encoderCount[1] & 0xFF);
Wire.write((uint8_t)(encoderCount[1] >> 8));
Wire.write((uint8_t)(encoderCount[1] & 0xFF));
}
if (bitRead(requestNumber, 5)) {
Wire.write(encoderCount[2] >> 8);
Wire.write(encoderCount[2] & 0xFF);
Wire.write((uint8_t)(encoderCount[2] >> 8));
Wire.write((uint8_t)(encoderCount[2] & 0xFF));
}
if (bitRead(requestNumber, 4)) {
Wire.write(encoderCount[3] >> 8);
Wire.write(encoderCount[3] & 0xFF);
Wire.write((uint8_t)(encoderCount[3] >> 8));
Wire.write((uint8_t)(encoderCount[3] & 0xFF));
}
if (bitRead(requestNumber, 3)) {
Wire.write(encoderCount[4] >> 8);
Wire.write(encoderCount[4] & 0xFF);
Wire.write((uint8_t)(encoderCount[4] >> 8));
Wire.write((uint8_t)(encoderCount[4] & 0xFF));
}
if (bitRead(requestNumber, 2)) {
Wire.write(encoderCount[5] >> 8);
Wire.write(encoderCount[5] & 0xFF);
Wire.write((uint8_t)(encoderCount[5] >> 8));
Wire.write((uint8_t)(encoderCount[5] & 0xFF));
}
if (bitRead(requestNumber, 1)) {
Wire.write(encoderCount[6] >> 8);
Wire.write(encoderCount[6] & 0xFF);
Wire.write((uint8_t)(encoderCount[6] >> 8));
Wire.write((uint8_t)(encoderCount[6] & 0xFF));
}
if (bitRead(requestNumber, 0)) {
Wire.write(encoderCount[7] >> 8);
Wire.write(encoderCount[7] & 0xFF);
Wire.write((uint8_t)(encoderCount[7] >> 8));
Wire.write((uint8_t)(encoderCount[7] & 0xFF));
}
}

Expand Down Expand Up @@ -114,17 +114,17 @@ ISR(PCINT0_vect)
lastPortBPinStates = pinStates;

if (bitRead(changedPins, 0))
(bitRead(pinStates, 0) == bitRead(pinStates, 1)) ? encoderCount[3]++ : encoderCount[3]--;
(bitRead(pinStates, 0) == bitRead(pinStates, 1)) ? encoderCount[4]++ : encoderCount[4]--;
if (bitRead(changedPins, 1))
(bitRead(pinStates, 0) != bitRead(pinStates, 1)) ? encoderCount[3]++ : encoderCount[3]--;
(bitRead(pinStates, 0) != bitRead(pinStates, 1)) ? encoderCount[4]++ : encoderCount[4]--;
if (bitRead(changedPins, 2))
(bitRead(pinStates, 2) == bitRead(pinStates, 3)) ? encoderCount[4]++ : encoderCount[4]--;
(bitRead(pinStates, 2) == bitRead(pinStates, 3)) ? encoderCount[3]++ : encoderCount[3]--;
if (bitRead(changedPins, 3))
(bitRead(pinStates, 2) != bitRead(pinStates, 3)) ? encoderCount[4]++ : encoderCount[4]--;
(bitRead(pinStates, 2) != bitRead(pinStates, 3)) ? encoderCount[3]++ : encoderCount[3]--;
if (bitRead(changedPins, 4))
(bitRead(pinStates, 4) == bitRead(pinStates, 5)) ? encoderCount[5]++ : encoderCount[5]--;
(bitRead(pinStates, 4) == bitRead(pinStates, 5)) ? encoderCount[2]++ : encoderCount[2]--;
if (bitRead(changedPins, 5))
(bitRead(pinStates, 4) != bitRead(pinStates, 5)) ? encoderCount[5]++ : encoderCount[5]--;
(bitRead(pinStates, 4) != bitRead(pinStates, 5)) ? encoderCount[2]++ : encoderCount[2]--;
}

ISR(PCINT1_vect)
Expand All @@ -134,29 +134,29 @@ ISR(PCINT1_vect)
uint8_t changedPins = pinStates ^ lastPortCPinStates;
lastPortCPinStates = pinStates;
if (bitRead(changedPins, 0))
(bitRead(pinStates, 0) == bitRead(pinStates, 1)) ? encoderCount[6]++ : encoderCount[6]--;
(bitRead(pinStates, 0) == bitRead(pinStates, 1)) ? encoderCount[1]++ : encoderCount[1]--;
if (bitRead(changedPins, 1))
(bitRead(pinStates, 0) != bitRead(pinStates, 1)) ? encoderCount[6]++ : encoderCount[6]--;
(bitRead(pinStates, 0) != bitRead(pinStates, 1)) ? encoderCount[1]++ : encoderCount[1]--;
if (bitRead(changedPins, 2))
(bitRead(pinStates, 2) == bitRead(pinStates, 3)) ? encoderCount[7]++ : encoderCount[7]--;
(bitRead(pinStates, 2) == bitRead(pinStates, 3)) ? encoderCount[0]-- : encoderCount[0]++;
if (bitRead(changedPins, 3))
(bitRead(pinStates, 2) != bitRead(pinStates, 3)) ? encoderCount[7]++ : encoderCount[7]--;
(bitRead(pinStates, 2) != bitRead(pinStates, 3)) ? encoderCount[0]-- : encoderCount[0]++;
}
ISR(PCINT2_vect)
{
uint8_t pinStates = PIND & PORTD_used_pins_mask; // D7-D2
uint8_t changedPins = pinStates ^ lastPortDPinStates;
lastPortDPinStates = pinStates;
if (bitRead(changedPins, 2))
(bitRead(pinStates, 2) == bitRead(pinStates, 3)) ? encoderCount[0]++ : encoderCount[0]--;
(bitRead(pinStates, 2) == bitRead(pinStates, 3)) ? encoderCount[7]++ : encoderCount[7]--;
if (bitRead(changedPins, 3))
(bitRead(pinStates, 2) != bitRead(pinStates, 3)) ? encoderCount[0]++ : encoderCount[0]--;
(bitRead(pinStates, 2) != bitRead(pinStates, 3)) ? encoderCount[7]++ : encoderCount[7]--;
if (bitRead(changedPins, 4))
(bitRead(pinStates, 4) == bitRead(pinStates, 5)) ? encoderCount[1]++ : encoderCount[1]--;
(bitRead(pinStates, 4) == bitRead(pinStates, 5)) ? encoderCount[6]++ : encoderCount[6]--;
if (bitRead(changedPins, 5))
(bitRead(pinStates, 4) != bitRead(pinStates, 5)) ? encoderCount[1]++ : encoderCount[1]--;
(bitRead(pinStates, 4) != bitRead(pinStates, 5)) ? encoderCount[6]++ : encoderCount[6]--;
if (bitRead(changedPins, 6))
(bitRead(pinStates, 6) == bitRead(pinStates, 7)) ? encoderCount[2]++ : encoderCount[2]--;
(bitRead(pinStates, 6) == bitRead(pinStates, 7)) ? encoderCount[5]++ : encoderCount[5]--;
if (bitRead(changedPins, 7))
(bitRead(pinStates, 6) != bitRead(pinStates, 7)) ? encoderCount[2]++ : encoderCount[2]--;
(bitRead(pinStates, 6) != bitRead(pinStates, 7)) ? encoderCount[5]++ : encoderCount[5]--;
}
1 change: 1 addition & 0 deletions extras/firmware/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

[platformio]
src_dir = byte-sized-encoder-decoder-firmware
default_envs = atmega328p

[env:atmega328p]
framework = arduino
Expand Down
Binary file added extras/hardware/photos/P1039300.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added extras/hardware/photos/P1039304.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ board = uno
[env:esp32]
platform = espressif32
board = esp32dev

[env:adafruit_qtpy_esp32s3_n4r2]
framework = arduino
platform = espressif32
board = adafruit_qtpy_esp32s3_n4r2
42 changes: 40 additions & 2 deletions src/byte-sized-encoder-decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,22 @@
*/
class ByteSizedEncoderDecoder {
protected:
/**
* @brief I2C address of the Byte Sized Encoder Decoder board (as selected by the jumpers)
*/
uint8_t address;
/**
* @brief array of 8 numbers representing the number of steps each encoder has taken
*/
int16_t encoderCount[8];
/**
* @brief array of 8 numbers representing the number of times each encoder's counter variable has overflowed
*/
int16_t encoderOverflows[8];
/**
* @brief array of 8 numbers representing the last encoder count read from the board
*/
int16_t lastEncoderCount[8];
/**
* @brief I2C bus to communicate over.
*/
Expand Down Expand Up @@ -82,10 +93,30 @@ class ByteSizedEncoderDecoder {
if (high == -1 || low == -1) {
// we didn't get data
} else {
encoderCount[i] = (wire->read() << 8 | wire->read());
lastEncoderCount[i] = encoderCount[i];
encoderCount[i] = (((uint16_t)high) << 8 | ((uint16_t)low));
if (abs(encoderCount[i] - lastEncoderCount[i]) > (1 << 15)) {
encoderOverflows[i] += (encoderCount[i] > lastEncoderCount[i]) ? -1 : 1;
}
}
}
}
/**
* @brief gets the position of an encoder as a 32 bit signed integer (it counts how many times the 16 bit number has overflowed)
* @param n: encoder number (1-8), other values will return 0
* @param read: whether to read the encoder positions from the board before returning the value, default is false
* @retval int32_t: the number of steps the encoder has taken
*/
int32_t getEncoderPositionWithOverflows(uint8_t n, boolean read = false)
{
if (read) {
run();
}
if (n > 8 || n < 1) {
return 0;
}
return (int32_t)encoderCount[n - 1] + (int32_t)encoderOverflows[n - 1] * 65536;
}
/**
* @brief gets the position of an encoder
* @param n: encoder number (1-8), other values will return 0
Expand All @@ -104,10 +135,17 @@ class ByteSizedEncoderDecoder {
}
/**
* @brief resets all encoder positions to 0
* @param resetVariables: whether to reset the encoderCount and encoderOverflows variables, default is true
*/
void resetEncoderPositions()
void resetEncoderPositions(boolean resetVariables = true)
{
write(0);
if (resetVariables) {
for (byte i = 0; i < 8; i++) {
encoderCount[i] = 0;
encoderOverflows[i] = 0;
}
}
}
/**
* @brief set which encoders you want to receive data from
Expand Down

0 comments on commit 01b0c1f

Please sign in to comment.