From 3d292ff4804ea3fedc3d797177610dcf924089ae Mon Sep 17 00:00:00 2001 From: Ed Walker Date: Mon, 18 Jan 2021 18:38:18 -0800 Subject: [PATCH] Some cleanup and refactoring. --- visca-controller/visca-controller.h | 36 +++++--- visca-controller/visca-controller.ino | 125 +++++++++++--------------- 2 files changed, 76 insertions(+), 85 deletions(-) diff --git a/visca-controller/visca-controller.h b/visca-controller/visca-controller.h index e45e662..00d0715 100644 --- a/visca-controller/visca-controller.h +++ b/visca-controller/visca-controller.h @@ -21,21 +21,35 @@ #define VISCATX 3 // Button inputs -#define BTN1 4 -#define BTN2 5 -#define BTN3 6 -#define BTN4 7 -#define BTN5 8 -#define BTN6 9 -#define BTN7 10 -#define BTN8 11 -#define BTN9 12 -#define BTN10 13 +//#define BTN1 4 +//#define BTN2 5 +//#define BTN3 6 +//#define BTN4 7 +//#define BTN5 8 +//#define BTN6 9 +//#define BTN7 10 +//#define BTN8 11 +//#define BTN9 12 +//#define BTN10 13 +int buttons[] = { + 4, // BTN1 + 5, // BTN2 + 6, // BTN3 + 7, // BTN4 + 8, // BTN5 + 9, // BTN6 + 10, // BTN7 + 11, // BTN8 + 12, // BTN9 + 13 // BTN10 +}; + const byte numChars = 16; byte viscaMessage[numChars]; -byte buttonStatus = 0x00; +byte buttonPreviousStatus = 0x00; +byte buttonCurrentStatus = 0x00; // Pan/Tilt byte panTilt[9] = { 0x81, 0x01, 0x06, 0x01, 0x00, 0x00, 0x03, 0x03, 0xFF }; diff --git a/visca-controller/visca-controller.ino b/visca-controller/visca-controller.ino index c3f2ba4..11e08f8 100644 --- a/visca-controller/visca-controller.ino +++ b/visca-controller/visca-controller.ino @@ -91,57 +91,6 @@ void handleSerialControl() { case '0': sendViscaPacket(callLedOff, sizeof(callLedOff)); break; - -// Pan/Tilt - case 'q': - sendViscaPacket(panUpLeft, sizeof(panUpLeft)); - break; - case 'w': - sendViscaPacket(panUp, sizeof(panUp)); - break; - case 'e': - sendViscaPacket(panUpRight, sizeof(panUpRight)); - break; - case 'a': - sendViscaPacket(panLeft, sizeof(panLeft)); - break; - case 's': - sendViscaPacket(panStop, sizeof(panStop)); - break; - case 'd': - sendViscaPacket(panRight, sizeof(panRight)); - break; - case 'z': - sendViscaPacket(panDownLeft, sizeof(panDownLeft)); - break; - case 'x': - sendViscaPacket(panDown, sizeof(panDown)); - break; - case 'c': - sendViscaPacket(panDownRight, sizeof(panDownRight)); - break; - -// Zoom - case 'r': - sendViscaPacket(zoomTele, sizeof(zoomTele)); - break; - case 'f': - sendViscaPacket(zoomStop, sizeof(zoomStop)); - break; - case 'v': - sendViscaPacket(zoomWide, sizeof(zoomWide)); - break; - -// Focus - case 't': - sendViscaPacket(focusFar, sizeof(focusFar)); - break; - case 'g': - sendViscaPacket(focusStop, sizeof(focusStop)); - break; - case 'b': - sendViscaPacket(focusNear, sizeof(focusNear)); - break; } } } @@ -152,23 +101,64 @@ void processButtons() { if(millis() > time_now + 100) { time_now = millis(); - if((bool) digitalRead(BTN1) == true) { + for (int i = 0; i < sizeof(buttons) / sizeof (buttons [0]); i++) { + bitWrite(buttonCurrentStatus, buttons[i], (int) digitalRead(buttons[i])); + } + + int btn1 = buttons[0]; + if(buttonPressed(btn1) == true) { + setButtonStatus(btn1, true); + sendZoomPacket(0x20, zoomSpeed); + } else if(buttonReleased(btn1) == true) { + setButtonStatus(btn1, false); sendViscaPacket(zoomStop, sizeof(zoomStop)); } - if((bool) digitalRead(BTN2) == true) { - uint8_t zoomDirSpeed = (uint8_t) 0x20 + zoomSpeed; - zoom[4] = zoomDirSpeed; - sendViscaPacket(zoom, sizeof(zoom)); + + int btn2 = buttons[1]; + if(buttonPressed(btn2) == true) { + setButtonStatus(btn2, true); + sendZoomPacket(0x30, zoomSpeed); + } else if(buttonReleased(btn2) == true) { + setButtonStatus(btn2, false); + sendViscaPacket(zoomStop, sizeof(zoomStop)); } - - if((bool) digitalRead(BTN3) == true) { - uint8_t zoomDirSpeed = (uint8_t) 0x30 + zoomSpeed; - zoom[4] = zoomDirSpeed; - sendViscaPacket(zoom, sizeof(zoom)); + + int btn5 = buttons[4]; + if(buttonPressed(btn5) == true) { + setButtonStatus(btn5, true); + initCameras(); + } else if(buttonReleased(btn5) == true) { + setButtonStatus(btn5, false); } } } +bool buttonPressed(uint8_t button) { + return getCurrentButtonStatus(button) == true && getPreviousButtonStatus(button) == false; +} + +bool buttonReleased(uint8_t button) { + return getCurrentButtonStatus(button) == false && getPreviousButtonStatus(button) == true; +} + +bool getCurrentButtonStatus(uint8_t button) { + return (bool) bitRead(buttonCurrentStatus, button); +} + +void sendZoomPacket(byte zoomDir, int zoomSpeed) { + uint8_t zoomDirSpeed = (uint8_t) zoomDir + zoomSpeed; + zoom[4] = zoomDirSpeed; + sendViscaPacket(zoom, sizeof(zoom)); +} + +bool getPreviousButtonStatus(uint8_t input) { + return (bool) bitRead(buttonPreviousStatus, input); +} + +void setButtonStatus(uint8_t input, bool status) { + bitWrite(buttonPreviousStatus, input, (int) status); +} + void processPan(int pan) { if(pan < ptLow || ptHight < pan) { uint8_t panSpeed; @@ -227,18 +217,6 @@ void processTilt(int tilt) { } } -void handleButton(uint8_t input, uint8_t bitPosition) { - bool previousStatus = (bool) bitRead(buttonStatus, bitPosition); - bool currentStatus = (bool) digitalRead(input); - if(previousStatus != currentStatus) { - if(currentStatus == true) { - bitWrite(buttonStatus, bitPosition, (int) currentStatus); - } else { - - } - } -} - void toggleFocusControl() { sendViscaPacket(focusModeInq, sizeof(focusModeInq)); delay(100); @@ -268,7 +246,6 @@ void sendViscaPacket(byte* packet, int byteSize, bool echoCommand, bool sendPack visca.write(packet[i]); } } -// visca.write(byte 0xFF); if(echoCommand == true) { Serial.println(); }