-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from teslaworksumn/dev
Dev -> Master
- Loading branch information
Showing
34 changed files
with
2,524 additions
and
12 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,6 @@ | |
|
||
# Debug files | ||
*.dSYM/ | ||
|
||
# OS Files | ||
.DS_Store |
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
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 |
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
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); | ||
} | ||
|
||
} | ||
} | ||
|
||
|
||
} | ||
|
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
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
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]); | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.