Last active
May 4, 2016 12:50
-
-
Save daniel-j/2e58c8d90e43e3624404d04a6e478de8 to your computer and use it in GitHub Desktop.
Pimoroni Displayotron Hat C++ API DRAFT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <wiringPi.h> | |
#include "St7036.h" | |
#include "Sn3218.h" | |
St7036 lcd; | |
Sn3218 backlight; | |
int main() { | |
// setup | |
wiringPiSetup(); | |
lcd.begin(); | |
int heart[] = { | |
0b00000, | |
0b01010, | |
0b11111, | |
0b11111, | |
0b11111, | |
0b01110, | |
0b00100, | |
0b00000 | |
}; | |
lcd.createChar(0, heart); | |
lcd.home(); | |
backlight.begin(); | |
backlight.enableLeds(Sn3218::CH_ALL); | |
// main program | |
// you only need to specify length (32) if you have character 0 in the string | |
lcd.writeString("Hello World! \x00 \x00 \x00 \x00 \x00 \x00 C++!", 32); | |
int brightness = 30; | |
for(int i = 0; i < 6; i++) { | |
backlight.set(i*3+0, (255*brightness)/255); | |
backlight.set(i*3+1, ( 0*brightness)/255); | |
backlight.set(i*3+2, (255*brightness)/255); | |
} | |
backlight.update(); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CXX = g++ | |
CFLAGS = -Wall -g -std=c++11 | |
#CFLAGS += -O3 | |
LIBS = -lwiringPi | |
CORE = dothat | |
OBJS = main.o St7036.o Sn3218.o | |
all: $(CORE) | |
$(CORE): $(OBJS) Makefile | |
$(CXX) $(CFLAGS) -o $(CORE) $(OBJS) $(LIBS) | |
clean: | |
rm -f $(CORE) | |
rm -f *.o | |
%.o: %.cpp | |
$(CXX) -c $(CFLAGS) $< -o $@ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Sn3218.h" | |
#include <unistd.h> // write() | |
#include <wiringPiI2C.h> | |
void Sn3218::begin() { | |
_fdDev = wiringPiI2CSetup(DEV_ADDR); | |
enable(); | |
} | |
void Sn3218::update() { | |
this->writeReg(CMD_UPDATE, 0x00); | |
} | |
void Sn3218::reset() { | |
writeReg(CMD_RESET, 0x00); | |
enable(); | |
} | |
void Sn3218::enable() { | |
writeReg(CMD_ENABLE_OUTPUT, 0x01); | |
} | |
void Sn3218::disable() { | |
writeReg(CMD_ENABLE_OUTPUT, 0x00); | |
} | |
void Sn3218::enableLeds(unsigned long enable_mask) { | |
writeReg(CMD_ENABLE_LEDS, enable_mask & 0x3F); | |
writeReg(CMD_ENABLE_LEDS + 1, (enable_mask >> 6) & 0x3F); | |
writeReg(CMD_ENABLE_LEDS + 2, (enable_mask >> 12) & 0x3F); | |
update(); | |
} | |
void Sn3218::set(unsigned char chan, unsigned char val) { | |
writeReg(CMD_SET_PWM_VALUES + chan, val); | |
} | |
void Sn3218::writeReg(unsigned char reg, unsigned char val) { | |
uint8_t buffer[2]; | |
buffer[0] = reg; | |
buffer[1] = val; | |
write(_fdDev, buffer, 2); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef SN3218_H | |
#define SN3218_H | |
#include <stdint.h> | |
class Sn3218 { | |
public: | |
void begin(); | |
void update(); | |
void enable(); | |
void enableLeds( unsigned long enable_mask ); | |
void disable(); | |
void reset(); | |
void set( unsigned char chan, unsigned char val ); | |
static const int NUM_CHANNELS = 18; | |
// use with enableLeds() | |
static const int CH_ALL = 0x3FFFF; | |
static const int CH_00 = 0x00001; | |
static const int CH_01 = 0x00002; | |
static const int CH_02 = 0x00004; | |
static const int CH_03 = 0x00008; | |
static const int CH_04 = 0x00010; | |
static const int CH_05 = 0x00020; | |
static const int CH_06 = 0x00040; | |
static const int CH_07 = 0x00080; | |
static const int CH_08 = 0x00100; | |
static const int CH_09 = 0x00200; | |
static const int CH_10 = 0x00400; | |
static const int CH_11 = 0x00800; | |
static const int CH_12 = 0x01000; | |
static const int CH_13 = 0x02000; | |
static const int CH_14 = 0x04000; | |
static const int CH_15 = 0x08000; | |
static const int CH_16 = 0x10000; | |
static const int CH_17 = 0x20000; | |
private: | |
int _fdDev; | |
void writeReg( unsigned char reg, unsigned char val ); | |
static const int DEV_ADDR = 0x54; | |
static const int CMD_ENABLE_OUTPUT = 0x00; | |
static const int CMD_SET_PWM_VALUES = 0x01; | |
static const int CMD_ENABLE_LEDS = 0x13; | |
static const int CMD_UPDATE = 0x16; | |
static const int CMD_RESET = 0x17; | |
}; | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "St7036.h" | |
#include <cstring> | |
#include <unistd.h> // read()/write() | |
#include <wiringPi.h> | |
#include <wiringPiSPI.h> | |
St7036::St7036(int register_select_pin, int reset_pin, int rows, int columns, int spi_chip_select) { | |
_resetPin = reset_pin; | |
_regPin = register_select_pin; | |
_rows = rows; | |
_columns = columns; | |
_cs = spi_chip_select; | |
} | |
void St7036::begin() { | |
_fdSpi = wiringPiSPISetup(_cs, 1000000); | |
pinMode(_resetPin, OUTPUT); | |
pinMode(_regPin, OUTPUT); | |
reset(); | |
digitalWrite(_regPin, HIGH); | |
updateDisplayMode(); | |
// set entry mode (no shift, cursor direction) | |
writeCommand(0b00000100 | 0b00000010); | |
// ??? | |
setBias(1); | |
setContrast(40); | |
clear(); | |
} | |
void St7036::reset() { | |
digitalWrite(_resetPin, LOW); | |
delay(1); | |
digitalWrite(_resetPin, HIGH); | |
delay(1); | |
} | |
void St7036::writeInstructionSet(int instruction_set) { | |
digitalWrite(_regPin, LOW); | |
writeByte(INSTRUCTION_SET_TEMPLATE | instruction_set | (_doubleHeight << 2)); | |
delay(1); | |
} | |
void St7036::writeByte(uint8_t b) { | |
uint8_t buf[1]; | |
buf[0] = b; | |
//wiringPiSPIDataRW(_cs, buf, 1); | |
write(_fdSpi, buf, 1); | |
} | |
void St7036::writeCommand(uint8_t value, int instruction_set) { | |
digitalWrite(_regPin, LOW); | |
writeInstructionSet(instruction_set); | |
writeByte(value); | |
delay(1); | |
} | |
void St7036::updateDisplayMode() { | |
int mask = COMMAND_SET_DISPLAY_MODE; | |
mask |= DISPLAY_ON * _enabled; | |
mask |= CURSOR_ON * _cursorEnabled; | |
mask |= BLINK_ON * _cursorBlink; | |
writeCommand(mask); | |
} | |
void St7036::setCursorOffset(int offset) { | |
writeCommand(0b10000000 | offset); | |
} | |
void St7036::setCursorPosition(int column, int row) { | |
int offset = _columns * row + column; | |
writeCommand(0b10000000 | offset); | |
} | |
void St7036::home() { | |
setCursorPosition(0, 0); | |
} | |
void St7036::clear() { | |
writeCommand(COMMAND_CLEAR); | |
home(); | |
} | |
void St7036::setBias(int bias) { | |
writeCommand(COMMAND_BIAS | (bias << 4) | 1, 1); | |
} | |
void St7036::setContrast(int contrast) { | |
if (contrast < 0 || contrast > 0x3F) { | |
return; | |
} | |
// For 3.3v operation the booster must be on, which is | |
// on the same command as the (2-bit) high-nibble of contrast | |
writeCommand((0b01010100 | ((contrast >> 4) & 0x03)), 1); | |
writeCommand(0b01101011, 1); | |
// Set low-nibble of the contrast | |
writeCommand((0b01110000 | (contrast & 0x0F)), 1); | |
} | |
void St7036::enableCursor(bool cursor) { | |
_cursorEnabled = cursor; | |
updateDisplayMode(); | |
} | |
void St7036::enableBlink(bool blink) { | |
_cursorBlink = blink; | |
updateDisplayMode(); | |
} | |
void St7036::writeString(const char* str, int length) { | |
digitalWrite(_regPin, HIGH); | |
length = length <= 0? strlen(str) : length; | |
for (int i = 0; i < length; i++){ | |
writeByte(str[i]); | |
} | |
delay(1); | |
} | |
void St7036::writeChar(int value) { | |
digitalWrite(_regPin, HIGH); | |
writeByte(value); | |
} | |
void St7036::createChar(int charPos, int charMap[]) { | |
if (charPos < 0 || charPos > 7) { | |
return; | |
} | |
int baseAddress = charPos*8; | |
for (int i = 0; i < 8; i++) { | |
writeCommand(0x40 | (baseAddress+i)); | |
writeChar(charMap[i]); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef ST7036_H | |
#define ST7036_H | |
#include <stdint.h> | |
class St7036 { | |
public: | |
St7036(int register_select_pin = 6, int reset_pin = 26, int rows = 3, int columns = 16, int spi_chip_select=0); | |
void begin(); | |
void reset(); | |
void clear(); | |
void home(); | |
void setContrast(int contrast); | |
void setCursorOffset(int offset); | |
void setCursorPosition(int column, int row); | |
void enableCursor(bool cursor); | |
void enableBlink(bool blink); | |
void writeString(const char* str, int length = 0); | |
void createChar(int charPos, int charMap[]); | |
private: | |
int _resetPin; | |
int _regPin; | |
int _rows; | |
int _columns; | |
int _cs; | |
int _fdSpi; | |
bool _enabled = 1; | |
bool _cursorEnabled = 0; | |
bool _cursorBlink = 0; | |
bool _doubleHeight = 0; | |
void writeByte(uint8_t b); | |
void writeInstructionSet(int instruction_set = 0); | |
void writeCommand(uint8_t value, int instruction_set = 0); | |
void updateDisplayMode(); | |
void setBias(int bias=1); | |
void writeChar(int value); | |
static const int INSTRUCTION_SET_TEMPLATE = 0b00111000; | |
// commands | |
static const int COMMAND_CLEAR = 0b00000001; | |
static const int COMMAND_HOME = 0b00000010; | |
static const int COMMAND_SCROLL = 0b00010000; | |
static const int COMMAND_DOUBLE = 0b00010000; | |
static const int COMMAND_BIAS = 0b00010100; | |
static const int COMMAND_SET_DISPLAY_MODE = 0b00001000; | |
// display modes | |
static const int BLINK_ON = 0b00000001; | |
static const int CURSOR_ON = 0b00000010; | |
static const int DISPLAY_ON = 0b00000100; | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment