Skip to content

Instantly share code, notes, and snippets.

@nvbn
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nvbn/cdc14bd6adcb41639781 to your computer and use it in GitHub Desktop.
Save nvbn/cdc14bd6adcb41639781 to your computer and use it in GitHub Desktop.
stellaris led indicator
#define LEDS 32
#define ROWS 8
#define IMG 64
#define COLORS 3
#define BLACK 1
#define RED 2
#define GREEN 3
#define BLUE 5
const byte ledPins[LEDS + 1] = {1,
PB_5, PB_0, PD_0, PB_1, PD_1, PE_4, PD_2, PE_5,
PD_3, PB_4, PE_1, PA_5, PE_2, PA_6, PE_3, PA_7,
PD_7, PA_4, PD_6, PB_6, PC_7, PA_3, PC_6, PC_5,
PF_0, PC_4, PE_0, PB_3, PB_2, PF_3, PF_2, PF_1
};
const byte rows[ROWS] = {17, 18, 19, 20, 29, 30, 31, 32};
const byte reds[ROWS] = {9, 10, 11, 12, 13, 14, 15, 16};
const byte greens[ROWS] = {28, 27, 26, 25, 24, 23, 22, 21};
const byte blues[ROWS] = {1, 2, 3, 4, 5, 6, 7, 8};
char img[IMG];
void initLeds() {
for (int i = 1; i <= LEDS; i++) {
pinMode(ledPins[i], OUTPUT);
}
}
void on(byte num) {
digitalWrite(ledPins[num], HIGH);
}
void off(byte num) {
digitalWrite(ledPins[num], LOW);
}
void resetLeds() {
for (int i = 0; i < ROWS; i++) {
off(rows[i]);
on(reds[i]);
on(greens[i]);
on(blues[i]);
}
}
void resetImg(byte color) {
for (byte i = 0; i <= IMG; i++)
img[i] = color;
}
void drawImg() {
for (byte x = 0; x < ROWS; x++) {
resetLeds();
on(rows[x]);
for (byte y = 0; y < ROWS; y++) {
byte px = img[x * ROWS + y];
if (px % GREEN == 0) off(greens[y]);
if (px % RED == 0) off(reds[y]);
if (px % BLUE == 0) off(blues[y]);
}
delayMicroseconds(400);
}
}
char buf[IMG];
byte readed = 0;
boolean reading = false;
void readImg() {
while (Serial.available()) {
byte bt = Serial.read();
if (bt == 0) {
readed = 0;
reading = true;
} else if (bt == 7 && reading) {
reading = false;
for (byte i = 0; i < IMG; i++)
img[i] = buf[i];
} else if (readed == IMG) {
reading = false;
} else if (reading) {
buf[readed] = bt;
readed++;
}
}
}
void setup() {
Serial.begin(9600);
initLeds();
resetImg(GREEN);
}
void serialEvent() {
readImg();
}
void loop() {
drawImg();
}
import sys
import array
import serial
from PIL import Image
def make_color((r, g, b)):
color = 1
if r > 200:
color *= 2
if g > 200:
color *= 3
if b > 200:
color *= 5
return color
def preapare_img(path):
pxs = Image.open(path)\
.resize((8, 8))\
.load()
return [
make_color(pxs[x, y])
for x in range(8) for y in range(8)]
def write(img, device):
ser = serial.Serial(device, 9600)
arr = array.array('b', [0] + img + [7])
ser.write(arr.tostring())
write(preapare_img(sys.argv[1]), sys.argv[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment