Skip to content

Instantly share code, notes, and snippets.

@smittytone
Last active August 2, 2019 12:54
Show Gist options
  • Save smittytone/9393674 to your computer and use it in GitHub Desktop.
Save smittytone/9393674 to your computer and use it in GitHub Desktop.
A Squirrel program to display characters and graphics on an 8 x 8 LED matrix connect to an Imp device.
// Matrix LED character set display by Tony Smith
// Based on code from LinkSprite:
// [http://linksprite.com/wiki/index.php5?title=LED_Matrix_Kit]
// Additional code copyright © Electric Imp, 2014
// Maxim Integrated Max7219 Datasheet:
// [http://www.maximintegrated.com/datasheet/index.mvp/id/1339]
// Set the number of available characters in the character set
const alpha_count = 95;
// Set up recognisable aliases for the three Imp GPIO pins
led_clk <- hardware.pin9;
led_cs <- hardware.pin8;
led_din <- hardware.pin7;
// Set these pins as digital outputs
led_clk.configure(DIGITAL_OUT);
led_cs.configure(DIGITAL_OUT);
led_din.configure(DIGITAL_OUT);
// Establish an array of 8 x 8 character matrices
// Have fun making up your own characters!
charset <- [
[0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0], // Space - Ascii 32
[0x0,0x10,0x10,0x10,0x10,0x0,0x10,0x0], // !
[0x0,0x24,0x24,0x0,0x0,0x0,0x0,0x0], // ”
[0x0,0x24,0x7E,0x24,0x24,0x7E,0x24,0x0], // #
[0x0,0x8,0x3E,0x28,0x3E,0xA,0x3E,0x8], // $
[0x0,0x62,0x64,0x8,0x10,0x26,0x46,0x0], // %
[0x0,0x10,0x28,0x10,0x2A,0x44,0x3A,0x0], // &
[0x0,0x8,0x10,0x0,0x0,0x0,0x0,0x0], // ‘
[0x0,0x4,0x8,0x8,0x8,0x8,0x4,0x0], // (
[0x0,0x20,0x10,0x10,0x10,0x10,0x20,0x0], // )
[0x0,0x0,0x14,0x8,0x3E,0x8,0x14,0x0], // *
[0x0,0x0,0x8,0x8,0x3E,0x8,0x8,0x0], // +
[0x0,0x0,0x0,0x0,0x0,0x8,0x8,0x10], // ,
[0x0,0x0,0x0,0x0,0x3E,0x0,0x0,0x0], // -
[0x0,0x0,0x0,0x0,0x0,0x18,0x18,0x0], // .
[0x0,0x2,0x4,0x8,0x10,0x20,0x40,0x0], // /
[0x0,0x3C,0x46,0x4A,0x52,0x62,0x3C,0x0], // 0 - Ascii 48
[0x0,0x30,0x50,0x10,0x10,0x10,0x7C,0x0], // 1
[0x0,0x3C,0x42,0x2,0x3C,0x40,0x7E,0x0], // 2
[0x0,0x3C,0x42,0xC,0x2,0x42,0x3C,0x0], // 3
[0x0,0x8,0x18,0x28,0x48,0x7E,0x8,0x0], // 4
[0x0,0x7E,0x40,0x7C,0x2,0x42,0x3C,0x0], // 5
[0x0,0x3C,0x40,0x7C,0x42,0x42,0x3C,0x0], // 6
[0x0,0x7E,0x2,0x4,0x8,0x10,0x10,0x0], // 7
[0x0,0x3C,0x42,0x3C,0x42,0x42,0x3C,0x0], // 8
[0x0,0x3C,0x42,0x42,0x3E,0x2,0x3C,0x0], // 9
[0x0,0x0,0x10,0x0,0x0,0x0,0x10,0x0], // : - Ascii 58
[0x0,0x0,0x10,0x0,0x0,0x10,0x10,0x20], // ;
[0x0,0x0,0x4,0x8,0x10,0x8,0x4,0x0], // <
[0x0,0x0,0x0,0x3E,0x0,0x3E,0x0,0x0], // =
[0x0,0x0,0x10,0x8,0x4,0x8,0x10,0x0], // >
[0x0,0x3C,0x42,0x4,0x8,0x0,0x8,0x0], // ?
[0x0,0x3C,0x4A,0x56,0x5E,0x40,0x3C,0x0], // @
[0x0,0x3C,0x42,0x42,0x7E,0x42,0x42,0x0], // A - Ascii 65
[0x0,0x7C,0x42,0x7C,0x42,0x42,0x7C,0x0], // B
[0x0,0x3C,0x42,0x40,0x40,0x42,0x3C,0x0], // C
[0x0,0x78,0x44,0x42,0x42,0x44,0x78,0x0], // D
[0x0,0x7E,0x40,0x7C,0x40,0x40,0x7E,0x0], // E
[0x0,0x7E,0x40,0x7C,0x40,0x40,0x40,0x0], // F
[0x0,0x3C,0x42,0x40,0x4E,0x42,0x3C,0x0], // G
[0x0,0x42,0x42,0x7E,0x42,0x42,0x42,0x0], // H
[0x0,0x7C,0x10,0x10,0x10,0x10,0x7C,0x0], // I
[0x0,0x2,0x2,0x2,0x2,0x42,0x3C,0x0], // J
[0x0,0x44,0x48,0x70,0x48,0x44,0x42,0x0], // K
[0x0,0x40,0x40,0x40,0x40,0x40,0x7E,0x0], // L
[0x0,0x42,0x66,0x5A,0x42,0x42,0x42,0x0], // M
[0x0,0x42,0x62,0x52,0x4A,0x46,0x42,0x0], // N
[0x0,0x3C,0x42,0x42,0x42,0x42,0x3C,0x0], // O
[0x0,0x7C,0x42,0x42,0x7C,0x40,0x40,0x0], // P
[0x0,0x3C,0x42,0x42,0x52,0x4A,0x3C,0x0], // Q
[0x0,0x7C,0x42,0x42,0x7C,0x44,0x42,0x0], // R
[0x0,0x3C,0x40,0x3C,0x2,0x42,0x3C,0x0], // S
[0x0,0x7C,0x10,0x10,0x10,0x10,0x10,0x0], // T
[0x0,0x42,0x42,0x42,0x42,0x42,0x3C,0x0], // U
[0x0,0x42,0x42,0x42,0x42,0x24,0x18,0x0], // V
[0x0,0x42,0x42,0x42,0x42,0x5A,0x24,0x0], // W
[0x0,0x42,0x24,0x18,0x18,0x24,0x42,0x0], // X
[0x0,0x44,0x28,0x10,0x10,0x10,0x10,0x0], // Y
[0x0,0x7E,0x4,0x8,0x10,0x20,0x7E,0x0], // Z - Ascii 90
[0x0,0xE,0x8,0x8,0x8,0x8,0xE,0x0], // [
[0x0,0x0,0x40,0x20,0x10,0x8,0x4,0x0], // \
[0x0,0x70,0x10,0x10,0x10,0x10,0x70,0x0], // ]
[0x0,0x10,0x38,0x54,0x10,0x10,0x10,0x0], // ^
[0x0,0x0,0x0,0x0,0x0,0x0,0x0,0xFF], // _
[0x0,0x1C,0x22,0x78,0x20,0x20,0x7E,0x0], // £
[0x0,0x0,0x38,0x4,0x3C,0x44,0x3C,0x0], // a - Ascii 97
[0x0,0x40,0x40,0x78,0x44,0x44,0x78,0x0], // b
[0x0,0x0,0x38,0x40,0x40,0x40,0x38,0x0], // c
[0x0,0x4,0x4,0x3C,0x44,0x44,0x3C,0x0], // d
[0x0,0x0,0x38,0x44,0x78,0x40,0x3C,0x0], // e
[0x0,0x30,0x40,0x60,0x40,0x40,0x40,0x0], // f
[0x0,0x3C,0x44,0x44,0x3C,0x4,0x38,0x0], // g
[0x0,0x40,0x40,0x40,0x78,0x44,0x44,0x0], // h
[0x0,0x20,0x0,0x60,0x20,0x20,0x70,0x0], // i
[0x0,0x8,0x0,0x8,0x8,0x48,0x30,0x0], // j
[0x0,0x40,0x50,0x60,0x60,0x50,0x48,0x0], // k
[0x0,0x40,0x40,0x40,0x40,0x40,0x30,0x0], // l
[0x0,0x0,0x68,0x54,0x54,0x54,0x54,0x0], // m
[0x0,0x0,0x78,0x44,0x44,0x44,0x44,0x0], // n
[0x0,0x0,0x38,0x44,0x44,0x44,0x38,0x0], // o
[0x0,0x78,0x44,0x44,0x78,0x40,0x40,0x0], // p
[0x0,0x3C,0x44,0x44,0x3C,0x4,0x6,0x0], // q
[0x0,0x0,0x1C,0x20,0x20,0x20,0x20,0x0], // r
[0x0,0x0,0x38,0x40,0x38,0x4,0x78,0x0], // s
[0x0,0x20,0x70,0x20,0x20,0x20,0x18,0x0], // t
[0x0,0x0,0x44,0x44,0x44,0x44,0x38,0x0], // u
[0x0,0x0,0x44,0x44,0x28,0x28,0x10,0x0], // v
[0x0,0x0,0x44,0x54,0x54,0x54,0x28,0x0], // w
[0x0,0x0,0x44,0x28,0x10,0x28,0x44,0x0], // x
[0x0,0x0,0x44,0x44,0x3C,0x4,0x38,0x0], // y
[0x0,0x0,0x7C,0x8,0x10,0x20,0x7C,0x0], // z - Ascii 122
[0x0,0xE,0x8,0x30,0x8,0x8,0xE,0x0], // {
[0x0,0x8,0x8,0x8,0x8,0x8,0x8,0x0], // |
[0x0,0x70,0x10,0xC,0x10,0x10,0x70,0x0], // }
[0x0,0x14,0x28,0x0,0x0,0x0,0x0,0x0], // ~
[0x3C,0x42,0x99,0xA1,0xA1,0x99,0x42,0x3C], // © - Ascii 127
];
// Define functions
function write_LED_byte(byte_value) {
// Writes a single byte of data to the MAX7219 display controller
// one bit at a time. Writes data to the LED in the MAX7219’s 16-bit
// serial format, which puts the register address in the first eight
// bits then the data in the second set of eight bits.
for (local i = 8 ; i > 0 ; i--) {
led_clk.write(0);
led_din.write(byte_value & 0x80); // Extract bit 8 and write it to LED
byte_value = byte_value << 1; // Shift DATA bits left by one bit
led_clk.write(1);
}
}
function write_LED(register_address, byte_value) {
// Writes a single value to the MAX7219,
// preceded by the register
// it is being written to
led_cs.write(0);
write_LED_byte(register_address);
write_LED_byte(byte_value);
led_cs.write(1);
}
function init_LED() {
// Initialise the MAX7219’s parameter registers by writing
// the register’s address followed by its 8-bit value
write_LED(0x09, 0x00); // Set MAX7219’s BCD decode mode to ‘none’
write_LED(0x0a, 0x00); // Set MAX7219’s LED intensity to a 2/32 duty cycle
write_LED(0x0b, 0x07); // Set MAX7219’s scan limit to all eight LED columns
write_LED(0x0c, 0x01); // Set MAX7219’s power mode. 0 = power down, 1 = normal
write_LED(0x0f, 0x00); // Set MAX7219’s display test mode off
// Nb. BCD = Binary Coded Decimal
}
function delay(value) {
// Delay for ‘value’ milliseconds
local a = hardware.millis() + value;
while (hardware.millis() < a) {
// NOP
}
}
function rotate_matrix(matrix_in) {
// Rotate the character matrix through -90 degrees
// Used if the LED matrix pins are connected directly to a breadboard
local i = 0;
local j = 0;
local a = 0;
local line_value = 0;
local matrix_out = [0,0,0,0,0,0,0,0];
for (i = 0 ; i < 8 ; i++) {
line_value = matrix_in[i];
for (j = 7 ; j > -1 ; j--) {
a = (line_value & math.pow(2, j));
if (a > 0) matrix_out[7-j] = matrix_out[7-j] + math.pow(2, i);
}
}
return matrix_out;
}
function display_charset() {
local i = 0;
local j = 0;
local matrix_out = [0,0,0,0,0,0,0,0];
// Run through each of the charset's characters, one by one
for (j = 0 ; j < alpha_count ; j++) {
// Rotate the character matrix to reflect the display orientation
matrix_out = rotate_matrix(charset[j]);
for (i = 1 ; i < 9 ; i++) {
// Write each line of the character 8 x 8 matrix
// to the appropriate MAX7219 line-display register, 1 to 8
write_LED(i, matrix_out[i-1]);
}
delay(1000); // 1s delay between characters
}
}
function loop() {
display_charset();
imp.wakeup(2.0, loop); // 2s delay between loop cycles
}
// Start point for the program
init_LED(); // Initialise the LED matrix
loop(); // Enter the program loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment