From daece3b45a83300c3ae9882e1dae4a852f8baf9c Mon Sep 17 00:00:00 2001 From: Matthew McWilliams Date: Tue, 11 Apr 2023 00:38:06 -0400 Subject: [PATCH] Add the new mcopy_cam_relay script. --- ino/mcopy_cam_relay/McopySerial.cpp | 68 ++++++++++++ ino/mcopy_cam_relay/McopySerial.h | 83 ++++++++++++++ ino/mcopy_cam_relay/mcopy_cam_relay.ino | 141 ++++++++++++++++++++++++ scripts/ino.sh | 1 + 4 files changed, 293 insertions(+) create mode 100644 ino/mcopy_cam_relay/McopySerial.cpp create mode 100644 ino/mcopy_cam_relay/McopySerial.h create mode 100644 ino/mcopy_cam_relay/mcopy_cam_relay.ino diff --git a/ino/mcopy_cam_relay/McopySerial.cpp b/ino/mcopy_cam_relay/McopySerial.cpp new file mode 100644 index 0000000..d1af619 --- /dev/null +++ b/ino/mcopy_cam_relay/McopySerial.cpp @@ -0,0 +1,68 @@ +/// mcopy Serial Library + +#include "McopySerial.h" + +McopySerial::McopySerial () {} + +void McopySerial::begin (char identity) { + id = identity; + 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) { + debug(!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::debug (bool state) { + debugOn = state; + log("debug()"); +} + +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); +} \ No newline at end of file diff --git a/ino/mcopy_cam_relay/McopySerial.h b/ino/mcopy_cam_relay/McopySerial.h new file mode 100644 index 0000000..f02c089 --- /dev/null +++ b/ino/mcopy_cam_relay/McopySerial.h @@ -0,0 +1,83 @@ +#ifndef MCOPY_SERIAL +#define MCOPY_SERIAL + +#include + +class McopySerial { + + private: + + const uint16_t serialDelay = 5; + const uint16_t baud = 57600; + + volatile bool debugOn = false; + volatile char cmdChar = 'z'; + volatile char id; + + void _internal (); + void _connect (); + void _identify (); + + public: + + /* CMD FLAGS */ + const char BLACK = 'b'; + const char CAMERA = 'c'; + const char CAMERA_BACKWARD = 'f'; + const char CAMERA_CAPPER_IDENTIFIER = '8'; + const char CAMERA_CAPPER_PROJECTOR_IDENTIFIER = '9'; + const char CAMERA_CAPPER_PROJECTORS_IDENTIFIER = '0'; + const char CAMERA_EXPOSURE = 'G'; + const char CAMERA_FORWARD = 'e'; + const char CAMERA_IDENTIFIER = 'k'; + const char CAMERA_PROJECTORS_IDENTIFIER = '5'; + const char CAMERA_SECOND = '3'; + const char CAMERA_SECOND_BACKWARD = '2'; + const char CAMERA_SECOND_FORWARD = '1'; + const char CAMERA_SECOND_IDENTIFIER = 'y'; + const char CAMERA_TIMED = 'n'; + const char CAMERAS = '4'; + const char CAMERAS_IDENTIFIER = 'a'; + const char CAMERAS_PROJECTOR_IDENTIFIER = '6'; + const char CAMERAS_PROJECTORS_IDENTIFIER = '7'; + const char CAPPER_IDENTIFIER = 'C'; + const char CAPPER_OFF = 'B'; + const char CAPPER_ON = 'A'; + const char CONNECT = 'i'; + const char DEBUG = 'd'; + const char ERROR = 'E'; + const char LIGHT = 'l'; + const char LIGHT_IDENTIFIER = 'o'; + const char MCOPY_IDENTIFIER = 'm'; + const char PROJECTOR = 'p'; + const char PROJECTOR_BACKWARD = 'h'; + const char PROJECTOR_CAMERA_IDENTIFIER = 's'; + const char PROJECTOR_CAMERA_LIGHT_IDENTIFIER = 'r'; + const char PROJECTOR_FORWARD = 'g'; + const char PROJECTOR_IDENTIFIER = 'j'; + const char PROJECTOR_LIGHT_IDENTIFIER = 'q'; + const char PROJECTOR_SECOND = 'w'; + const char PROJECTOR_SECOND_BACKWARD = 'v'; + const char PROJECTOR_SECOND_FORWARD = 'u'; + const char PROJECTOR_SECOND_IDENTIFIER = 't'; + const char PROJECTORS = 'x'; + const char PROJECTORS_IDENTIFIER = 'd'; + const char STATE = 'H'; + const char TAKEUP_BACKWARD = 'F'; + const char TAKEUP_FORWARD = 'D'; + /* END CMD FLAGS */ + + McopySerial(); + + void begin(char identity); + char loop(); + void confirm(char cmd); + String getString(); + void print(String message); + + void debug (bool state); + void log (String message); + +}; + +#endif diff --git a/ino/mcopy_cam_relay/mcopy_cam_relay.ino b/ino/mcopy_cam_relay/mcopy_cam_relay.ino new file mode 100644 index 0000000..76be15f --- /dev/null +++ b/ino/mcopy_cam_relay/mcopy_cam_relay.ino @@ -0,0 +1,141 @@ +/* + Wiring + + CAMERA + CAMERA_DIR + Wire directly to corresponding relay pins. + Arduino 2 3 + Relay 1 2 +*/ + +boolean debug_state = false; + +//unsigned long now; //to be compared to stored values every loop + +//CAMERA CONSTANTS +const int CAMERA = 2; +const int CAMERA_DIR = 3; + +const int CAMERA_MOMENT = 240; +const int CAMERA_FRAME = 600; +//CAMERA VARIABLES +boolean cam_dir = true; + +//CAMERA COMMANDS +const char cmd_camera = 'c'; +const char cmd_cam_forward = 'e'; +const char cmd_cam_backward = 'f'; + +const char cmd_debug = 'd'; +const char cmd_connect = 'i'; +volatile char cmd_char = 'z'; +const char cmd_mcopy_identifier = 'm'; + +const char cmd_cam_identifier = 'k'; + +const int serialDelay = 5; + +void setup () { + Serial.begin(57600); + Serial.flush(); + //Serial.setTimeout(serialDelay); + + pins(); +} + +void loop () { + //now = millis(); + if (Serial.available()) { + /* read the most recent byte */ + cmd_char = (char)Serial.read(); + } + if (cmd_char != 'z') { + cmd(cmd_char); + cmd_char = 'z'; + } +} + +void pins () { + //RELAYS + pinMode(CAMERA, OUTPUT); + + pinMode(CAMERA_DIR, OUTPUT); + + //SET LOW + digitalWrite(CAMERA, HIGH); + + digitalWrite(CAMERA_DIR, HIGH); + +} + +void cmd (char val) { + if (val == cmd_debug) { + debug(); + } else if (val == cmd_connect) { + connect(); + } else if (val == cmd_mcopy_identifier) { + identify(); + } else if (val == cmd_cam_forward) { + cam_direction(true); //explicit + } else if (val == cmd_cam_backward) { + cam_direction(false); + } else if (val == cmd_camera) { + cam_start(); + } +} + +void debug () { + debug_state = true; + Serial.println(cmd_debug); + log("debugging enabled"); +} + +void connect () { + Serial.println(cmd_connect); + log("connect()"); +} + +void identify () { + Serial.println(cmd_cam_identifier); + log("identify()"); +} + +void setDir (int pin, boolean dir) { + if (!dir) { + digitalWrite(pin, LOW); + } else { + digitalWrite(pin, HIGH); + } +} + +void cam_start () { + digitalWrite(CAMERA, LOW); + delay(CAMERA_MOMENT); + digitalWrite(CAMERA, HIGH); + delay(CAMERA_FRAME - CAMERA_MOMENT); + cam_stop(); +} + +void cam_stop () { + Serial.println(cmd_camera); + log("camera()"); +} + + +void cam_direction (boolean state) { + cam_dir = state; + if (state) { + digitalWrite(CAMERA_DIR, LOW); + Serial.println(cmd_cam_forward); + log("cam_direction -> true"); + } else { + digitalWrite(CAMERA_DIR, HIGH); + Serial.println(cmd_cam_backward); + log("cam_direction -> false"); + } +} + +void log (String msg) { + if (debug_state) { + Serial.println(msg); + } +} diff --git a/scripts/ino.sh b/scripts/ino.sh index ae9bb81..48f7c6b 100644 --- a/scripts/ino.sh +++ b/scripts/ino.sh @@ -18,6 +18,7 @@ fi SKETCHES=( mcopy_cam_canon_ble + mcopy_cam_relay mcopy_JKMM100 components/mcopy_light )