First init.
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* This is a demo for a QT Py RP2040 connected to an ANO encoder breakout and a 7-segment breakout
|
||||
* using the onboard Stemma QT Port
|
||||
* https://www.adafruit.com/product/878
|
||||
* https://www.adafruit.com/product/5740
|
||||
*
|
||||
*/
|
||||
#include "Adafruit_seesaw.h"
|
||||
#include "Adafruit_LEDBackpack.h"
|
||||
|
||||
|
||||
#define SS_SWITCH_SELECT 1
|
||||
#define SS_SWITCH_UP 2
|
||||
#define SS_SWITCH_LEFT 3
|
||||
#define SS_SWITCH_DOWN 4
|
||||
#define SS_SWITCH_RIGHT 5
|
||||
|
||||
#define SEESAW_ADDR 0x49
|
||||
|
||||
Adafruit_7segment seven = Adafruit_7segment();
|
||||
Adafruit_seesaw ss = Adafruit_seesaw(&Wire1);
|
||||
int32_t encoder_position;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
//while (!Serial) delay(10);
|
||||
|
||||
seven.begin(0x70, &Wire1);
|
||||
|
||||
Serial.println("Looking for seesaw!");
|
||||
|
||||
if (! ss.begin(SEESAW_ADDR)) {
|
||||
Serial.println("Couldn't find seesaw on default address");
|
||||
while(1) delay(10);
|
||||
}
|
||||
Serial.println("seesaw started");
|
||||
uint32_t version = ((ss.getVersion() >> 16) & 0xFFFF);
|
||||
if (version != 5740){
|
||||
Serial.print("Wrong firmware loaded? ");
|
||||
Serial.println(version);
|
||||
while(1) delay(10);
|
||||
}
|
||||
Serial.println("Found Product 5740");
|
||||
|
||||
ss.pinMode(SS_SWITCH_UP, INPUT_PULLUP);
|
||||
ss.pinMode(SS_SWITCH_DOWN, INPUT_PULLUP);
|
||||
ss.pinMode(SS_SWITCH_LEFT, INPUT_PULLUP);
|
||||
ss.pinMode(SS_SWITCH_RIGHT, INPUT_PULLUP);
|
||||
ss.pinMode(SS_SWITCH_SELECT, INPUT_PULLUP);
|
||||
|
||||
// get starting position
|
||||
encoder_position = ss.getEncoderPosition();
|
||||
seven.print(encoder_position, DEC);
|
||||
seven.writeDisplay();
|
||||
|
||||
Serial.println("Turning on interrupts");
|
||||
ss.enableEncoderInterrupt();
|
||||
ss.setGPIOInterrupts((uint32_t)1 << SS_SWITCH_UP, 1);
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (! ss.digitalRead(SS_SWITCH_UP)) {
|
||||
seven.println(" UP ");
|
||||
seven.writeDisplay();
|
||||
Serial.println("UP pressed!");
|
||||
}
|
||||
else if (! ss.digitalRead(SS_SWITCH_DOWN)) {
|
||||
seven.println("DOWN");
|
||||
seven.writeDisplay();
|
||||
Serial.println("DOWN pressed!");
|
||||
}
|
||||
else if (! ss.digitalRead(SS_SWITCH_SELECT)) {
|
||||
seven.println("SELE");
|
||||
seven.writeDisplay();
|
||||
Serial.println("SELECT pressed!");
|
||||
}
|
||||
else if (! ss.digitalRead(SS_SWITCH_LEFT)) {
|
||||
seven.println("LEFT");
|
||||
seven.writeDisplay();
|
||||
Serial.println("LEFT pressed!");
|
||||
}
|
||||
else if (! ss.digitalRead(SS_SWITCH_RIGHT)) {
|
||||
seven.println("RIGT");
|
||||
seven.writeDisplay();
|
||||
Serial.println("RIGHT pressed!");
|
||||
} else {
|
||||
seven.print(encoder_position, DEC);
|
||||
seven.writeDisplay();
|
||||
}
|
||||
|
||||
int32_t new_position = ss.getEncoderPosition();
|
||||
// did we move around?
|
||||
if (encoder_position != new_position) {
|
||||
Serial.println(new_position); // display new position
|
||||
encoder_position = new_position; // and save for next round
|
||||
}
|
||||
|
||||
// don't overwhelm serial port
|
||||
delay(10);
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* This example shows how to read from a seesaw encoder module.
|
||||
* The available encoder API is:
|
||||
* int32_t getEncoderPosition();
|
||||
int32_t getEncoderDelta();
|
||||
void enableEncoderInterrupt();
|
||||
void disableEncoderInterrupt();
|
||||
*/
|
||||
#include "Adafruit_seesaw.h"
|
||||
|
||||
#define SS_SWITCH_SELECT 1
|
||||
#define SS_SWITCH_UP 2
|
||||
#define SS_SWITCH_LEFT 3
|
||||
#define SS_SWITCH_DOWN 4
|
||||
#define SS_SWITCH_RIGHT 5
|
||||
|
||||
#define SEESAW_ADDR 0x49
|
||||
|
||||
Adafruit_seesaw ss;
|
||||
int32_t encoder_position;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
while (!Serial) delay(10);
|
||||
|
||||
Serial.println("Looking for seesaw!");
|
||||
|
||||
if (! ss.begin(SEESAW_ADDR)) {
|
||||
Serial.println("Couldn't find seesaw on default address");
|
||||
while(1) delay(10);
|
||||
}
|
||||
Serial.println("seesaw started");
|
||||
uint32_t version = ((ss.getVersion() >> 16) & 0xFFFF);
|
||||
if (version != 5740){
|
||||
Serial.print("Wrong firmware loaded? ");
|
||||
Serial.println(version);
|
||||
while(1) delay(10);
|
||||
}
|
||||
Serial.println("Found Product 5740");
|
||||
|
||||
ss.pinMode(SS_SWITCH_UP, INPUT_PULLUP);
|
||||
ss.pinMode(SS_SWITCH_DOWN, INPUT_PULLUP);
|
||||
ss.pinMode(SS_SWITCH_LEFT, INPUT_PULLUP);
|
||||
ss.pinMode(SS_SWITCH_RIGHT, INPUT_PULLUP);
|
||||
ss.pinMode(SS_SWITCH_SELECT, INPUT_PULLUP);
|
||||
|
||||
// get starting position
|
||||
encoder_position = ss.getEncoderPosition();
|
||||
|
||||
Serial.println("Turning on interrupts");
|
||||
ss.enableEncoderInterrupt();
|
||||
ss.setGPIOInterrupts((uint32_t)1 << SS_SWITCH_UP, 1);
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (! ss.digitalRead(SS_SWITCH_UP)) {
|
||||
Serial.println("UP pressed!");
|
||||
}
|
||||
if (! ss.digitalRead(SS_SWITCH_DOWN)) {
|
||||
Serial.println("DOWN pressed!");
|
||||
}
|
||||
if (! ss.digitalRead(SS_SWITCH_SELECT)) {
|
||||
Serial.println("SELECT pressed!");
|
||||
}
|
||||
if (! ss.digitalRead(SS_SWITCH_LEFT)) {
|
||||
Serial.println("LEFT pressed!");
|
||||
}
|
||||
if (! ss.digitalRead(SS_SWITCH_RIGHT)) {
|
||||
Serial.println("RIGHT pressed!");
|
||||
}
|
||||
|
||||
int32_t new_position = ss.getEncoderPosition();
|
||||
// did we move around?
|
||||
if (encoder_position != new_position) {
|
||||
Serial.println(new_position); // display new position
|
||||
|
||||
encoder_position = new_position; // and save for next round
|
||||
}
|
||||
|
||||
// don't overwhelm serial port
|
||||
delay(10);
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
/*
|
||||
* This is a demo for a Feather ESP32-S2 TFT connected to a Quad
|
||||
* Rotary Encoder board
|
||||
* https://www.adafruit.com/product/5300
|
||||
* https://www.adafruit.com/product/5752
|
||||
*
|
||||
*/
|
||||
#include "Adafruit_seesaw.h"
|
||||
#include <seesaw_neopixel.h>
|
||||
#include <Adafruit_ST7789.h>
|
||||
#include <Fonts/FreeSans12pt7b.h>
|
||||
// we hvae loooots of PSRAM so lets use a GFX canvas for flicker-free graphics!
|
||||
GFXcanvas16 canvas(240, 135);
|
||||
Adafruit_ST7789 display = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
|
||||
|
||||
|
||||
#define SS_NEO_PIN 18
|
||||
#define SS_ENC0_SWITCH 12
|
||||
#define SS_ENC1_SWITCH 14
|
||||
#define SS_ENC2_SWITCH 17
|
||||
#define SS_ENC3_SWITCH 9
|
||||
|
||||
#define SEESAW_ADDR 0x49
|
||||
|
||||
Adafruit_seesaw ss = Adafruit_seesaw(&Wire);
|
||||
seesaw_NeoPixel pixels = seesaw_NeoPixel(4, SS_NEO_PIN, NEO_GRB + NEO_KHZ800);
|
||||
|
||||
int32_t enc_positions[4] = {0, 0, 0, 0};
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
//while (!Serial) delay(10);
|
||||
|
||||
pinMode(TFT_BACKLITE, OUTPUT);
|
||||
digitalWrite(TFT_BACKLITE, LOW);
|
||||
|
||||
// turn on the TFT / I2C power supply
|
||||
pinMode(TFT_I2C_POWER, OUTPUT);
|
||||
digitalWrite(TFT_I2C_POWER, HIGH);
|
||||
delay(10);
|
||||
|
||||
Serial.println("Looking for seesaw!");
|
||||
|
||||
if (! ss.begin(SEESAW_ADDR) || !pixels.begin(SEESAW_ADDR)) {
|
||||
Serial.println("Couldn't find seesaw on default address");
|
||||
while(1) delay(10);
|
||||
}
|
||||
Serial.println("seesaw started");
|
||||
uint32_t version = ((ss.getVersion() >> 16) & 0xFFFF);
|
||||
if (version != 5752){
|
||||
Serial.print("Wrong firmware loaded? ");
|
||||
Serial.println(version);
|
||||
while(1) delay(10);
|
||||
}
|
||||
Serial.println("Found Product 5752");
|
||||
|
||||
ss.pinMode(SS_ENC0_SWITCH, INPUT_PULLUP);
|
||||
ss.pinMode(SS_ENC1_SWITCH, INPUT_PULLUP);
|
||||
ss.pinMode(SS_ENC2_SWITCH, INPUT_PULLUP);
|
||||
ss.pinMode(SS_ENC3_SWITCH, INPUT_PULLUP);
|
||||
ss.setGPIOInterrupts(1UL << SS_ENC0_SWITCH | 1UL << SS_ENC1_SWITCH |
|
||||
1UL << SS_ENC2_SWITCH | 1UL << SS_ENC3_SWITCH, 1);
|
||||
|
||||
|
||||
// get starting positions
|
||||
for (int e=0; e<4; e++) {
|
||||
enc_positions[e] = ss.getEncoderPosition(e);
|
||||
ss.enableEncoderInterrupt(e);
|
||||
}
|
||||
|
||||
Serial.println("Turning on interrupts");
|
||||
|
||||
pixels.setBrightness(255);
|
||||
pixels.show(); // Initialize all pixels to 'off'
|
||||
|
||||
display.init(135, 240); // Init ST7789 240x135
|
||||
display.setRotation(3);
|
||||
display.fillScreen(ST77XX_BLUE);
|
||||
digitalWrite(TFT_BACKLITE, HIGH);
|
||||
|
||||
canvas.setFont(&FreeSans12pt7b);
|
||||
canvas.setTextColor(ST77XX_WHITE);
|
||||
}
|
||||
|
||||
uint8_t switch_pins[] = {SS_ENC0_SWITCH, SS_ENC1_SWITCH, SS_ENC2_SWITCH, SS_ENC3_SWITCH};
|
||||
|
||||
void loop() {
|
||||
bool changed = false;
|
||||
canvas.fillScreen(ST77XX_BLACK);
|
||||
canvas.setCursor(0, 25);
|
||||
canvas.setTextColor(ST77XX_WHITE);
|
||||
//canvas.println("Quad Encoder QT Demo");
|
||||
|
||||
for (int e=0; e<4; e++) {
|
||||
int32_t new_enc_position = ss.getEncoderPosition(e);
|
||||
// did we move around?
|
||||
if (enc_positions[e] != new_enc_position) {
|
||||
Serial.print("Rot. #");
|
||||
Serial.print(e);
|
||||
Serial.print(" -> ");
|
||||
Serial.println(new_enc_position); // display new position
|
||||
enc_positions[e] = new_enc_position; // and save for next round
|
||||
|
||||
// change the neopixel color, mulitply the new positiion by 4 to speed it up
|
||||
pixels.setPixelColor(e, Wheel((new_enc_position*4) & 0xFF));
|
||||
pixels.show();
|
||||
changed = true;
|
||||
}
|
||||
canvas.setTextColor(ST77XX_WHITE);
|
||||
canvas.print("Rotary #");
|
||||
canvas.print(e);
|
||||
canvas.print(": ");
|
||||
canvas.print(enc_positions[e]);
|
||||
|
||||
if (! ss.digitalRead(switch_pins[e])) {
|
||||
Serial.print("ENC");
|
||||
Serial.print("e");
|
||||
Serial.println("pressed!");
|
||||
canvas.setTextColor(ST77XX_RED);
|
||||
canvas.print(" DOWN");
|
||||
changed = true;
|
||||
}
|
||||
canvas.println();
|
||||
}
|
||||
|
||||
if (changed) {
|
||||
display.drawRGBBitmap(0, 0, canvas.getBuffer(), 240, 135);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* This is a demo for a QT Py RP2040 connected to a quad rotary encoder breakout
|
||||
* using the onboard Stemma QT Port
|
||||
* https://www.adafruit.com/product/4900
|
||||
* https://www.adafruit.com/product/5752
|
||||
*
|
||||
*/
|
||||
#include "Adafruit_seesaw.h"
|
||||
#include <seesaw_neopixel.h>
|
||||
|
||||
#define SS_NEO_PIN 18
|
||||
#define SS_ENC0_SWITCH 12
|
||||
#define SS_ENC1_SWITCH 14
|
||||
#define SS_ENC2_SWITCH 17
|
||||
#define SS_ENC3_SWITCH 9
|
||||
|
||||
#define SEESAW_ADDR 0x49
|
||||
|
||||
Adafruit_seesaw ss = Adafruit_seesaw(&Wire);
|
||||
seesaw_NeoPixel pixels = seesaw_NeoPixel(4, SS_NEO_PIN, NEO_GRB + NEO_KHZ800);
|
||||
|
||||
int32_t enc_positions[4] = {0, 0, 0, 0};
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
while (!Serial) delay(10);
|
||||
|
||||
Serial.println("Looking for seesaw!");
|
||||
|
||||
if (! ss.begin(SEESAW_ADDR) || !pixels.begin(SEESAW_ADDR)) {
|
||||
Serial.println("Couldn't find seesaw on default address");
|
||||
while(1) delay(10);
|
||||
}
|
||||
Serial.println("seesaw started");
|
||||
uint32_t version = ((ss.getVersion() >> 16) & 0xFFFF);
|
||||
if (version != 5752){
|
||||
Serial.print("Wrong firmware loaded? ");
|
||||
Serial.println(version);
|
||||
while(1) delay(10);
|
||||
}
|
||||
Serial.println("Found Product 5752");
|
||||
|
||||
ss.pinMode(SS_ENC0_SWITCH, INPUT_PULLUP);
|
||||
ss.pinMode(SS_ENC1_SWITCH, INPUT_PULLUP);
|
||||
ss.pinMode(SS_ENC2_SWITCH, INPUT_PULLUP);
|
||||
ss.pinMode(SS_ENC3_SWITCH, INPUT_PULLUP);
|
||||
ss.setGPIOInterrupts(1UL << SS_ENC0_SWITCH | 1UL << SS_ENC1_SWITCH |
|
||||
1UL << SS_ENC2_SWITCH | 1UL << SS_ENC3_SWITCH, 1);
|
||||
|
||||
|
||||
// get starting positions
|
||||
for (int e=0; e<4; e++) {
|
||||
enc_positions[e] = ss.getEncoderPosition(e);
|
||||
ss.enableEncoderInterrupt(e);
|
||||
}
|
||||
|
||||
Serial.println("Turning on interrupts");
|
||||
|
||||
pixels.setBrightness(255);
|
||||
pixels.show(); // Initialize all pixels to 'off'
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
if (! ss.digitalRead(SS_ENC0_SWITCH)) {
|
||||
Serial.println("ENC0 pressed!");
|
||||
}
|
||||
if (! ss.digitalRead(SS_ENC1_SWITCH)) {
|
||||
Serial.println("ENC1 pressed!");
|
||||
}
|
||||
if (! ss.digitalRead(SS_ENC2_SWITCH)) {
|
||||
Serial.println("ENC2 pressed!");
|
||||
}
|
||||
if (! ss.digitalRead(SS_ENC3_SWITCH)) {
|
||||
Serial.println("ENC3 pressed!");
|
||||
}
|
||||
|
||||
|
||||
for (int e=0; e<4; e++) {
|
||||
int32_t new_enc_position = ss.getEncoderPosition(e);
|
||||
// did we move around?
|
||||
if (enc_positions[e] != new_enc_position) {
|
||||
Serial.print("Encoder #");
|
||||
Serial.print(e);
|
||||
Serial.print(" -> ");
|
||||
Serial.println(new_enc_position); // display new position
|
||||
enc_positions[e] = new_enc_position; // and save for next round
|
||||
|
||||
// change the neopixel color, mulitply the new positiion by 4 to speed it up
|
||||
pixels.setPixelColor(e, Wheel((new_enc_position*4) & 0xFF));
|
||||
pixels.show();
|
||||
}
|
||||
}
|
||||
|
||||
// don't overwhelm serial port
|
||||
delay(10);
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* This example shows how to read from a seesaw encoder module.
|
||||
* The available encoder API is:
|
||||
* int32_t getEncoderPosition();
|
||||
int32_t getEncoderDelta();
|
||||
void enableEncoderInterrupt();
|
||||
void disableEncoderInterrupt();
|
||||
void setEncoderPosition(int32_t pos);
|
||||
*/
|
||||
#include "Adafruit_seesaw.h"
|
||||
#include <seesaw_neopixel.h>
|
||||
|
||||
#define SS_SWITCH 24
|
||||
#define SS_NEOPIX 6
|
||||
|
||||
#define SEESAW_ADDR 0x36
|
||||
|
||||
Adafruit_seesaw ss;
|
||||
seesaw_NeoPixel sspixel = seesaw_NeoPixel(1, SS_NEOPIX, NEO_GRB + NEO_KHZ800);
|
||||
|
||||
int32_t encoder_position;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
while (!Serial) delay(10);
|
||||
|
||||
Serial.println("Looking for seesaw!");
|
||||
|
||||
if (! ss.begin(SEESAW_ADDR) || ! sspixel.begin(SEESAW_ADDR)) {
|
||||
Serial.println("Couldn't find seesaw on default address");
|
||||
while(1) delay(10);
|
||||
}
|
||||
Serial.println("seesaw started");
|
||||
|
||||
uint32_t version = ((ss.getVersion() >> 16) & 0xFFFF);
|
||||
if (version != 4991){
|
||||
Serial.print("Wrong firmware loaded? ");
|
||||
Serial.println(version);
|
||||
while(1) delay(10);
|
||||
}
|
||||
Serial.println("Found Product 4991");
|
||||
|
||||
// set not so bright!
|
||||
sspixel.setBrightness(20);
|
||||
sspixel.show();
|
||||
|
||||
// use a pin for the built in encoder switch
|
||||
ss.pinMode(SS_SWITCH, INPUT_PULLUP);
|
||||
|
||||
// get starting position
|
||||
encoder_position = ss.getEncoderPosition();
|
||||
|
||||
Serial.println("Turning on interrupts");
|
||||
delay(10);
|
||||
ss.setGPIOInterrupts((uint32_t)1 << SS_SWITCH, 1);
|
||||
ss.enableEncoderInterrupt();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (! ss.digitalRead(SS_SWITCH)) {
|
||||
Serial.println("Button pressed!");
|
||||
}
|
||||
|
||||
int32_t new_position = ss.getEncoderPosition();
|
||||
// did we move arounde?
|
||||
if (encoder_position != new_position) {
|
||||
Serial.println(new_position); // display new position
|
||||
|
||||
// change the neopixel color
|
||||
sspixel.setPixelColor(0, Wheel(new_position & 0xFF));
|
||||
sspixel.show();
|
||||
encoder_position = new_position; // and save for next round
|
||||
}
|
||||
|
||||
// don't overwhelm serial port
|
||||
delay(10);
|
||||
}
|
||||
|
||||
|
||||
uint32_t Wheel(byte WheelPos) {
|
||||
WheelPos = 255 - WheelPos;
|
||||
if (WheelPos < 85) {
|
||||
return sspixel.Color(255 - WheelPos * 3, 0, WheelPos * 3);
|
||||
}
|
||||
if (WheelPos < 170) {
|
||||
WheelPos -= 85;
|
||||
return sspixel.Color(0, WheelPos * 3, 255 - WheelPos * 3);
|
||||
}
|
||||
WheelPos -= 170;
|
||||
return sspixel.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
/* Demo with 128x64 OLED display and multiple I2C encoders wired up. The sketch will auto-
|
||||
* detect up to 4 encoder on the first 4 addresses. Twisting will display text on OLED
|
||||
* and change neopixel color.
|
||||
* set USE_OLED to true t
|
||||
*/
|
||||
|
||||
#define USE_OLED false // set to false to skip the OLED, true to use it!
|
||||
|
||||
|
||||
#include "Adafruit_seesaw.h"
|
||||
#include <seesaw_neopixel.h>
|
||||
|
||||
#if USE_OLED
|
||||
#include <Adafruit_SH110X.h>
|
||||
#include <Fonts/FreeSans9pt7b.h>
|
||||
Adafruit_SH1107 display = Adafruit_SH1107(64, 128, &Wire);
|
||||
#endif
|
||||
|
||||
#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
|
||||
|
||||
|
||||
// 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("128x64 OLED + seesaw Encoders test");
|
||||
|
||||
#if USE_OLED
|
||||
display.begin(0x3C, true); // Address 0x3C default
|
||||
Serial.println("OLED begun");
|
||||
display.display();
|
||||
delay(500); // Pause for half second
|
||||
display.setRotation(1);
|
||||
display.setFont(&FreeSans9pt7b);
|
||||
display.setTextColor(SH110X_WHITE);
|
||||
#endif
|
||||
|
||||
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() {
|
||||
#if USE_OLED
|
||||
display.clearDisplay();
|
||||
uint16_t display_line = 1;
|
||||
#endif
|
||||
|
||||
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;
|
||||
|
||||
// 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 USE_OLED
|
||||
// draw the display
|
||||
display.setCursor(0, 20*display_line++);
|
||||
display.print("Enc #");
|
||||
display.print(enc);
|
||||
display.print(" : ");
|
||||
display.print(encoder_positions[enc]);
|
||||
#endif
|
||||
|
||||
if (! encoders[enc].digitalRead(SS_SWITCH)) {
|
||||
Serial.print("Encoder #");
|
||||
Serial.print(enc);
|
||||
Serial.println(" pressed");
|
||||
#if USE_OLED
|
||||
display.print(" P");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
#if USE_OLED
|
||||
display.display();
|
||||
#endif
|
||||
|
||||
// don't overwhelm serial port
|
||||
yield();
|
||||
delay(10);
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
Reference in New Issue
Block a user