Skip to content

Instantly share code, notes, and snippets.

@nealey
Created October 13, 2019 15:28
Show Gist options
  • Save nealey/1e6d0173d250671d4988e2bff2d95f35 to your computer and use it in GitHub Desktop.
Save nealey/1e6d0173d250671d4988e2bff2d95f35 to your computer and use it in GitHub Desktop.
/* e-paper display lib */
#include <GxEPD.h>
//Use the GxGDEW029T5 class if you have Badgy Rev 2C. Make sure you are on GxEPD 3.05 or above
#include <GxGDEW029T5/GxGDEW029T5.h>
#include <GxIO/GxIO_SPI/GxIO_SPI.h>
#include <GxIO/GxIO.h>
#include <Fonts/FreeSansBold9pt7b.h>
#include <Fonts/FreeSansBold12pt7b.h>
#include <Fonts/FreeSansBold24pt7b.h>
/* WiFi libs*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPUpdateServer.h>
#include <WiFiManager.h>
#define len(a) (sizeof(a) / sizeof(*a))
char *badges[][4] = {
{
"Neale", "Pickett",
"Los Alamos National Laboratory",
"Cyber Fire Lead",
},
{
"Neale", "Pickett",
"Cybersecurity Educator",
"Los Alamos National Laboratory",
},
{
"Neale", "Pickett",
"",
"",
},
NULL
};
int badgeno = 0;
/* Always include the update server, or else you won't be able to do OTA updates! */
/**/const int port = 8888;
/**/ESP8266WebServer httpServer(port);
/**/ESP8266HTTPUpdateServer httpUpdater;
/* */
/* Configure pins for display */
GxIO_Class io(SPI, SS, 0, 2);
GxEPD_Class display(io); // default selection of D4, D2
/* A single byte is used to store the button states for debouncing */
byte buttonState = 0;
byte lastButtonState = 0; //the previous reading from the input pin
unsigned long lastDebounceTime = 0; //the last time the output pin was toggled
unsigned long debounceDelay = 50; //the debounce time
void setup()
{
display.init();
pinMode(1,INPUT_PULLUP); //down
pinMode(3,INPUT_PULLUP); //left
pinMode(5,INPUT_PULLUP); //center
pinMode(12,INPUT_PULLUP); //right
pinMode(10,INPUT_PULLUP); //up
/* Enter OTA mode if the center button is pressed */
if(digitalRead(5) == 0){
/* WiFi Manager automatically connects using the saved credentials, if that fails it will go into AP mode */
WiFiManager wifiManager;
wifiManager.setAPCallback(configModeCallback);
wifiManager.autoConnect("Badgy AP");
/* Once connected to WiFi, startup the OTA update server if the center button is held on boot */
httpUpdater.setup(&httpServer);
httpServer.begin();
showIP();
while(1){
httpServer.handleClient();
}
}
showBadge(); //show "Hello my name is" immediately on boot
}
void loop()
{
byte reading = (digitalRead(1) == 0 ? 0 : (1<<0)) | //down
(digitalRead(3) == 0 ? 0 : (1<<1)) | //left
(digitalRead(5) == 0 ? 0 : (1<<2)) | //center
(digitalRead(12) == 0 ? 0 : (1<<3)) | //right
(digitalRead(10) == 0 ? 0 : (1<<4)); //up
if(reading != lastButtonState){
lastDebounceTime = millis();
}
if((millis() - lastDebounceTime) > debounceDelay){
if(reading != buttonState){
buttonState = reading;
for(int i=0; i<5; i++){
if(bitRead(buttonState, i) == 0){
switch(i){
case 0:
//do something when the user presses down
showText("You pressed the down button!");
break;
case 1:
//do something when the user presses left
showText("You pressed the left button!");
break;
case 2:
//do something when the user presses center
badgeno += 1;
if (badges[badgeno][0] == NULL) {
badgeno = 0;
}
showBadge();
break;
case 3:
//do something when the user presses right
showText("You pressed the right button!");
break;
case 4:
//do something when the user presses up
showText("You pressed the up button!");
break;
default:
break;
}
}
}
}
}
lastButtonState = reading;
}
void configModeCallback (WiFiManager *myWiFiManager){
display.setRotation(3); //even = portrait, odd = landscape
display.fillScreen(GxEPD_WHITE);
const GFXfont* f = &FreeSansBold9pt7b ;
display.setTextColor(GxEPD_BLACK);
display.setFont(f);
display.setCursor(0,50);
display.println("Connect to Badgy AP");
display.println("to setup your WiFi!");
display.update();
}
void showText(char *text)
{
display.setRotation(3); //even = portrait, odd = landscape
display.fillScreen(GxEPD_WHITE);
const GFXfont* f = &FreeSansBold9pt7b ;
display.setTextColor(GxEPD_BLACK);
display.setFont(f);
display.setCursor(10,70);
display.println(text);
display.update();
}
void showIP(){
display.setRotation(3); //even = portrait, odd = landscape
display.fillScreen(GxEPD_WHITE);
const GFXfont* f = &FreeSansBold9pt7b ;
display.setTextColor(GxEPD_BLACK);
display.setFont(f);
display.setCursor(0,10);
String url = WiFi.localIP().toString() + ":"+String(port)+"/update";
byte charArraySize = url.length() + 1;
char urlCharArray[charArraySize];
url.toCharArray(urlCharArray, charArraySize);
display.println("You are now connected!");
display.println("");
display.println("Go to:");
display.println(urlCharArray);
display.println("to upload a new sketch.");
display.update();
}
void showBadge()
{
display.setRotation(3); //even = portrait, odd = landscape
display.fillScreen(GxEPD_WHITE);
display.setTextColor(GxEPD_BLACK);
display.setCursor(30, 35);
display.setFont(&FreeSansBold24pt7b);
display.println(badges[badgeno][0]);
display.setCursor(30, 65);
display.setFont(&FreeSansBold12pt7b);
display.println(badges[badgeno][1]);
display.setCursor(0, 100);
display.setFont(&FreeSansBold9pt7b);
display.println(badges[badgeno][2]);
display.println(badges[badgeno][3]);
display.update();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment