Skip to content

Instantly share code, notes, and snippets.

@xseignard
Created May 6, 2015 01:49
Show Gist options
  • Save xseignard/3b2b8d810122a8235540 to your computer and use it in GitHub Desktop.
Save xseignard/3b2b8d810122a8235540 to your computer and use it in GitHub Desktop.
sweatlodgeParty
#include <SPI.h>
#include <DMD.h>
#include <TimerOne.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
// -----------------------------------------------------------
// DMD stuff
// USE with https://github.com/xseignard/DMD, not the freetronics one!!
// -----------------------------------------------------------
#define DISPLAYS_ACROSS 5
#define DISPLAYS_DOWN 1
#define DISPLAYS_BPP 1
#define RED 0xFF
#define BLACK 0
DMD dmd(DISPLAYS_ACROSS, DISPLAYS_DOWN, DISPLAYS_BPP);
//DMD dmd(DISPLAYS_ACROSS, DISPLAYS_DOWN);
int currentHeight = 0;
int brightness = 255;
// -----------------------------------------------------------
// Ethernet/UDP stuff
// -----------------------------------------------------------
// one
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 2, 2);
// two
//byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF };
//IPAddress ip(192, 168, 2, 3);
// three
//byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xFE };
//IPAddress ip(192, 168, 2, 4);
unsigned int localPort = 8888;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
EthernetUDP Udp;
void setup() {
Serial.begin(9600);
// ethernet communication
Ethernet.begin(mac, ip);
Udp.begin(localPort);
// set brightness of the display
pinMode(9, OUTPUT);
analogWrite(9, brightness);
// initialize timer for spi communication
Timer1.initialize(2000);
Timer1.attachInterrupt(ScanDMD);
// turn off all pixels
flash(2);
fill(80, 1000);
}
void loop() {
int packetSize = Udp.parsePacket();
if (packetSize) {
// clear previous things stored in the buffer
memset(packetBuffer, 0, sizeof(packetBuffer));
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
handlePacket(packetBuffer);
}
}
void handlePacket(char* text) {
// fill mode
if (text[0] == 'f') {
String msg = String(text[1]) + String(text[2]) + String(text[3]);
int height = msg.toInt();
height = constrain(height, 0, 32 * DISPLAYS_ACROSS);
fill(height, 1000);
}
// set a defined brightness
else if (text[0] == 'b') {
String msg = String(text[1]) + String(text[2]) + String(text[3]);
brightness = msg.toInt();
brightness = constrain(brightness, 0, 255);
}
// increase brightness
else if (text[0] == '+') {
brightness += 10;
brightness = constrain(brightness, 0, 255);
}
// decrease brightness
else if (text[0] == '-') {
brightness -= 10;
brightness = constrain(brightness, 0, 255);
}
// stops the displays
else if (text[0] == 's') {
dmd.clearScreen(true);
}
}
// -----------------------------------------------------------
// DMD effects
// -----------------------------------------------------------
void fill(int height, int microDelay) {
if (height > currentHeight) {
for(int i=currentHeight; i<=height; i++){
addLine();
delayMicroseconds(microDelay);
}
}
else if (currentHeight > height) {
for(int i=height; i<=currentHeight; i++){
removeLine();
delayMicroseconds(microDelay);
}
}
}
void addLine() {
if (currentHeight < 32*DISPLAYS_ACROSS) currentHeight++;
int height = 32*DISPLAYS_ACROSS - currentHeight;
dmd.drawLine(height, 0, height, 16, RED);
}
void removeLine() {
int height = 32*DISPLAYS_ACROSS - currentHeight;
dmd.drawLine(height, 0, height, 16, BLACK);
if (currentHeight > 0) currentHeight--;
}
void flash(int times) {
int oldBrightness = brightness;
brightness = 255;
for(int i = 0; i < times; i++) {
dmd.clearScreen(RED);
delay(500);
dmd.clearScreen(BLACK);
delay(500);
}
brightness = oldBrightness;
}
void ScanDMD() {
dmd.scanDisplayBySPI();
analogWrite(9, brightness);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment