Rewrite mcopy_light to use McopySerial

This commit is contained in:
Matt McWilliams 2023-03-18 17:13:41 -04:00
parent 210dc85558
commit 3689eb7528
4 changed files with 179 additions and 40 deletions

View File

@ -0,0 +1,76 @@
/// mcopy Serial Library
#include "McopySerial.h"
McopySerial::McopySerial (char identity) {
id = identity;
}
void McopySerial::begin () {
Serial.begin(baud);
Serial.flush();
Serial.setTimeout(serialDelay);
}
char McopySerial::loop () {
if (Serial.available()) {
cmdChar = (char) Serial.read();
_internal();
} else {
cmdChar = 'z';
}
return cmdChar;
}
void McopySerial::_internal () {
if (cmdChar == DEBUG) {
debugOn = !debugOn;
} else if (cmdChar == CONNECT) {
_connect();
} else if (cmdChar == MCOPY_IDENTIFIER) {
_identify();
}
}
void McopySerial::_connect () {
Serial.println(CONNECT);
log("connect()");
}
void McopySerial::_identify () {
Serial.println(id);
log("identify()");
}
void McopySerial::setBaud (int baudRate) {
baud = baudRate;
}
void McopySerial::setIdentity (char identity) {
id = identity;
}
void McopySerial::debug (bool state) {
debugOn = state;
}
void McopySerial::confirm (char cmd) {
Serial.println(cmd);
}
void McopySerial::log (String message) {
if (debugOn) {
Serial.println(message);
}
}
String McopySerial::getString () {
while (Serial.available() == 0) {
//Wait for value string
}
return Serial.readString();
}
void McopySerial::print (String message) {
Serial.println(message);
}

View File

@ -0,0 +1,84 @@
#ifndef MCOPY_SERIAL
#define MCOPY_SERIAL
#include "Arduino.h"
class McopySerial {
private:
const int serialDelay = 5;
volatile int baud = 57600;
volatile bool debugOn = false;
volatile char cmdChar = 'z';
volatile char id;
void _internal ();
void _connect ();
void _identify ();
public:
/* CMD FLAGS */
static const char BLACK = 'b';
static const char CAMERA = 'c';
static const char CAMERA_BACKWARD = 'f';
static const char CAMERA_CAPPER_IDENTIFIER = '8';
static const char CAMERA_CAPPER_PROJECTOR_IDENTIFIER = '9';
static const char CAMERA_CAPPER_PROJECTORS_IDENTIFIER = '0';
static const char CAMERA_EXPOSURE = 'G';
static const char CAMERA_FORWARD = 'e';
static const char CAMERA_IDENTIFIER = 'k';
static const char CAMERA_PROJECTORS_IDENTIFIER = '5';
static const char CAMERA_SECOND = '3';
static const char CAMERA_SECOND_BACKWARD = '2';
static const char CAMERA_SECOND_FORWARD = '1';
static const char CAMERA_SECOND_IDENTIFIER = 'y';
static const char CAMERA_TIMED = 'n';
static const char CAMERAS = '4';
static const char CAMERAS_IDENTIFIER = 'a';
static const char CAMERAS_PROJECTOR_IDENTIFIER = '6';
static const char CAMERAS_PROJECTORS_IDENTIFIER = '7';
static const char CAPPER_IDENTIFIER = 'C';
static const char CAPPER_OFF = 'B';
static const char CAPPER_ON = 'A';
static const char CONNECT = 'i';
static const char DEBUG = 'd';
static const char ERROR = 'E';
static const char LIGHT = 'l';
static const char LIGHT_IDENTIFIER = 'o';
static const char MCOPY_IDENTIFIER = 'm';
static const char PROJECTOR = 'p';
static const char PROJECTOR_BACKWARD = 'h';
static const char PROJECTOR_CAMERA_IDENTIFIER = 's';
static const char PROJECTOR_CAMERA_LIGHT_IDENTIFIER = 'r';
static const char PROJECTOR_FORWARD = 'g';
static const char PROJECTOR_IDENTIFIER = 'j';
static const char PROJECTOR_LIGHT_IDENTIFIER = 'q';
static const char PROJECTOR_SECOND = 'w';
static const char PROJECTOR_SECOND_BACKWARD = 'v';
static const char PROJECTOR_SECOND_FORWARD = 'u';
static const char PROJECTOR_SECOND_IDENTIFIER = 't';
static const char PROJECTORS = 'x';
static const char PROJECTORS_IDENTIFIER = 'd';
static const char STATE = 'H';
static const char TAKEUP_BACKWARD = 'F';
static const char TAKEUP_FORWARD = 'D';
/* END CMD FLAGS */
McopySerial(char identity);
void begin();
void setBaud(int baudRate);
void setIdentity(char identity);
char loop();
void confirm(char cmd);
String getString();
void print(String message);
void debug (bool state);
void log (String message);
};
#endif

