Skip to content

Commit

Permalink
Merge pull request #6 from teslaworksumn/dev
Browse files Browse the repository at this point in the history
Dev -> Master
  • Loading branch information
computergeek125 authored Mar 6, 2017
2 parents 4201a4a + 653f71c commit 699fe33
Show file tree
Hide file tree
Showing 34 changed files with 2,524 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@

# Debug files
*.dSYM/

# OS Files
.DS_Store
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "lib/Adafruit-PWM-Servo-Driver-Library"]
path = lib/Adafruit-PWM-Servo-Driver-Library
url = https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library
115 changes: 115 additions & 0 deletions blossom_boardcontrol/blossom_boardcontrol.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#include <Adafruit_PWMServoDriver.h>
#include <crc16.h>

// Defines
#define RTSPIN 2
#define CTSPIN 3
#define NUM_BOARDS 9
#define CH_PER_BOARD 16
#define MAGIC_LENGTH 4

const byte magic[] = {0xde, 0xad, 0xbe, 0xef};
Adafruit_PWMServoDriver *boards = new Adafruit_PWMServoDriver[NUM_BOARDS];
crc16 crc;


void setup()
{
// Start serial
Serial.begin(115200);

// Setup pins
pinMode(RTSPIN, INPUT);
pinMode(CTSPIN, OUTPUT);

// Setup breakout boards
for (uint8_t i = 0; i < NUM_BOARDS; i++)
{
// the default address is 0x40
Adafruit_PWMServoDriver *pwm = new Adafruit_PWMServoDriver(0x40 + i);
pwm->begin();
pwm->setPWMFreq(1600); // 1600? is the maximum PWM frequency
boards[i] = *pwm;
}

}

uint8_t *readData(uint16_t len)
{
uint8_t *data = new uint8_t[len];
for (uint8_t i = 0; i < len; i++)
{
// TODO check that Serial is available. while loop?
uint8_t datam = Serial.read();
data[i] = datam;
crc.updateCrc(datam);
}
return data;
}

void sendData(uint8_t *data)
{
for (uint8_t board_idx = 0; board_idx < NUM_BOARDS; board_idx++)
{
for (uint8_t channel = 0; channel < CH_PER_BOARD; channel++)
{
int index = board_idx * CH_PER_BOARD + channel;
boards[board_idx].setPWM(channel, 0, data[index] << 4);
}
}
}

boolean magicFound()
{
if (Serial.peek() == 0xde) Serial.read();
else return false;

if (Serial.peek() == 0xad) Serial.read();
else return false;

if (Serial.peek() == 0xbe) Serial.read();
else return false;

if (Serial.peek() == 0xef) Serial.read();
else return false;

return true;
}

uint16_t getTwoBytesSerial()
{
uint8_t low = Serial.read();
uint16_t high = Serial.read() << 8;
uint16_t combined = high + low;
return combined;
}

void loop()
{
digitalWrite(CTSPIN, HIGH);
if (Serial.available())
{
if (magicFound())
{
digitalWrite(CTSPIN, LOW);

uint8_t lengthLow = Serial.read();
uint16_t lengthHigh = Serial.read() << 8;
uint16_t len = lengthHigh + lengthLow;

crc.clearCrc();
uint8_t *data = readData(len);
uint16_t packetCrc = getTwoBytesSerial();

if (crc.getCrc() == packetCrc)
{
sendData(data);
digitalWrite(CTSPIN, HIGH);
}

}
}


}

25 changes: 13 additions & 12 deletions blossom.ino → debug/debug.ino
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ Adafruit_PWMServoDriver* boards = new Adafruit_PWMServoDriver[9];

void setup()
{
// Serial.begin(9600);
// Serial.println("16 channel PWM test!");
Wire.begin();

Serial.begin(9600);
Serial.println("Starting up");

for (uint8_t i = 0; i < 9; i++)
{
Adafruit_PWMServoDriver* pwm = new Adafruit_PWMServoDriver(i);
pwm->begin();
pwm->setPWMFreq(1600); // This is the maximum PWM frequency
boards[i] = *pwm;
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x40 + i);
pwm.begin();
pwm.setPWMFreq(800); // This is the maximum PWM frequency
boards[i] = pwm;
}

}
Expand All @@ -24,23 +26,22 @@ void loop()
// Fill with random data
uint8_t* data = new uint8_t[144];
uint8_t* fill = new uint8_t[3];
fill[0] = 0;
fill[1] = 120;
fill[0] = 255;
fill[1] = 150;
fill[2] = 255;
for (int i = 0; i < 144; i++)
{
data[i] = fill[i%3];
}

// Bit shift and send to boards
for (uint8_t board_idx = 0; board_idx < 9; board_idx++)
{
for (uint8_t channel = 0; channel < 16; channel++)
{
boards[board_idx].setPWM(channel, 0, data[board_idx * 16 + channel] << 4);
int index = board_idx * 16 + channel;
boards[board_idx].setPWM(channel, 0, data[index] << 4);
}
}



}
155 changes: 155 additions & 0 deletions domeshow_blossom/domeshow_blossom.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
#include <crc16.h>

#define DEBUG
#ifdef DEBUG
boolean messagewalk = false; // Prevents spamming Serial monitor
#endif

#define BOARD_IDX 5

#define NUM_BOARDS 9
#define CH_PER_BOARD 16

#define MAGIC_LENGTH 4
const byte magic[] = {0xde, 0xad, 0xbe, 0xef};
byte magic_status = 0;

#define STATE_READY 0
#define STATE_READING 1
#define STATE_WRITING 2
byte state = STATE_READY;

Adafruit_PWMServoDriver* board;
crc16 crc;

byte *data;
uint16_t data_len;


void setup() {
Serial.begin(115200);
Serial.write('B');

// Guess at size of data array
data_len = NUM_BOARDS * CH_PER_BOARD;
data = new byte[data_len];

uint8_t addresses[] = {0x42, 0x44, 0x41, 0x45, 0x40, 0x43, 0x46, 0x47, 0x48};

// Setup breakout boards
board = new Adafruit_PWMServoDriver(0x40 + BOARD_IDX);
board->begin();
board->setPWMFreq(1600); // 1600? is the maximum PWM frequency

}

void loop() {
switch (state) {
case STATE_READY:
#ifdef DEBUG
if (!messagewalk) Serial.println("READY");
#endif
messagewalk = true;
//cts();
if (Serial.available() > 1) {
Serial.print("Peek: ");
char b = Serial.peek();
Serial.println(b);
if (Serial.read() == magic[magic_status]) {
magic_status++;
} else {
magic_status=0;
}
Serial.print("Magic status: ");
Serial.println(magic_status);
if (magic_status >= MAGIC_LENGTH) {
state = STATE_READING;
messagewalk = false;
}
}
break;
case STATE_READING:
#ifdef DEBUG
if (!messagewalk) Serial.println("READING");
#endif
messagewalk = true;
if (Serial.available() > 2) {
uint16_t len = getTwoBytesSerial();
Serial.print("Length: ");
Serial.println(len);
readData(len);
Serial.println("Data read");
// Update len for future use (writing)
data_len = len;
uint16_t packetCrc = getTwoBytesSerial();
Serial.print("Calculated CRC: ");
uint16_t calculatedCrc = crc.XModemCrc(data, 0, data_len);
Serial.println(calculatedCrc, HEX);
Serial.print("Received CRC: ");
Serial.println(packetCrc, HEX);
messagewalk = false;
if (calculatedCrc != packetCrc)
{
Serial.println("CRC doesn't match");
state = STATE_READY;
}
else
{
state = STATE_WRITING;
}
}
break;
case STATE_WRITING:
#ifdef DEBUG
if (!messagewalk) Serial.println("WRITING");
#endif
messagewalk = false;
for (uint8_t channel = 0; channel < CH_PER_BOARD; channel++) {
uint16_t index = BOARD_IDX * CH_PER_BOARD + channel;
board->setPWM(channel, 0, data[index] << 4);
}
Serial.println("Done writing");
state = STATE_READY;
break;
default:
state = STATE_READY;
messagewalk = false;
break;
}
}

uint16_t getTwoBytesSerial() {
// Wait for serial bytes
while (Serial.available() < 2) {}
uint16_t high = Serial.read() << 8;
uint16_t low = Serial.read();
uint16_t combined = high | low;
return combined;
}

void readData(uint16_t len) {
// Resize data array if smaller than len
// Should never happen
if (len > data_len) {
Serial.println("Resizing data array");
byte* new_data = new byte[len];
memcpy(data, new_data, data_len);
data_len = len;
}

while (Serial.available() < len) {}

// Read in data from serial
Serial.readBytes(data, len);

}

void cts() {
for (int i=MAGIC_LENGTH-1; i>=0; i--) {
Serial.write(magic[i]);
}
}


Loading

0 comments on commit 699fe33

Please sign in to comment.