mcopy/ino/mcopy_oxberry_camera/mcopy_oxberry_camera.ino

121 lines
2.4 KiB
Arduino
Raw Normal View History

2024-06-23 02:57:20 +00:00
#include "EndstopCameraShield.h"
#include "McopySerial.h"
const bool DEBUG = false;
const uint8_t enableButtonPin = 9;
const uint8_t enableButtonPin = 10;
const uint8_t enableButtonPin = 11;
const uint8_t enableButtonPin = 12;
const uint8_t LEDPin = 13;
const uint32_t usPulse = 300;
const uint8_t microsteps = 2;
volatile char cmdChar = 'z';
volatile long now;
volatile long exposureAvg = -1; //pre-fill
volatile String exposureString;
volatile long exposureTarget = -1;
volatile bool direction = true;
volatile bool directionSwitch = true;
EndstopCameraShield cam(usPulse, microsteps);
McopySerial mc;
2024-06-23 02:57:20 +00:00
void setup () {
mc.begin(mc.CAMERA_IDENTIFIER);
mc.debug(DEBUG);
cam.setup();
if (cam.isOpened()) {
mc.log("Camera is OPENED");
} else if (cam.isClosed()) {
mc.log("Camera is CLOSED");
}
2024-06-23 02:57:20 +00:00
}
void loop () {
now = millis();
cmdChar = mc.loop();
cmd(cmdChar);
cam.loop();
}
void cmd (char val) {
if (val == mc.CAMERA_FORWARD) {
camera_direction(true);
} else if (val == mc.CAMERA_BACKWARD) {
camera_direction(false);
} else if (val == mc.CAMERA) {
camera();
} else if (val == mc.CAMERA_EXPOSURE) {
exposure();
} else if (val == mc.STATE) {
state();
}
}
void exposure () {
exposureString = mc.getString();
parseExposureString();
cameraFrame = exposureTarget;
mc.confirm(mc.CAMERA_EXPOSURE);
}
void parseExposureString () {
exposureTarget = exposureString.toInt();
}
void camera_direction (boolean state) {
direction = state;
cam.setDirection(direction);
if (state) {
mc.confirm(mc.CAMERA_FORWARD);
mc.log("camera_direction(true)");
} else {
mc.confirm(mc.CAMERA_BACKWARD);
mc.log("camera_direction(false)");
}
}
void camera () {
long start, half, pause;
if (exposureTarget > -1) {
half = exposureAvg / 2; //assume a 180 shutter
pause = exposureTarget - half;
if (pause < exposureAvg) {
cam.frame();
} else {
cam.toOpen();
delay(pause);
cam.toClose();
}
} else{
millis();
cam.frame();
updateAvg(millis() - start);
}
mc.confirm(mc.CAMERA);
}
void state () {
String stateString = String(mc.STATE);
stateString += String(mc.CAMERA_EXPOSURE);
if (exposureTarget > -1) {
stateString += String(exposureTarget);
} else {
stateString += String(exposureAvg);
}
stateString += String(mc.STATE);
mc.sendString(stateString);
}
void updateAvg (long value) {
exposureAvg = round((exposureAvg + value) / 2);
}
/**
* Button logic
**/