Last active
September 1, 2022 03:29
-
-
Save Tech500/2de3a22cd9fe50f5f8a6d39a0dcedd44 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Adafruit Seesaw library: "multiencoder.ino" example; expanded to use Sparkfun Alphanumeric I²C display. | |
Modified by William Lucid | |
*/ | |
#include <Wire.h> | |
#include "Adafruit_seesaw.h" | |
#include <seesaw_neopixel.h> | |
#include <SparkFun_Alphanumeric_Display.h> | |
HT16K33 display; | |
#define SS_SWITCH 24 // this is the pin on the encoder connected to switch | |
#define SS_NEOPIX 6 // this is the pin on the encoder connected to neopixel | |
#define SEESAW_BASE_ADDR 0x36 // I2C address, starts with 0x36 | |
int result; //ASCII Character code in decimal | |
// create 4 encoders! | |
Adafruit_seesaw encoders[4]; | |
// create 4 encoder pixels | |
seesaw_NeoPixel encoder_pixels[4] = { | |
seesaw_NeoPixel(1, SS_NEOPIX, NEO_GRB + NEO_KHZ800), | |
seesaw_NeoPixel(1, SS_NEOPIX, NEO_GRB + NEO_KHZ800), | |
seesaw_NeoPixel(1, SS_NEOPIX, NEO_GRB + NEO_KHZ800), | |
seesaw_NeoPixel(1, SS_NEOPIX, NEO_GRB + NEO_KHZ800)}; | |
int32_t encoder_positions[] = {0, 0, 0, 0}; | |
bool found_encoders[] = {false, false, false, false}; | |
void setup() { | |
Serial.begin(115200); | |
// wait for serial port to open | |
while (!Serial) delay(10); | |
Serial.println("Alphanumeric display + seesaw Encoders test"); | |
Wire.begin(); | |
//check if display will acknowledge | |
if (display.begin() == false) | |
{ | |
Serial.println("Device did not acknowledge! Freezing."); | |
while(1); | |
} | |
Serial.println("Display acknowledged."); | |
Serial.println("Looking for seesaws!"); | |
for (uint8_t enc=0; enc<sizeof(found_encoders); enc++) { | |
// See if we can find encoders on this address | |
if (! encoders[enc].begin(SEESAW_BASE_ADDR + enc) || | |
! encoder_pixels[enc].begin(SEESAW_BASE_ADDR + enc)) { | |
Serial.print("Couldn't find encoder # "); | |
Serial.println(enc); | |
} else { | |
Serial.print("Found encoder + pixel #"); | |
Serial.println(enc); | |
uint32_t version = ((encoders[enc].getVersion() >> 16) & 0xFFFF); | |
if (version != 4991){ | |
Serial.print("Wrong firmware loaded? "); | |
Serial.println(version); | |
while(1) delay(10); | |
} | |
Serial.println("Found Product 4991"); | |
// use a pin for the built in encoder switch | |
encoders[enc].pinMode(SS_SWITCH, INPUT_PULLUP); | |
// get starting position | |
encoder_positions[enc] = encoders[enc].getEncoderPosition(); | |
Serial.println("Turning on interrupts"); | |
delay(10); | |
encoders[enc].setGPIOInterrupts((uint32_t)1 << SS_SWITCH, 1); | |
encoders[enc].enableEncoderInterrupt(); | |
// set not so bright! | |
encoder_pixels[enc].setBrightness(30); | |
encoder_pixels[enc].show(); | |
found_encoders[enc] = true; | |
} | |
} | |
Serial.println("Encoders started"); | |
} | |
void loop() { | |
for (uint8_t enc=0; enc<sizeof(found_encoders); enc++) { | |
if (found_encoders[enc] == false) continue; | |
int32_t new_position = encoders[enc].getEncoderPosition(); | |
// did we move around? | |
if (encoder_positions[enc] != new_position) | |
Serial.print("Encoder #"); | |
Serial.print(enc); | |
Serial.print(" -> "); | |
Serial.println(new_position); // display new position | |
encoder_positions[enc] = new_position; | |
// Code from paulfjujo, on rntlabs.com forum | |
if (encoder_positions[enc]< 27) | |
{ | |
result = (65 + encoder_positions[enc]); | |
} else { | |
result = (48 + (encoder_positions[enc]-27)); | |
} | |
Serial.print("encoder_positions[enc]: "); | |
Serial.println(encoder_positions[enc]); | |
Serial.write(result); //ASCII Char code in decimal | |
Serial.println(""); | |
Serial.println(result); | |
encoder_positions[enc] = new_position; | |
char letter = char(result); | |
display.printChar(letter, 0); | |
display.printChar('O', 1); | |
display.printChar('O', 2); | |
display.printChar('O', 3); | |
display.updateDisplay(); | |
// change the neopixel color, mulitply the new positiion by 4 to speed it up | |
encoder_pixels[enc].setPixelColor(0, Wheel((new_position*4) & 0xFF)); | |
encoder_pixels[enc].show(); | |
if (! encoders[enc].digitalRead(SS_SWITCH)) { | |
Serial.print("Encoder #"); | |
Serial.print(enc); | |
Serial.println(" pressed"); | |
} | |
} | |
} | |
uint32_t Wheel(byte WheelPos) { | |
WheelPos = 255 - WheelPos; | |
if (WheelPos < 85) { | |
return seesaw_NeoPixel::Color(255 - WheelPos * 3, 0, WheelPos * 3); | |
} | |
if (WheelPos < 170) { | |
WheelPos -= 85; | |
return seesaw_NeoPixel::Color(0, WheelPos * 3, 255 - WheelPos * 3); | |
} | |
WheelPos -= 170; | |
return seesaw_NeoPixel::Color(WheelPos * 3, 255 - WheelPos * 3, 0); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment