#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED width, in pixels
#define SCREEN_HEIGHT 64 // OLED height, in pixels
const unsigned char PROGMEM d20[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x07, 0xc0, 0x00, 0x00, 0x19, 0x30, 0x00, 0x00, 0x21, 0x08, 0x00, 0x00, 0xc7, 0xc6, 0x00,
0x03, 0x3a, 0xb9, 0x80, 0x0c, 0xc4, 0x46, 0x60, 0x1f, 0x04, 0x41, 0xf0, 0x18, 0x08, 0x20, 0x30,
0x18, 0x10, 0x10, 0x30, 0x18, 0x10, 0x10, 0x30, 0x14, 0x20, 0x08, 0x50, 0x14, 0x40, 0x04, 0x50,
0x14, 0x40, 0x04, 0x50, 0x14, 0x80, 0x02, 0x50, 0x14, 0x80, 0x02, 0x50, 0x12, 0x80, 0x02, 0x90,
0x13, 0x00, 0x01, 0x90, 0x12, 0x00, 0x00, 0x90, 0x13, 0xff, 0xff, 0x90, 0x16, 0x80, 0x02, 0xd0,
0x1c, 0x40, 0x04, 0x70, 0x18, 0x20, 0x08, 0x30, 0x06, 0x10, 0x10, 0xc0, 0x01, 0x88, 0x23, 0x00,
0x00, 0x44, 0x44, 0x00, 0x00, 0x32, 0x98, 0x00, 0x00, 0x0d, 0x60, 0x00, 0x00, 0x03, 0x80, 0x00};
const unsigned char d12[] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0xc0, 0x00, 0x00, 0x18, 0x30, 0x00, 0x00, 0x20, 0x08, 0x00,
0x00, 0x40, 0x04, 0x00, 0x00, 0xa0, 0x0a, 0x00, 0x01, 0x1f, 0xf1, 0x00, 0x01, 0x10, 0x11, 0x00,
0x01, 0x20, 0x09, 0x00, 0x01, 0x20, 0x09, 0x00, 0x01, 0x20, 0x09, 0x00, 0x01, 0x20, 0x09, 0x00,
0x01, 0x10, 0x11, 0x00, 0x01, 0x28, 0x29, 0x00, 0x01, 0x44, 0x45, 0x00, 0x00, 0x83, 0x82, 0x00,
0x00, 0x41, 0x04, 0x00, 0x00, 0x21, 0x08, 0x00, 0x00, 0x19, 0x30, 0x00, 0x00, 0x07, 0xc0, 0x00
};
const unsigned char d10[] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x80, 0x00,
0x00, 0x06, 0xc0, 0x00, 0x00, 0x0c, 0x60, 0x00, 0x00, 0x14, 0x50, 0x00, 0x00, 0x28, 0x28, 0x00,
0x00, 0x48, 0x24, 0x00, 0x00, 0x90, 0x12, 0x00, 0x00, 0x90, 0x12, 0x00, 0x00, 0xe0, 0x0e, 0x00,
0x00, 0x90, 0x12, 0x00, 0x00, 0x48, 0x24, 0x00, 0x00, 0x24, 0x48, 0x00, 0x00, 0x13, 0x90, 0x00,
0x00, 0x09, 0x20, 0x00, 0x00, 0x05, 0x40, 0x00, 0x00, 0x03, 0x80, 0x00, 0x00, 0x01, 0x00, 0x00
};
const unsigned char d8 [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x80, 0x00, 0x00, 0x05, 0x40, 0x00,
0x00, 0x09, 0x20, 0x00, 0x00, 0x11, 0x10, 0x00, 0x00, 0x21, 0x08, 0x00, 0x00, 0x41, 0x04, 0x00,
0x00, 0x81, 0x02, 0x00, 0x01, 0xc1, 0x07, 0x00, 0x02, 0x39, 0x38, 0x80, 0x01, 0x06, 0xc1, 0x00,
0x00, 0x81, 0x02, 0x00, 0x00, 0x41, 0x04, 0x00, 0x00, 0x21, 0x08, 0x00, 0x00, 0x11, 0x10, 0x00,
0x00, 0x09, 0x20, 0x00, 0x00, 0x05, 0x40, 0x00, 0x00, 0x03, 0x80, 0x00, 0x00, 0x01, 0x00, 0x00
};
const unsigned char d6 [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xe0, 0x00, 0x02, 0x00, 0x30, 0x00,
0x04, 0x00, 0x50, 0x00, 0x08, 0x00, 0x88, 0x00, 0x10, 0x01, 0x08, 0x00, 0x20, 0x02, 0x08, 0x00,
0x40, 0x04, 0x04, 0x00, 0x7f, 0xf8, 0x04, 0x00, 0x40, 0x08, 0x02, 0x00, 0x40, 0x08, 0x02, 0x00,
0x40, 0x08, 0x02, 0x00, 0x20, 0x04, 0x04, 0x00, 0x20, 0x04, 0x08, 0x00, 0x10, 0x02, 0x10, 0x00,
0x10, 0x02, 0x20, 0x00, 0x10, 0x02, 0x40, 0x00, 0x08, 0x01, 0x80, 0x00, 0x0f, 0xff, 0x00, 0x00
};
const unsigned char d4 [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x03, 0x80, 0x00, 0x00, 0x05, 0x40, 0x00, 0x00, 0x09, 0x20, 0x00, 0x00, 0x09, 0x20, 0x00,
0x00, 0x11, 0x10, 0x00, 0x00, 0x21, 0x08, 0x00, 0x00, 0x20, 0x88, 0x00, 0x00, 0x40, 0x84, 0x00,
0x00, 0x80, 0x82, 0x00, 0x00, 0x80, 0x82, 0x00, 0x01, 0x00, 0x81, 0x00, 0x02, 0x00, 0x80, 0x80,
0x02, 0x00, 0x40, 0x80, 0x04, 0x00, 0x40, 0x40, 0x04, 0x00, 0x40, 0x40, 0x03, 0x00, 0x40, 0x40,
0x00, 0xc0, 0x41, 0x80, 0x00, 0x30, 0x26, 0x00, 0x00, 0x0c, 0x38, 0x00, 0x00, 0x03, 0xe0, 0x00
};
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Create an instance of the character
int buttonPins[3] = {32,33,25};
int currentState[3];
int lastState[3] = {HIGH,HIGH,HIGH};
int diceValues[6]= {4,6,8,10,12,20};
int diceState = 5; // 0= d4, 1= d6, 2= d8, 3= d10, 4= d12, 5=20
int gameState = 0; //0= select dice, 1= select bonus, 2=roll
int bobCounter = 0; // advances every frame
int bobAmplitude = 4; // how many pixels up/down
float bobFrequency = 0.15;
int bonusMod= 0;
int totRoll = 0;
void drawBobbingBitmap(int x, int yBase, const unsigned char* bitmap, int w, int h, int frame) {
int yOffset = sin(frame * bobFrequency) * bobAmplitude; // compute vertical bob
oled.drawBitmap(x, yBase + yOffset, bitmap, w, h, WHITE);
}
void updateControls(){
switch (gameState){
case 0:
for (int i=0; i<3; i++){
currentState[i] = digitalRead(buttonPins[i]);
if (lastState[i]== HIGH && currentState[i]==LOW) {
if (i==0&& diceState!=0){
diceState = diceState -1;
} else if (i==1){
gameState ++;
} else if (i==2&& diceState!=5){
diceState++;
}
}
lastState[i] = currentState[i];
}
delay(20);
break;
case 1:
for (int i=0; i<3; i++){
currentState[i] = digitalRead(buttonPins[i]);
if (lastState[i]== HIGH && currentState[i]==LOW) {
if (i==0){
bonusMod--;
} else if (i==1){
gameState++;
} else if (i==2){
bonusMod++;
}
}
lastState[i] = currentState[i];
}
delay(20);
Serial.print(bonusMod);
break;
case 3:
//totRoll= rand(diceValues[diceState]) + bonusMod;
break;
}
// animation
}
void drawUI(){
switch (gameState){
case 0:
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(26, 0);
oled.println("Select a dice");
oled.setCursor(60, 57);
switch (diceState){
case 0:
oled.println("d4");
drawBobbingBitmap(52, 10, d4, 32, 32, bobCounter);
break;
case 1:
oled.println("d6");
drawBobbingBitmap(56, 10, d6, 32, 32, bobCounter);
break;
case 2:
oled.println("d8");
drawBobbingBitmap(52, 10, d8, 32, 32, bobCounter);
break;
case 3:
//oled.setCursor(54, 57);
oled.println("d10");
drawBobbingBitmap(52, 10, d10, 32, 32, bobCounter);
break;
case 4:
//oled.setCursor(54, 57);
oled.println("d12");
drawBobbingBitmap(52, 10, d12, 32, 32, bobCounter);
break;
case 5:
//oled.setCursor(54, 57);
oled.println("d20");
drawBobbingBitmap(52, 15, d20, 32, 32, bobCounter);
break;
}
break;
case 1:
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(13, 0);
oled.println("Add roll Modifier");
switch (bonusMod) {
case 10 ... 99:
oled.setTextSize(2);
oled.setTextColor(WHITE);
oled.setCursor(54, 30);
oled.println(bonusMod);
oled.setCursor(42, 30);
oled.println("+");
break;
case -99 ... -1:
oled.setTextSize(2);
oled.setTextColor(WHITE);
oled.setCursor(48, 30);
oled.println(bonusMod);
break;
default:
oled.setTextSize(2);
oled.setTextColor(WHITE);
oled.setCursor(60, 30);
oled.println(bonusMod);
oled.setCursor(48, 30);
oled.println("+");
break;
}
break;
}
oled.display();
}
void drawDice(){
}
void setup() {
Serial.begin(9600);
for (int i = 0; i < 3; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
// initialize OLED display with I2C address 0x3C
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("failed to start SSD1306 OLED"));
while (1);
}
delay(2000); // wait two seconds for initializing
oled.clearDisplay(); // clear display
oled.setTextSize(1); // set text size
oled.setTextColor(WHITE); // set text color
oled.setCursor(0, 2); // set position to display (x,y)
oled.println("Robotronix"); // set text
oled.display();
}
void loop() {
updateControls();
oled.clearDisplay();
drawUI();
drawDice();
bobCounter++;
}