From 3d62d79573dd6dd0a8dbb56f3e07506bd1931119 Mon Sep 17 00:00:00 2001 From: Ed Walker Date: Sun, 17 Jan 2021 14:23:43 -0800 Subject: [PATCH] Some cleanup. --- visca-controller/visca-controller.h | 2 +- visca-controller/visca-controller.ino | 188 ++++++++++++-------------- 2 files changed, 87 insertions(+), 103 deletions(-) diff --git a/visca-controller/visca-controller.h b/visca-controller/visca-controller.h index 2b5ce5b..e45e662 100644 --- a/visca-controller/visca-controller.h +++ b/visca-controller/visca-controller.h @@ -86,7 +86,7 @@ byte wbTableDirect[9] = { 0x81, 0x01, 0x04, 0x75, 0x00, 0x00, 0x0, 0x00, 0xff }; // Config byte address_command[4] = { 0x88, 0x30, 0x01, 0xFF }; // Sets camera address (Needed for Daisy Chaining) byte if_clear[5] = { 0x88, 0x01, 0x00, 0x01, 0xFF }; // Checks to see if communication line is clear -byte ir_off[6] = { 0x81, 0x01, 0x06, 0x09, 0x03, 0xff }; // Turn off IR control (required for speed control of Pan/Tilt) +byte ir_off[6] = { 0x81, 0x01, 0x06, 0x09, 0x03, 0xff }; // Turn off IR control (required for speed control of Pan/Tilt on TelePresence cameras) byte callLedOn[6] = { 0x81, 0x01, 0x33, 0x01, 0x01, 0xff}; byte callLedOff[6] = { 0x81, 0x01, 0x33, 0x01, 0x00, 0xff}; byte callLedBlink[6] = { 0x81, 0x01, 0x33, 0x01, 0x02, 0xff}; diff --git a/visca-controller/visca-controller.ino b/visca-controller/visca-controller.ino index 19989b3..da92ea9 100644 --- a/visca-controller/visca-controller.ino +++ b/visca-controller/visca-controller.ino @@ -20,13 +20,28 @@ void setup() { } void loop() { - readSerial(); - receiveData(); - readButtons(); - // int talentDimmer = map(analogRead(TALENT_DIMMER_PIN), 0, 1023, 0, 255); + handleSerialControl(); + receiveViscaData(); + handleHardwareControl(); } -void receiveData() { +unsigned long time_now = 0; +bool panIdle = true; +bool tiltIdle = true; +int ptLow = 441; +int ptHight = 581; +int ptMaxSpeed = 5; + +void handleHardwareControl() { + processPan(analogRead(PAN)); + + processTilt(analogRead(TILT)); + + processButtons(); +} + + +void receiveViscaData() { static byte ndx = 0; byte rc; while (visca.available() > 0) { @@ -54,7 +69,7 @@ void receiveData() { } -void readSerial() { +void handleSerialControl() { if (Serial.available() > 0) { char inChar = Serial.read(); // read incoming serial data: @@ -132,114 +147,84 @@ void readSerial() { } } -unsigned long time_now = 0; -unsigned long pt_now = 0; -int lastZoomPos; -int lastZoom; -bool panIdle = true; -bool tiltIdle = true; -int ptLow = 441; -int ptHight = 581; -void readButtons() { +void processButtons() { int globalSpeed = analogRead(ZOOM); int zoomSpeed = map(globalSpeed, 0, 1023, 0, 15); -// int ptMaxSpeed = map(globalSpeed, 0, 1023, 0, 7); - int ptMaxSpeed = 5; - int pan = analogRead(PAN); - int tilt = analogRead(TILT); - - if(pan < ptLow || ptHight < pan) { - uint8_t panSpeed; - if(pan < ptLow) { - // Left - panSpeed = map(pan, ptLow, 0, 0, ptMaxSpeed); - panTilt[6] = 0x01; - } else { - panSpeed = map(pan, ptHight, 1018, 0, ptMaxSpeed); - // Right - panTilt[6] = 0x02; - } - - if(panTilt[4] != panSpeed) { - panTilt[4] = panSpeed; - sendViscaPacket(panTilt, sizeof(panTilt), true); - } - panIdle = false; - } else { - // Stop Pan - panTilt[4] = 0x00; - panTilt[6] = 0x03; - if(panIdle == false) { - sendViscaPacket(panTilt, sizeof(panTilt), true); - panIdle = true; - } - } - - if(tilt < ptLow || ptHight < tilt) { - uint8_t tiltSpeed; - if(tilt < ptLow) { - // Down - tiltSpeed = map(tilt, ptLow, 0, 0, ptMaxSpeed); - panTilt[7] = 0x02; - } else { - // Up - tiltSpeed = map(tilt, ptHight, 1018, 0, ptMaxSpeed); - panTilt[7] = 0x01; - } - - if(panTilt[5] != tiltSpeed) { - panTilt[5] = tiltSpeed; - sendViscaPacket(panTilt, sizeof(panTilt), true); - } - tiltIdle = false; - } else { - // Stop Tilt - panTilt[5] = 0x00; - panTilt[7] = 0x03; - if(tiltIdle == false) { - sendViscaPacket(panTilt, sizeof(panTilt), true); - tiltIdle = true; - } - } - - - if(millis() > time_now + 100) { time_now = millis(); -// if( -// if(zoomPos < lastZoomPos - 2 || lastZoomPos + 2 < zoomPos) { -// lastZoomPos = zoomPos; -// zoomPos = map(zoomPos, 0, 1023, 0, 2883); -// byte mask = 0xF; -// Serial.println(zoomPos); -// zoomDirect[7] = zoomPos & mask; -// zoomDirect[6] = zoomPos >> 4 & mask; -// zoomDirect[5] = zoomPos >> 8 & mask; -// zoomDirect[4] = zoomPos >> 12 & mask; -// sendViscaPacket(zoomDirect, sizeof(zoomDirect), true); -// } - if((bool) digitalRead(BTN1) == true) { - sendViscaPacket(zoomPosReq, sizeof(zoomPosReq), false); + sendViscaPacket(zoomStop, sizeof(zoomStop)); } - - if((bool) digitalRead(BTN3) == true) { + if((bool) digitalRead(BTN2) == true) { uint8_t zoomDirSpeed = (uint8_t) 0x20 + zoomSpeed; zoom[4] = zoomDirSpeed; sendViscaPacket(zoom, sizeof(zoom)); } - if((bool) digitalRead(BTN5) == true) { + if((bool) digitalRead(BTN3) == true) { uint8_t zoomDirSpeed = (uint8_t) 0x30 + zoomSpeed; zoom[4] = zoomDirSpeed; sendViscaPacket(zoom, sizeof(zoom)); } + } +} -// Serial.println((bool) digitalRead(BTN7)); -// if((bool) digitalRead(BTN7) == false) { -// sendViscaPacket(zoomStop, sizeof(zoomStop)); -// } +void processPan(int pan) { + if(pan < ptLow || ptHight < pan) { + uint8_t panSpeed; + if(pan < ptLow) { + // Left + panSpeed = map(pan, ptLow, 0, 0, ptMaxSpeed); + panTilt[6] = 0x01; + } else { + panSpeed = map(pan, ptHight, 1018, 0, ptMaxSpeed); + // Right + panTilt[6] = 0x02; + } + + if(panTilt[4] != panSpeed) { + panTilt[4] = panSpeed; + sendViscaPacket(panTilt, sizeof(panTilt), true); + } + panIdle = false; + } else { + // Stop Pan + panTilt[4] = 0x00; + panTilt[6] = 0x03; + if(panIdle == false) { + sendViscaPacket(panTilt, sizeof(panTilt), true); + panIdle = true; + } + } +} + +void processTilt(int tilt) { + if(tilt < ptLow || ptHight < tilt) { + uint8_t tiltSpeed; + if(tilt < ptLow) { + // Down + tiltSpeed = map(tilt, ptLow, 0, 0, ptMaxSpeed); + panTilt[7] = 0x02; + } else { + // Up + tiltSpeed = map(tilt, ptHight, 1018, 0, ptMaxSpeed); + panTilt[7] = 0x01; + } + + if(panTilt[5] != tiltSpeed) { + panTilt[5] = tiltSpeed; + sendViscaPacket(panTilt, sizeof(panTilt), true); + } + tiltIdle = false; + } else { + // Stop Tilt + panTilt[5] = 0x00; + panTilt[7] = 0x03; + if(tiltIdle == false) { + sendViscaPacket(panTilt, sizeof(panTilt), true); + tiltIdle = true; + } } } @@ -258,7 +243,7 @@ void handleButton(uint8_t input, uint8_t bitPosition) { void toggleFocusControl() { sendViscaPacket(focusModeInq, sizeof(focusModeInq)); delay(100); - receiveData(); + receiveViscaData(); Serial.print("Current Focus Status: "); if(viscaMessage[2] == 2) { Serial.println("Auto, Toggling to manual"); @@ -274,7 +259,6 @@ void sendViscaPacket(byte* packet, int byteSize, bool echoCommand, bool sendPack if(echoCommand == true) { Serial.print("Sending:"); } -// visca.write(byte 0x81); for (int i = 0; i < byteSize; i++) { if(echoCommand == true) { @@ -296,17 +280,17 @@ void initCameras() { Serial.println("Setting addresses..."); sendViscaPacket(address_command, sizeof(address_command)); delay(delayTime); //delay to allow camera time for next command - receiveData(); + receiveViscaData(); // Turn off IR control Serial.println("Disabling IR control..."); sendViscaPacket(ir_off, sizeof(ir_off)); delay(delayTime); //delay to allow camera time for next command - receiveData(); + receiveViscaData(); //Send IF_clear command Serial.println("Sending IF_Clear..."); sendViscaPacket(if_clear, sizeof(if_clear)); delay(delayTime); //delay to allow camera time for next command - receiveData(); + receiveViscaData(); }