Created
November 11, 2014 15:31
-
-
Save electricimp/3dfd80636a2c7022f2f1 to your computer and use it in GitHub Desktop.
Squirrel Class to control Adafruit HT16K33-based 1.2in, 4-digit, 7-segment LED via I2C
This file contains 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
class HT16K33BIG | |
{ | |
// Squirrel class for 1.2-inch four-digit, 7-segment LED displays driven by the HT16K33 controller | |
// For example: http://www.adafruit.com/products/1270 | |
// Communicates with any imp I2C bus | |
// Written by Tony Smith (@smittytone) October 2014 | |
// Version 1.0 | |
// HT16K33 registers and HT16K33-specific variables | |
HT16K33_REGISTER_DISPLAY_ON = "\x81" | |
HT16K33_REGISTER_DISPLAY_OFF = "\x80" | |
HT16K33_REGISTER_SYSTEM_ON = "\x21" | |
HT16K33_REGISTER_SYSTEM_OFF = "\x20" | |
HT16K33_DISPLAY_ADDRESS = "\x00" | |
HT16K33_I2C_ADDRESS = 0x70 | |
HT16K33_BLANK_CHAR = 16 | |
HT16K33_MINUS_CHAR = 17 | |
HT16K33_CHAR_COUNT = 17 | |
// Class properties; null for those defined in the Constructor | |
_buffer = null | |
_digits = null | |
_led = null | |
_ledAddress = 0 | |
constructor(bus, address = 0x70) | |
{ | |
// The parameter is whichever imp I2C bus is to be used for the HT16K33 | |
_led = bus | |
_ledAddress = address << 1 | |
// Buffer stores the character matrix values for each row of the display | |
_buffer = [0x00, 0x00, 0x00, 0x00, 0x00] | |
// digits store character matrices for 0-9, A-F, blank and minus | |
_digits = [ | |
0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, | |
0x5F, 0x7C, 0x58, 0x5E, 0x7B, 0x71, | |
0x00, 0x40 | |
] | |
} | |
function init(clearChar = 16, brightness = 15) | |
{ | |
// Parameters: | |
// 1. Integer index for the digits[] character matrix to zero the display to | |
// 2. Integer value for the display brightness, between 0 and 15 | |
// 3. Boolean value - should the colon (digits[2]) be shown? | |
// Configure the I2C bus | |
_led.configure(CLOCK_SPEED_100_KHZ) | |
// Set the brightness (which of necessity wipes and power cyles the dispay) | |
setBrightness(brightness) | |
// Clear the screen to the chosen character | |
setColon(0x00) | |
clearBuffer(clearChar) | |
updateDisplay() | |
} | |
function clearBuffer(clearChar = 16) | |
{ | |
// Fills the buffer with a blank character, or the digits[] character matrix whose index is provided | |
if (clearChar < 0 || clearChar > HT16K33_CHAR_COUNT) clearChar = HT16K33_BLANK_CHAR; | |
// Put the clear_character into the buffer except row 2 (colon row) | |
_buffer[0] = _digits[clearChar] | |
_buffer[1] = _digits[clearChar] | |
_buffer[3] = _digits[clearChar] | |
_buffer[4] = _digits[clearChar] | |
} | |
function setColon(bitVal) | |
{ | |
// Sets the colon (row 2) to the required pattern; also sets the initial | |
// colon and the raised decimal point at row 3 | |
// 0x02 - centre colon | |
// 0x04 - left colon, lower dot | |
// 0x08 - left colon, upper dot | |
// 0x10 - decimal point (upper) | |
_buffer[2] = bitVal | |
} | |
function writeChar(rowNum = 0, charVal = 0x7F) | |
{ | |
// Puts the input character matrix (an 8-bit integer) into the specified row, | |
// adding a decimal point if required. Character matrix value is calculated by | |
// setting the bit(s) representing the segment(s) you want illuminated. | |
// Bit-to-segment mapping runs clockwise from the top around the outside of the | |
// matrix; the inner segment is bit 6: | |
// | |
// 0 | |
// _ | |
// 5 | | 1 | |
// | | | |
// - <----- 6 | |
// 4 | | 2 | |
// | _ | | |
// 3 | |
// | |
if (rowNum < 0 || rowNum > 4) return | |
_buffer[rowNum] = charVal | |
} | |
function writeNumber(rowNum = 0, intVal = 0, hasDot = false) | |
{ | |
// Puts the number - ie. index of _digits[] - into the specified row, | |
// adding a decimal point if required | |
if (rowNum < 0 || rowNum > 4) return | |
if (hasDot) | |
{ | |
_buffer[rowNum] = _digits[intVal] | 0x80 | |
} | |
else | |
{ | |
_buffer[rowNum] = _digits[intVal] | |
} | |
} | |
function updateDisplay() | |
{ | |
// Converts the row-indexed buffer[] values into a single, combined | |
// string and writes it to the HT16K33 via I2C | |
local dataString = HT16K33_DISPLAY_ADDRESS | |
for (local i = 0 ; i < 5 ; i++) | |
{ | |
dataString = dataString + _buffer[i].tochar() + "\x00" | |
} | |
// Write the combined datastring to I2C | |
_led.write(_ledAddress, dataString) | |
} | |
function setBrightness(brightness = 15) | |
{ | |
// This function is called when the app changes the clock's brightness | |
// Default: 15 | |
if (brightness > 15) brightness = 15 | |
if (brightness < 0) brightness = 0 | |
brightness = brightness + 224 | |
// Preserve the current _buffer contents | |
local sbuffer = [0,0,0,0,0] | |
for (local i = 0, i < 5 ; i++) | |
{ | |
sbuffer[i] = _buffer[i] | |
} | |
clearBuffer(16) | |
updateDisplay() | |
// Power cycle the display | |
powerDown() | |
powerUp() | |
// Write the new brightness value to the HT16K33 | |
_led.write(_ledAddress, brightness.tochar() + "\x00") | |
// Restore the current _buffer contents | |
for (local i = 0, i < 5 ; i++) | |
{ | |
_buffer[i] = sbuffer[i] | |
} | |
updateDisplay() | |
} | |
function powerDown() | |
{ | |
_led.write(_ledAddress, HT16K33_REGISTER_DISPLAY_OFF); | |
_led.write(_ledAddress, HT16K33_REGISTER_SYSTEM_OFF); | |
} | |
function powerUp() | |
{ | |
_led.write(_ledAddress, HT16K33_REGISTER_SYSTEM_ON); | |
_led.write(_ledAddress, HT16K33_REGISTER_DISPLAY_ON); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment