From 9d6f0cce50cb2cde4eb558bac73277eb5395cfcf Mon Sep 17 00:00:00 2001 From: Julian Date: Mon, 5 Nov 2018 08:03:05 +0100 Subject: Added display drivers, and simple demo --- Jetris.ino | 212 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ MaxCommands.h | 18 +++++ ] | 62 +++++++++++++++++ sprites.h | 62 +++++++++++++++++ tags | 27 ++++++++ 5 files changed, 381 insertions(+) create mode 100644 Jetris.ino create mode 100644 MaxCommands.h create mode 100644 ] create mode 100644 sprites.h create mode 100644 tags diff --git a/Jetris.ino b/Jetris.ino new file mode 100644 index 0000000..970ada2 --- /dev/null +++ b/Jetris.ino @@ -0,0 +1,212 @@ +// THings that miss +// Functions that return something + +#include "sprites.h" +#include "MaxCommands.h" + +#define clk 5 +#define cs 6 +#define dataIn 3 +#define setDisplay(d) (curDisplay = d) + +//Use the drawDot function to draw +uint8_t screenBuffer[16]; + +int curDisplay = 0; + +void setup() { + //Init Serial + Serial.begin(9600); + + Serial.println("Starting up \n\n\n"); + + //Init pins + pinMode(cs, OUTPUT); + pinMode(clk, OUTPUT); + pinMode(dataIn, OUTPUT); + + //Setup displays + setDisplay(0); + initDisplay(); + + setDisplay(1); + initDisplay(); + + + +} + +///////////////////////////// +// GAME LOGIC // +///////////////////////////// + +int ballX = 4; +int ballY = 0; + +int ballXv = 1; +int ballYv = 1; + +int last = 0; + +Sprite *logo = &ball; + +void loop() { + delay(100); + + if(ballX + ballXv + logo->width > 8 || ballX + ballXv < 0){ + ballXv = -ballXv; + } + if(ballY + ballYv + logo->height > 16 || ballY + ballYv < 0){ + ballYv = -ballYv; + } + + ballX += ballXv; + ballY += ballYv; + + //Clear screen + drawRegion(0xFF, 0xFFFF, false); + + //Draw ball + drawSprite(ballX, ballY, logo); + + renderAll(); + //renderToSerial(); + + +} + +void renderAll(){ + setDisplay(0); + render(screenBuffer); + setDisplay(1); + render(screenBuffer + 8); +} + +void renderToSerial() { + Serial.write(0x33); Serial.println("[2J"); + for(int i = 0; i < 16; i++) { + for(int b = 0; b < 8; b++) { + Serial.print( ( screenBuffer[i] & ( 1 << 7 - b ) ) > 0 ); + } + Serial.println(); + } +} + +///////////////////////////// +// Screendrawing routines // +///////////////////////////// + +void initDisplay() { + + //clear all registers + for ( int i = 0; i < 0x0F; i++) { + writeCommand(i, 0); + } + + //Turn it on + writeCommand(maxSHUTDOWN_INV, 1); + + //Darker please + writeCommand(maxINTENSITY, 0x00); + + //Activate all lines + writeCommand(maxSCAN_LIMIT, 0x07); + +} + +//Draw a region, where mask specifies where to draw. +// xMask = 0b00111100, yMask = 0b01111110 will draw a small 4x6 box +void drawSprite(uint8_t x, uint8_t y, Sprite* sprite) { + //Check if in range + if (x + sprite->width - 1 > 7 || y + sprite->height - 1> 15 ) + return; + + //Clear out drawing area + uint8_t xMask = (uint8_t)(~( (1 << ( 8 - sprite->width ) ) -1 )) >> x; + uint16_t yMask = ( (1 << sprite->height ) -1 ) << y; + + drawRegion(xMask, yMask, false); + + + for ( int i = y; i < sprite->height + y; i++) { + //Calculate bits + uint8_t row = screenBuffer[i]; + row |= sprite->buff[i - y] >> x; + + screenBuffer[i] = row; + + } + +} + +void drawDot(uint8_t x, uint8_t y, bool state) { + //Check if in range + if (x > 7 || y > 16) + return; + //Generate masks + uint8_t xMask = 1 << x; + uint8_t yMask = 1 << y; + + //Draw the bit + drawRegion(xMask, yMask, state); +} + +void drawRegion(uint8_t xMask, uint16_t yMask, bool state) { + for (int i = 0; i < 16; i++ ) { + //If y index not in mask, go to next + if (! ( yMask & 1 << i ) ) + continue; + + //Flip the bits + uint8_t row = screenBuffer[i]; + if ( state ) { + //Write 1 where on the 1's places in xMask + row |= xMask; + } else { + //Write 0. ~ means bitwise NOT + row &= ~( xMask ); + } + screenBuffer[i] = row; + } +} + +void render(uint8_t buffer[]) { + for(int i = 0; i < 8; i++ ) { + + writeCommand(maxDIGIT_0 + i, buffer[i]); + } + + //Shift everything throught + for( int i = 0; i < curDisplay; i++) { + writeCommand(0, 0); + } + +} + +void writeCommand(uint8_t addr, uint8_t data) { + uint16_t uint8_tToWrite = addr << 8 | data; + + //Set Chip select low + digitalWrite(cs, LOW); + + //MSBFIRST is a arduino standard, which says that data goes from left to right + + shiftOut(dataIn, clk, MSBFIRST, addr); + shiftOut(dataIn, clk, MSBFIRST, data); + + //To get to other displays padding is added to step through them + serialPad(curDisplay * 2); + + digitalWrite(cs, HIGH); + + //To Clear out other displays more padding is added + serialPad(2 * 2); +} + +void serialPad(int count) { + + for(int i = 0; i < count; i++) { + //Pad with no-ops + shiftOut(dataIn, clk, MSBFIRST, 0); + } +} diff --git a/MaxCommands.h b/MaxCommands.h new file mode 100644 index 0000000..2baff64 --- /dev/null +++ b/MaxCommands.h @@ -0,0 +1,18 @@ +#ifndef MaxCommands +#define MaxCommands + +#define maxSHUTDOWN_INV 0x0C +#define maxTEST 0x0F +#define maxINTENSITY 0x0A +#define maxDIGIT_0 0x01 +#define maxDIGIT_1 0x02 +#define maxDIGIT_2 0x03 +#define maxDIGIT_3 0x04 +#define maxDIGIT_4 0x05 +#define maxDIGIT_5 0x06 +#define maxDIGIT_6 0x07 +#define maxDIGIT_7 0x08 +#define maxDECODE_MODE 0x09 +#define maxSCAN_LIMIT 0x0B + +#endif diff --git a/] b/] new file mode 100644 index 0000000..f335939 --- /dev/null +++ b/] @@ -0,0 +1,62 @@ +#include "maxCommands.h" + +#define cs 6 +#define clk 5 +#define dataIn 3 + + +void setup() { + Serial.begin(9600); + + Serial.println("Starting up \n\n\n"); + + pinMode(cs, OUTPUT); + pinMode(clk, OUTPUT); + pinMode(dataIn, OUTPUT); + + + digitalWrite(cs, HIGH); + + writeCommand(maxSHUTDOWN_INV, 1); + + writeCommand(maxINTENSITY, 0x00); + + +} + +void loop() { + + for ( int i = 0; i < 255; i++) { + writeCommand(maxDIGIT_1, i); + + delay(200); + } + +} + + +void writeCommand(uint8_t addr, uint8_t data) { + uint16_t byteToWrite = addr << 8 | data; + + //Set Chip select low + digitalWrite(cs, LOW); + + Serial.print("Writing: "); + for (int i = 0; i < 16; i++) { + bool bitToWrite = ( byteToWrite & 1 << 15 - i ) > 0; + Serial.print( bitToWrite ); + + //Write Data + digitalWrite(dataIn, bitToWrite); + delayMicroseconds(10); + + //Write clock + digitalWrite(clk, HIGH); + + //Wait and go low + delayMicroseconds(100); + digitalWrite(clk, LOW); + } + digitalWrite(cs, HIGH); + Serial.println(); +} diff --git a/sprites.h b/sprites.h new file mode 100644 index 0000000..5aceee4 --- /dev/null +++ b/sprites.h @@ -0,0 +1,62 @@ +#ifndef sprites +#define sprites + +struct Sprite { + uint8_t buff[8]; + uint8_t width; + uint8_t height; +}; + +const Sprite smiley = { + { + 0b11001100, + 0b11001100, + 0b00000000, + 0b00110000, + 0b10000000, + 0b11111100, + 0b00000000, + 0b00000000 + }, 6, 6 +}; + +const Sprite checker = { + { + 0b10101010, + 0b01010101, + 0b10101010, + 0b01010101, + 0b10101010, + 0b01010101, + 0b10101010, + 0b01010101 + }, 8, 8 +}; + +const Sprite ball = { + { + 0b11100000, + 0b10100000, + 0b11100000, + 0b00000000, + 0b00000000, + 0b00000000, + 0b00000000, + 0b00000000 + }, 3, 3 +}; + +const Sprite miniSmiley = { + { + 0b01010000, + 0b10000000, + 0b11110000, + 0b00000000, + 0b00000000, + 0b00000000, + 0b00000000, + 0b00000000 + }, 4, 3 +}; + +#endif diff --git a/tags b/tags new file mode 100644 index 0000000..458c10e --- /dev/null +++ b/tags @@ -0,0 +1,27 @@ +!_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/ +!_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/ +!_TAG_PROGRAM_AUTHOR Darren Hiebert /dhiebert@users.sourceforge.net/ +!_TAG_PROGRAM_NAME Exuberant Ctags // +!_TAG_PROGRAM_URL http://ctags.sourceforge.net /official site/ +!_TAG_PROGRAM_VERSION 5.8 // +MaxCommands MaxCommands.h 2;" d +Sprite sprites.h /^struct Sprite {$/;" s +buff sprites.h /^ uint8_t buff[8];$/;" m struct:Sprite +checker sprites.h /^const Sprite checker = {$/;" v +height sprites.h /^ uint8_t height;$/;" m struct:Sprite +maxDECODE_MODE MaxCommands.h 15;" d +maxDIGIT_0 MaxCommands.h 7;" d +maxDIGIT_1 MaxCommands.h 8;" d +maxDIGIT_2 MaxCommands.h 9;" d +maxDIGIT_3 MaxCommands.h 10;" d +maxDIGIT_4 MaxCommands.h 11;" d +maxDIGIT_5 MaxCommands.h 12;" d +maxDIGIT_6 MaxCommands.h 13;" d +maxDIGIT_7 MaxCommands.h 14;" d +maxINTENSITY MaxCommands.h 6;" d +maxSCAN_LIMIT MaxCommands.h 16;" d +maxSHUTDOWN_INV MaxCommands.h 4;" d +maxTEST MaxCommands.h 5;" d +smiley sprites.h /^const Sprite smiley = { $/;" v +sprites sprites.h 2;" d +width sprites.h /^ uint8_t width;$/;" m struct:Sprite -- cgit v1.2.3