Close

Arduino skeleton script

A project log for LAMEBOY - another ESP12 handheld

fully portable ESP12 project with battery charging and power muxing

davedarkodavedarko 03/14/2018 at 11:230 Comments

no wifi, 4 stars.

#include <ESP8266WiFi.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
#include <Wire.h>

uint32_t bat = 4200;
Adafruit_PCD8544 display = Adafruit_PCD8544(2, 15, 0);

#define BUTTON_UP    B10000000
#define BUTTON_DOWN  B00100000
#define BUTTON_LEFT  B01000000
#define BUTTON_RIGHT B00010000
#define BUTTON_A     B00000100
#define BUTTON_B     B00001000
#define BUTTON_C     B00000001
#define BUTTON_SD    B00000010

boolean bottonU_pressed = false;
boolean bottonD_pressed = false;
boolean bottonL_pressed = false;
boolean bottonR_pressed = false;
boolean bottonA_pressed = false;
boolean bottonB_pressed = false;
boolean bottonC_pressed = false;
boolean bottonSD_pressed = false;

void setup()
{
  
  Serial.begin(115200);
  Wire.begin();
  setBackgroundColor('c', 200,200,200);
  
  display.begin();
  display.clearDisplay();
  display.setRotation(2);
  display.setContrast(58);
  display.display();

  setBackgroundColor('n', 255, 100, 100);
  display.clearDisplay();
  display.setCursor(0, 0);
  display.setTextSize(2);
  display.setTextColor(BLACK);
  display.println("OH HI!");
  display.display();
  delay(2000);

  setBackgroundColor('c', 200 ,200, 200);
  display.setTextColor(BLACK, WHITE);
  display.setTextSize(1);
  
}

void loop() 
{
  read_buttons();
  display.clearDisplay();
  uint32_t aIn = analogRead(A0);
  aIn = map(aIn, 0, 1024, 0, 4400); 
  bat = (aIn+bat)/2;
  display.println(bat);
  if (bottonU_pressed) display.println("U");
  if (bottonD_pressed) display.println("D");
  if (bottonL_pressed) display.println("L");
  if (bottonR_pressed) display.println("R");
  if (bottonA_pressed) display.println("A");
  if (bottonB_pressed) display.println("B");
  if (bottonC_pressed) display.println("C");
  if (bottonSD_pressed) display.println("SD");
  display.display();
  delay(25);
}

void setBackgroundColor(char c, int r, int g, int b)
{
  // c is fade to color, n is now color
  Wire.beginTransmission(0x80);
  Wire.write(c);
  Wire.write(r);
  Wire.write(g);
  Wire.write(b);
  Wire.endTransmission();
}

void read_buttons()
{
  Wire.requestFrom(0x20, 1);
  byte data = Wire.read();
  bottonU_pressed = (~data & BUTTON_UP) > 0;
  bottonD_pressed = (~data & BUTTON_DOWN) > 0;
  bottonL_pressed = (~data & BUTTON_LEFT) > 0;
  bottonR_pressed = (~data & BUTTON_RIGHT) > 0;
  bottonA_pressed = (~data & BUTTON_A) > 0;
  bottonB_pressed = (~data & BUTTON_B) > 0;
  bottonC_pressed = (~data & BUTTON_C) > 0;
  bottonSD_pressed = (~data & BUTTON_SD) > 0;
}

Discussions