View File

@ -1,11 +1,13 @@
#include "SoftwareSerial.h" #include "SoftwareSerial.h"
#include "Adafruit_Pixie.h" #include "Adafruit_Pixie.h"
#include "McopySerial.h"
#define NUMPIXELS 1 // Number of Pixies in the strip #define NUMPIXELS 1 // Number of Pixies in the strip
#define PIXIEPIN 6 // Pin number for SoftwareSerial output #define PIXIEPIN 6 // Pin number for SoftwareSerial output
SoftwareSerial pixieSerial(-1, PIXIEPIN); SoftwareSerial pixieSerial(-1, PIXIEPIN);
Adafruit_Pixie light = Adafruit_Pixie(NUMPIXELS, &pixieSerial); Adafruit_Pixie light = Adafruit_Pixie(NUMPIXELS, &pixieSerial);
McopySerial mc(McopySerial::LIGHT_IDENTIFIER);
String color = "000,000,000"; String color = "000,000,000";
@ -22,18 +24,11 @@ volatile int b = 0;
unsigned long now; //to be compared to stored values every loop unsigned long now; //to be compared to stored values every loop
unsigned long light_time; unsigned long light_time;
volatile char cmd = 'z';
const char cmd_light = 'l';
const char cmd_debug = 'd';
const char cmd_connect = 'i';
volatile char cmd_char = 'z';
const int serialDelay = 5;
void setup () { void setup () {
Serial.begin(57600); mc.begin();
Serial.flush();
Serial.setTimeout(serialDelay);
pixieSerial.begin(115200); // Pixie REQUIRES this baud rate pixieSerial.begin(115200); // Pixie REQUIRES this baud rate
light.setPixelColor(0, 0, 0, 0); light.setPixelColor(0, 0, 0, 0);
light.show(); light.show();
@ -43,42 +38,25 @@ void setup () {
} }
void loop () { void loop () {
if (Serial.available()) { now = millis();
/* read the most recent byte */ cmd = mc.loop();
cmd_char = (char)Serial.read();
} if (cmd == McopySerial::LIGHT) {
if (cmd_char != 'z') { color = mc.getString();
cmd(cmd_char); parseColorString();
cmd_char = 'z'; mc.confirm(McopySerial::LIGHT);
} }
now = millis();
//send light signal to pixie every second //send light signal to pixie every second
if (now - light_time >= 1000) { if (now - light_time >= 1000) {
light.setPixelColor(0, r, g, b); light.setPixelColor(0, r, g, b);
light.show(); light.show();
light_time = now; light_time = now;
} }
} }
//
//l - light - followed by String
//
void cmd (char val) {
if (val == cmd_connect) {
Serial.println(cmd_connect);//confirm connection
} else if (val == cmd_light) {
colorString();
Serial.println(cmd_light);//confirm light change
}
}
void colorString () {
while (Serial.available() == 0) {
//Wait for color string
}
color = Serial.readString();
//Serial.println(color);
void parseColorString () {
commaR = color.indexOf(','); //comma trailing R commaR = color.indexOf(','); //comma trailing R
commaG = color.indexOf(',', commaR + 1); commaG = color.indexOf(',', commaR + 1);

View File

@ -17,4 +17,5 @@ if [ -f "$(which jq)" ]; then
fi fi
cp ino/lib/McopySerial/McopySerial.* ino/mcopy_cam_canon/ cp ino/lib/McopySerial/McopySerial.* ino/mcopy_cam_canon/
cp ino/lib/McopySerial/McopySerial.* ino/mcopy_JKMM100/ cp ino/lib/McopySerial/McopySerial.* ino/mcopy_JKMM100/
cp ino/lib/McopySerial/McopySerial.* ino/components/mcopy_light/