diff --git a/ino/mcopy_projector_firmware/IteadDualStepperShield.h b/ino/mcopy_projector_firmware/IteadDualStepperShield.h index cf69790..76fca75 100644 --- a/ino/mcopy_projector_firmware/IteadDualStepperShield.h +++ b/ino/mcopy_projector_firmware/IteadDualStepperShield.h @@ -1,7 +1,6 @@ #ifndef IteadDualStepperShield_h #define IteadDualStepperShield_h - #include class IteadDualStepperShield { diff --git a/ino/mcopy_projector_firmware/McopyProjector.cpp b/ino/mcopy_projector_firmware/McopyProjector.cpp new file mode 100644 index 0000000..6a88497 --- /dev/null +++ b/ino/mcopy_projector_firmware/McopyProjector.cpp @@ -0,0 +1,61 @@ +/// Mcopy Projector Class + +#include "McopyProjector.h" +#include "IteadDualStepperShield.h" + +McopyProjector::McopyProjector () { + +} + +void McopyProjector::begin () { + steppers.setup(); + steppers.setSpeed(0, _speed); + steppers.setSpeed(1, _speed); +} + +void McopyProjector::setDirection (bool dir) { + _dir = dir; + if (_dir) { + steppers.setDir(0, 1); + steppers.setDir(1, 1); + } else { + steppers.setDir(0, 0); + steppers.setDir(1, 0); + } +} + +void McopyProjector::frame (bool dir) { + if (dir != _dir) { + setDirection(dir); + } + steppers.stepBoth(_stepsPerFrame); + _posTakeup += dir ? _stepsPerFrame : -_stepsPerFrame; + _posFeed += dir ? _stepsPerFrame : -_stepsPerFrame; +} + +void McopyProjector::adjust(uint8_t motor, int32_t steps) { + if (steps < 0) { + steppers.setDir(motor, 0); + } else { + steppers.setDir(motor, 1); + } + steppers.step(motor, abs(steps)); + if (motor == 0) { + _posTakeup += steps; + } else if (motor == 1) { + _posFeed += steps; + } +} + +void McopyProjector::adjustBoth(int32_t steps) { + if (steps < 0) { + steppers.setDir(0, 0); + steppers.setDir(1, 0); + } else { + steppers.setDir(0, 1); + steppers.setDir(1, 1); + } + steppers.stepBoth(abs(steps)); + _posTakeup += steps; + _posFeed += steps; +} diff --git a/ino/mcopy_projector_firmware/McopyProjector.h b/ino/mcopy_projector_firmware/McopyProjector.h new file mode 100644 index 0000000..58c8b6d --- /dev/null +++ b/ino/mcopy_projector_firmware/McopyProjector.h @@ -0,0 +1,36 @@ +#ifndef MCOPY_PROJECTOR +#define MCOPY_PROJECTOR + +#include +#include "IteadDualStepperShield.h" + +class McopyProjector { + + private: + + IteadDualStepperShield steppers; + + uint8_t _motorSteps = 200; + uint8_t _frames = 8; + uint8_t _stepsPerFrame = round(_motorSteps / _frames); + uint16_t _speed = 500; + + int64_t _posTakeup = 0; + int64_t _posFeed = 0; + + bool _dir = true; + + public: + + McopyProjector(); + void begin(); + //0 = takeup, 1 = feed + void adjust(uint8_t motor, int32_t steps); + void adjustBoth(int32_t steps); + //true = forward, false = back + void frame(bool dir); + void frames(bool dir, uint32_t count); + void setDirection(bool dir); +}; + +#endif \ No newline at end of file diff --git a/ino/mcopy_projector_firmware/mcopy_projector_firmware.ino b/ino/mcopy_projector_firmware/mcopy_projector_firmware.ino index eb76b7f..594dea0 100644 --- a/ino/mcopy_projector_firmware/mcopy_projector_firmware.ino +++ b/ino/mcopy_projector_firmware/mcopy_projector_firmware.ino @@ -21,10 +21,11 @@ Y02A - Blue Y02B - Red */ - +#include "McopyProjector.h" #include "McopySerial.h" #include "IteadDualStepperShield.h" + //CAMERA CONSTANTS const int BUTTON = 7; const int LED_FWD = 8; @@ -41,17 +42,16 @@ volatile bool direction = true; volatile long start; McopySerial mcopy; -IteadDualStepperShield steppers; +McopyProjector projector; void setup () { - steppers.setup(); - steppers.setSpeed(0, 500); - steppers.setSpeed(1, 500); + pins(); digitalWrite(LED_FWD, HIGH); digitalWrite(LED_BWD, HIGH); mcopy.begin(mcopy.PROJECTOR_IDENTIFIER); + projector.begin(); delay(42); digitalWrite(LED_FWD, LOW); digitalWrite(LED_BWD, LOW); @@ -62,7 +62,7 @@ void loop () { cmdChar = mcopy.loop(); cmd(cmdChar); if (digitalRead(BUTTON) == LOW) { - projector(); + projector_frame(); } } @@ -76,12 +76,12 @@ void pins () { } void cmd (char val) { - if (val == mcopy.CAMERA_FORWARD) { + if (val == mcopy.PROJECTOR_FORWARD) { projector_direction(true); - } else if (val == mcopy.CAMERA_BACKWARD) { + } else if (val == mcopy.PROJECTOR_BACKWARD) { projector_direction(false); } else if (val == mcopy.PROJECTOR) { - projector(); + projector_frame(); } else if (val == mcopy.STATE) { state(); } @@ -92,13 +92,11 @@ void projector_direction (boolean state) { if (state) { mcopy.confirm(mcopy.PROJECTOR_FORWARD); mcopy.log("projector_direction(true)"); - steppers.setDir(0, 1); - steppers.setDir(1, 1); + projector.setDirection(true); } else { mcopy.confirm(mcopy.PROJECTOR_BACKWARD); mcopy.log("projector_direction(false)"); - steppers.setDir(0, 0); - steppers.setDir(1, 0); + projector.setDirection(false); } } @@ -116,10 +114,10 @@ void projector_timing_end () { } } -void projector () { +void projector_frame () { int LED = direction ? LED_FWD : LED_BWD; digitalWrite(LED, HIGH); - steppers.stepBoth(PROJECTOR_STEPS); + projector.frame(direction); mcopy.confirm(mcopy.PROJECTOR); digitalWrite(LED, LOW); }