Skip to content

Commit

Permalink
Initial checkin (existing code)
Browse files Browse the repository at this point in the history
  • Loading branch information
kjhughes097 committed Jul 15, 2016
1 parent 017db9b commit b0d5595
Show file tree
Hide file tree
Showing 6 changed files with 415 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
################################################################################
# This .gitignore file was automatically created by Microsoft(R) Visual Studio.
################################################################################

/.vscode
69 changes: 69 additions & 0 deletions charmap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
int fontData[][6] = {
{2,0x00,0x00}, // SPACE
{1,0x5E}, // !
{3,0x02,0x00,0x02}, // "
{5,0x24,0x7E,0x24,0x7E,0x24}, // #
{5,0x44,0x4A,0x7E,0x52,0x22}, // $
{5,0x22,0x10,0x08,0x04,0x42}, // %
{5,0x34,0x4A,0x54,0x20,0x40}, // &
{1,0x02}, // '
{3,0x18,0x24,0x42}, // (
{3,0x42,0x24,0x18}, // )
{5,0x54,0x38,0x7C,0x38,0x54}, // *
{5,0x08,0x08,0x7E,0x08,0x08}, // +
{2,0x40,0x20}, // ,
{3,0x08,0x08,0x08}, // -
{1,0x40}, // .
{4,0x60,0x10,0x08,0x06}, // /
{4,0x7E,0x52,0x4A,0x7E}, // 0
{3,0x44,0x7E,0x40}, // 1
{4,0x64,0x52,0x4A,0x44}, // 2
{3,0x42,0x4A,0x34}, // 3
{4,0x18,0x14,0x7E,0x10}, // 4
{3,0x4E,0x4A,0x32}, // 5
{4,0x3C,0x4A,0x4A,0x32}, // 6
{3,0x6E,0x1A,0x06}, // 7
{4,0x34,0x4A,0x4A,0x34}, // 8
{4,0x4C,0x52,0x56,0x3C}, // 9
{1,0x14}, // :
{2,0x20,0x14}, // ;
{3,0x10,0x28,0x44}, // <
{3,0x28,0x28,0x28}, // =
{3,0x44,0x28,0x10}, // >
{3,0x02,0x5A,0x04}, // ?
{5,0x7E,0x42,0x5A,0x52,0x5E}, // @
{4,0x7E,0x12,0x12,0x7e}, //A
{4,0x7E,0x4a,0x4a,0x34}, //B
{4,0x7e,0x42,0x42,0x42}, //C
{4,0x7E,0x42,0x42,0x3C}, //D
{4,0x7E,0x52,0x52,0x42}, //E
{4,0x7E,0x12,0x12,0x02}, //F
{4,0x7E,0x42,0x42,0x72}, //G
{4,0x7E,0x10,0x10,0x7E}, //H
{4,0x42,0x7E,0x42,0x00}, //I
{4,0x62,0x42,0x7E,0x02}, //J
{4,0x7E,0x18,0x24,0x42}, //K
{4,0x7E,0x40,0x40,0x40}, //L
{4,0x7E,0x04,0x04,0x7E}, //M
{4,0x7E,0x0C,0x30,0x7E}, //N
{4,0x7E,0x42,0x42,0x7E}, //O
{4,0x7E,0x12,0x12,0x1E}, //P
{4,0x7E,0x42,0x4A,0x7E}, //Q
{4,0x7E,0x12,0x32,0x6E}, //R
{4,0x4E,0x4A,0x4A,0x72}, //S
{4,0x02,0x7E,0x02,0x02}, //T
{4,0x7E,0x40,0x40,0x7E}, //U
{4,0x3E,0x60,0x60,0x3E}, //V
{4,0x7E,0x20,0x20,0x7E}, //W
{4,0x66,0x3C,0x3C,0x66}, //X
{4,0x1E,0x10,0x10,0x7E}, //Y
{4,0x62,0x52,0x4C,0x46}, //Z
{2,0x7E,0x42}, // [
{4,0x06,0x08,0x10,0x60}, // BACKSPACE
{2,0x42,0x7E}, // ]
{3,0x04,0x02,0x04}, // ^
{3,0x40,0x40,0x40}, // _
{2,0x02,0x04}, // `
{3,0x7E,0x7E,0x7E} // UNKNOWN OR NOT SUPPORTED

};
6 changes: 6 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
all:
gcc oled.cpp ssd1306.cpp -o oled -std=c++11

clean:
rm -f oled

53 changes: 53 additions & 0 deletions oled.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "ssd1306.h"



int main( int argc, char * argv [] ) {

SSD1306 myDisplay;
myDisplay.initDisplay();

for (int i = 1; i < argc; i++) {
if(strcmp(argv[i], "-i")==0) {
myDisplay.initDisplay();
}
if(strcmp(argv[i], "-c")==0) {
myDisplay.clearDisplay();
}
if(strcmp(argv[i], "-ww")==0) {
myDisplay.setWordWrap(TRUE);
}
if(strcmp(argv[i], "-dw")==0) {
myDisplay.setDisplayMode(SSD1306::Mode::WRAP);
}
if(strcmp(argv[i], "-t")==0) {
if((i + 1) < argc)
{
myDisplay.textDisplay(argv[i+1]);
i++;
}
}
}

/*
SSD1306 myDisplay;
myDisplay.setDisplayMode(SSD1306::Mode::SCROLL);
myDisplay.setWordWrap(TRUE);
myDisplay.textDisplay("THIS IS THE FIRST LINE.");
myDisplay.textDisplay("LONG WRAP THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG LONG WRAP THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG LONG WRAP THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG");
myDisplay.textDisplay("PUNCTUATION !\"#$%&'()*+,-./:;<=>?@[\\]^_`");
myDisplay.textDisplay("NUMBERS 0123456789");
myDisplay.setWordWrap(FALSE);
myDisplay.textDisplay("LONG NO WRAP THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG");
myDisplay.textDisplay("THIS IS THE LAST LINE.");
myDisplay.textDisplay("DISPLAY WRAP.");
//myDisplay.setDisplayMode(SSD1306::Mode::SCROLL);
for(int i=0; i<20; i++)
{
char buffer [10];
sprintf (buffer, "LINE %c", i + 64);
myDisplay.textDisplay(buffer);
}
*/
}
214 changes: 214 additions & 0 deletions ssd1306.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
#include <ctype.h>
#include "charmap.h"
#include "ssd1306.h"

