/hardware/

8BitConsole-demo

2020-12-31 06:00:00

Demo.ino

#include <LedControl.h>

const uint64_t LOADER[] = { 
  0x000000000f0f0f0f, 
  0x00000000f0f0f0f0, 
  0xf0f0f0f000000000, 
  0x0f0f0f0f00000000, 
  0x0f0f0f0ff0f0f0f0, 
  0xf0f0f0f00f0f0f0f, 
  0xffffffffffffffff, 
  0x0000000000000000,
};

const uint64_t IMG_LEFT = 0x1018fcfefc181000;
const uint64_t IMG_RIGHT = 0x10307efe7e301000;
const uint64_t IMG_UP = 0x383838fe7c381000;
const uint64_t IMG_DOWN = 0x10387cfe38383800;
const uint64_t IMG_A = 0x6666667e66663c00;
const uint64_t IMG_B = 0x3e66663e66663e00;
const uint64_t IMG_EMPTY = 0x0000000000000000;

const uint64_t FULL = 0xffffffffffffffff;

const int c = 261;
const int d = 294;
const int e = 329;
const int f = 349;
const int g = 391;
const int gS = 415;

#define BTN_RIGHT A0
#define BTN_UP A1
#define BTN_DOWN A2
#define BTN_LEFT A3
#define BTN_A A4
#define BTN_B A5

#define LOAD_PIN 7
#define CLOCK_PIN 6
#define DATA_PIN 8

#define CLOCK_COUNTER_PIN 2
#define LOAD_COUNTER_PIN 3
#define DATA_COUNTER_PIN 4

#define BUZZER_PIN 5

LedControl display = LedControl(DATA_PIN, CLOCK_PIN, LOAD_PIN);
LedControl display_counter = LedControl(DATA_COUNTER_PIN, CLOCK_COUNTER_PIN, LOAD_COUNTER_PIN,3);


void setup() {
  display.clearDisplay(0);
  display.shutdown(0, false);
  display.setIntensity(0, 5);

  display_counter.shutdown(0,false);
  display_counter.setIntensity(0,9);
  display_counter.clearDisplay(0);

  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);
  pinMode(A4, INPUT);
  pinMode(A5, INPUT);

  showLoader();
  displayImage(IMG_EMPTY);
  counter_demo();
  full();
}

void full(){
  displayImage(FULL);
  print(1, 8);
  print(2, 8);
  print(3, 8);
  delay(1000);
}

void loop() {
  if(digitalRead(BTN_RIGHT) == 1) {
    beep(c, 255);
    print(1, 1);
    print(2, 1);
    print(3, 1);
    displayImage(IMG_RIGHT);
  }
  if(digitalRead(BTN_LEFT) == 1) {
    beep(d, 255);
    print(1, 2);
    print(2, 2);
    print(3, 2);
    displayImage(IMG_LEFT);
  }
  if(digitalRead(BTN_UP) == 1) {
    beep(e, 255);
    print(1, 3);
    print(2, 3);
    print(3, 3);
    displayImage(IMG_UP);
  }
  if(digitalRead(BTN_DOWN) == 1) {
    beep(f, 255);
    print(1, 4);
    print(2, 4);
    print(3, 4);
    displayImage(IMG_DOWN);
  }
  if(digitalRead(BTN_A) == 1) {
    beep(g, 255);
    print(1, 5);
    print(2, 5);
    print(3, 5);
    displayImage(IMG_A);
  }
  if(digitalRead(BTN_B) == 1) {
    beep(gS, 255);
    print(1, 6);
    print(2, 6);
    print(3, 6);
    displayImage(IMG_B);
  }
}

void counter_demo() {

  int first;
  int second;
  int third;  

  for(int base=0;base<1000;base++) {

    first = base / 100 % 10;
    second =  base / 10 % 10;
    third = base % 10;

    print(1, first);
    print(2, second);
    print(3, third);

    delay(10);
  }
}

void showLoader() {
  for (int i = 0; i <= 7; i++) {
    displayImage(LOADER[i]);
    delay(300);
  }
}

void beep(int note, int duration) {
  tone(BUZZER_PIN, note, duration);
  delay(200);
  noTone(BUZZER_PIN);
}

void print(byte d,byte n) {
  display_counter.setColumn(0,d,pgm_read_byte_near(charTable + n));
}

void displayImage(uint64_t image) {
  for (int i = 0; i < 8; i++) {
    byte row = (image >> i * 8) & 0xFF;
    for (int j = 0; j < 8; j++) {
      display.setLed(0, i, j, bitRead(row, j));
    }
  }
}