From 0b59f4906fd5dba558570276339d1c5f4d4d7ec4 Mon Sep 17 00:00:00 2001 From: mattmcw Date: Mon, 5 Aug 2024 06:47:12 -0400 Subject: [PATCH] Implement the button and switch logic for physical interfaces to the camera. In the case that the open/close switch is changed, move to that state, otherwise allow for digital control. --- .../mcopy_mitchell_camera.ino | 78 ++++++++++++++++++- 1 file changed, 74 insertions(+), 4 deletions(-) diff --git a/ino/mcopy_mitchell_camera/mcopy_mitchell_camera.ino b/ino/mcopy_mitchell_camera/mcopy_mitchell_camera.ino index 2ae008c..4b539db 100644 --- a/ino/mcopy_mitchell_camera/mcopy_mitchell_camera.ino +++ b/ino/mcopy_mitchell_camera/mcopy_mitchell_camera.ino @@ -18,9 +18,10 @@ volatile long exposureAvg = 250; volatile String exposureString; volatile long exposureTarget = -1; -volatile bool direction = true; +volatile bool direction = true; //true forward, false backward -volatile bool directionSwitch = true; +volatile bool directionSwitch = true; //true forward, false backward +volatile bool openCloseSwitch = true; //true closed, false opened EndstopCameraShield cam(usPulse, microsteps); McopySerial mc; @@ -48,6 +49,7 @@ void loop () { cmdChar = mc.loop(); cmd(cmdChar); cam.loop(); + buttons(); } void cmd (char val) { @@ -97,6 +99,11 @@ void camera () { long pause; long ms; + if (cam.isOpened()) { + cam.toClose(); + start = millis(); + } + if (exposureTarget > -1) { half = exposureAvg / 2; //assume a 180 shutter pause = exposureTarget - half; @@ -154,6 +161,69 @@ void state () { void updateAvg (long value) { exposureAvg = round((exposureAvg + value) / 2); } + /** - * Button logic - **/ \ No newline at end of file + * Button/Switch logic + **/ + +void buttons () { + int cameraButtonState = digitalRead(cameraButtonPin); + int directionSwitchState = digitalRead(directionSwitchPin); + int openCloseSwitchState = digitalRead(openCloseSwitchPin); + + if (directionSwitchState == LOW && directionSwitch == false) { + directionSwitch = true; + } else if (directionSwitchState == HIGH && directionSwitch == true) { + directionSwitch = false; + } + + if (openCloseSwitchState == LOW && openCloseSwitch == false) { + openCloseSwitch = true; + switch_open_close(); + } else if (openCloseSwitchState == HIGH && openCloseSwitch == true) { + openCloseSwitch = false; + switch_open_close(); + } + + if (cameraButtonState == LOW) { + button_camera(); + } +} + +void button_camera () { + long start = millis(); + long ms; + + if (direction != directionSwitch) { + cam.setDirection(directionSwitch); + } + + if (cam.isOpened()) { + cam.toClose(); + start = millis(); + } + + cam.frame(); + ms = millis() - start; + updateAvg(ms); + + mc.log("button_camera()"); + + if (direction != directionSwitch) { + cam.setDirection(direction); + } +} + +void switch_open_close () { + if (direction != directionSwitch) { + cam.setDirection(directionSwitch); + } + if (openCloseSwitch && !cam.isClosed()) { + cam.toClose(); + } else if (!openCloseSwitch && !cam.isOpened()) { + cam.toOpen(); + } + if (direction != directionSwitch) { + cam.setDirection(direction); + } +} \ No newline at end of file