#ifdef DEBUG
#define D if(1)
#else
#define D if(0)
#endif

SSD1306::SSD1306() {
for(int i=0; i<8; i++)
{
for(int j=0; j<128; j++)
{
displayLines[i][j] = 0;
}
}
}

void SSD1306::initDisplay() {
D printf("initDisplay\n");
writeI2C(initSequence, 26);
i2cInitialised = TRUE;
}

void SSD1306::textDisplay(const char *message) {
if(!i2cInitialised)
{
initDisplay();
}

// and scroll up if needed (in SCROLL mode)
if((displayMode == SSD1306::Mode::SCROLL) && needScroll)
{
scrollUp(1);
}

D printf("textDisplay - %s\n", message);
int currByteCount = 0;
for(int i=0; i<strlen(message); i++)
{
int bytesAdded = addFontBytes(currByteCount, message[i]);
if(bytesAdded > 0)
{
currByteCount += bytesAdded;
}
else
{
if(wordWrap)
{
// fill the rest of this line with 0s
while(currByteCount < 128)
{
displayLines[currentLine][currByteCount++] = 0x00;
}
// move it onto the next line
currentLine++;
// and scroll up if needed (in SCROLL mode)
if((displayMode == SSD1306::Mode::SCROLL) && needScroll)
{
scrollUp(1);
}
if (currentLine > 7)
{
currentLine = 0;
needScroll = TRUE;
}
currByteCount=0;
i--; // move back one so we try it again
}
}
}

// now fill up any left over with 0x00s on the current line
while(currByteCount < 128)
{
displayLines[currentLine][currByteCount++] = 0x00;
}

// now point to next line and set to first char
currByteCount = 0;
currentLine++;
if(currentLine > 7)
{
currentLine = 0;
needScroll = TRUE;
}

updateDisplayFull();
}

void SSD1306::clearDisplay() {
// blank out the line buffers
for(int i=0; i<8; i++)
{
for(int j=0; j<128; j++)
{
displayLines[i][j] = 0;
}
}
//reset the scroll pointer and line pointer
currentScrollLine = 0;
currentLine = 0;
// write it out
updateDisplayFull();
}

void SSD1306::setWordWrap(int b) {
wordWrap = b;
}

void SSD1306::setDisplayMode(SSD1306::Mode mode) {
displayMode = mode;
}

// private functions
void SSD1306::scrollUp(int lines) {
if(!i2cInitialised)
{
initDisplay();
}
currentScrollLine += lines;
if (currentScrollLine > 7) { currentScrollLine -= 8; }
scrollUpSequence[2] = currentScrollLine * 8;
D printf("scrollUp\n");
writeI2C(scrollUpSequence, 3);
}

int SSD1306::addFontBytes(int curr, unsigned char c) {
D printf("addFontBytes - %i - ", c);
c = toupper(c); // we only support UPPERCASE letters
int letterIdx = (c - ' ');
if(letterIdx > 64)
{
letterIdx = 65;
}

int letterBytes = fontData[letterIdx][0];
if((curr + letterBytes + 1) > 127 )
{
return 0;
D printf("\n");
}
else
{
for(int i=0; i<letterBytes; i++)
{
D printf("%x ", fontData[letterIdx][1 + i]);
displayLines[currentLine][curr + i] = fontData[letterIdx][1 + i];
}
displayLines[currentLine][curr + letterBytes++] = 0x00; // single byte space / seperator
D printf("\n");
return letterBytes;
}
}

void SSD1306::setDisplayRange(int line = -1) {
// -1 = full range
// 0..7 = line
D printf("setDisplayRange (7 bytes)\n");
if(line == -1)
{
writeI2C(setFullRange, 7);
}
}

void SSD1306::updateDisplayFull() {
setDisplayRange(-1);
for(int line=0; line<8; line++)
{
for(int chunk=0; chunk< 8; chunk++)
{
unsigned char buffer[17] = {0};
buffer[0] = 0x40;
for(int i=0; i<16;i++)
{
D printf("[%i]=%x ", (i+1), displayLines[line][(chunk * 16) + i]);
buffer[i+1] = displayLines[line][(chunk * 16) + i];
}
D printf("\n");
writeI2C(buffer, 17);
}
}
}

void SSD1306::writeI2C(unsigned char* data, int bytes) {
char *deviceName = (char*)"/dev/i2c-1";
if ((i2cHandle = open(deviceName, O_RDWR)) < 0)
{
printf("error opening I2C\n");
}
else
{
D printf("Opened I2C bus (Handle = %i)\n", i2cHandle);
if (ioctl(i2cHandle, I2C_SLAVE, i2cAddress) < 0)
{
printf("Error at ioctl\n");
}
else
{
D printf("writeI2C : ");
for(int i=0; i<bytes; i++) { D printf("%x ", data[i]); }
write(i2cHandle, data, bytes);
D printf("\n");
}

// Close the i2c device bus
char *deviceName = (char*)"dev/i2c-1";
close(*deviceName);
}

}

Loading

0 comments on commit b0d5595

Please sign in to comment.