#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//--------------- OLED ----------------------------------------
// Initialise the display
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//Define Pins
#define OLED_RESET 4
#define CONTROL_A A0
//Define Visuals
#define FONT_SIZE 2
#define SCREEN_WIDTH 127 //real size minus 1, because coordinate system starts with 0
#define SCREEN_HEIGHT 63 //real size minus 1, because coordinate system starts with 0
#define PADDLE_WIDTH 4
#define PADDLE_HEIGHT 10
#define PADDLE_PADDING 10
#define SCORE_PADDING 10
int paddleLocationA = 0;
int paddleLocationB = 0;
int lastPaddleLocationA = 0;
int lastPaddleLocationB = 0;
int scoreA = 0;
//--------------- SETUP Y LOOP ----------------------------------------
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3D); // initialize with the I2C addr 0x3D (for the 128x64)
// initialise the display
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
display.setTextWrap(false);
display.setTextColor(WHITE);
display.setTextSize(FONT_SIZE);
display.clearDisplay();
}
void loop() {
calculateMovement();
draw();
}
//-------------------------------------------------------
void calculateMovement()
{
int controlA = analogRead(CONTROL_A); // Ambos paddles se mueven por la salida de un mismo potenciometro.
paddleLocationA = map(controlA, 0, 1023, 0, SCREEN_HEIGHT - PADDLE_HEIGHT);
paddleLocationB = map(controlA, 0, 1023, 0, SCREEN_HEIGHT - PADDLE_HEIGHT); // Paddle B tiene los mismos movimientos de paddle A
int paddleSpeedA = paddleLocationA - lastPaddleLocationA;
int paddleSpeedB = paddleLocationB - lastPaddleLocationB;
//set last paddle locations
lastPaddleLocationA = paddleLocationA;
lastPaddleLocationB = paddleLocationB;
scoreA = paddleLocationA;
}
void draw()
{
display.clearDisplay();
//--------------- Dibujar paddles ----------------------------------------
//draw paddle A
display.fillRect(PADDLE_PADDING,paddleLocationA,PADDLE_WIDTH,PADDLE_HEIGHT,WHITE);
//draw paddle B
display.fillRect(SCREEN_WIDTH-PADDLE_WIDTH-PADDLE_PADDING,paddleLocationB,PADDLE_WIDTH,PADDLE_HEIGHT,WHITE);
//--------------- Dibujar linea central ----------------------------------------
//draw center line
for (int i=0; i<SCREEN_HEIGHT; i+=4) {
display.drawFastVLine(SCREEN_WIDTH/2, i, 2, WHITE);
}
//--------------- Puntaje equivalente a posición en Y ----------------------------------------
int scoreAWidth = 5 * FONT_SIZE;
if (scoreA > 9) scoreAWidth += 6 * FONT_SIZE;
if (scoreA > 99) scoreAWidth += 6 * FONT_SIZE;
// Se ubica el cursor en la posición del valor izquierdo.
display.setCursor(SCREEN_WIDTH/2 - SCORE_PADDING - scoreAWidth,0);
// Se imprime para actualizar el valor.
display.print(scoreA);
// Se ubica el cursor en la posición del valor derecho.
display.setCursor(SCREEN_WIDTH/2 + SCORE_PADDING+1,0); //+1 because of dotted line.
// Se imprime para actualizar el valor.
display.print(scoreA);
display.display();
}