Browse Source

More restructuring to edit code in a CLion/platform.io environment.

I wanted to use a more featureful code editor.
extract-visca-communication-to-class
Ed Walker 5 years ago
parent
commit
869353aa63
8 changed files with 410 additions and 215 deletions
  1. +3
    -0
      visca_controller/.gitignore
  2. +33
    -0
      visca_controller/CMakeLists.txt
  3. +39
    -0
      visca_controller/include/README
  4. +46
    -0
      visca_controller/lib/README
  5. +15
    -0
      visca_controller/platformio.ini
  6. +235
    -203
      visca_controller/src/visca_controller.cpp
  7. +28
    -12
      visca_controller/src/visca_controller.h
  8. +11
    -0
      visca_controller/test/README

+ 3
- 0
visca_controller/.gitignore View File

@ -0,0 +1,3 @@
.pio
CMakeListsPrivate.txt
cmake-build-*/

+ 33
- 0
visca_controller/CMakeLists.txt View File

@ -0,0 +1,33 @@
# !!! WARNING !!! AUTO-GENERATED FILE, PLEASE DO NOT MODIFY IT AND USE
# https://docs.platformio.org/page/projectconf/section_env_build.html#build-flags
#
# If you need to override existing CMake configuration or add extra,
# please create `CMakeListsUser.txt` in the root of project.
# The `CMakeListsUser.txt` will not be overwritten by PlatformIO.
cmake_minimum_required(VERSION 3.13)
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_C_COMPILER_WORKS 1)
set(CMAKE_CXX_COMPILER_WORKS 1)
project("visca_controller" C CXX)
include(CMakeListsPrivate.txt)
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/CMakeListsUser.txt)
include(CMakeListsUser.txt)
endif()
add_custom_target(
Production ALL
COMMAND platformio -c clion run "$<$<NOT:$<CONFIG:All>>:-e${CMAKE_BUILD_TYPE}>"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
add_custom_target(
Debug ALL
COMMAND platformio -c clion run --target debug "$<$<NOT:$<CONFIG:All>>:-e${CMAKE_BUILD_TYPE}>"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
add_executable(Z_DUMMY_TARGET ${SRC_LIST})

+ 39
- 0
visca_controller/include/README View File

@ -0,0 +1,39 @@
This directory is intended for project header files.
A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.
```src/main.c
#include "header.h"
int main (void)
{
...
}
```
Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.
In C, the usual convention is to give header files names that end with `.h'.
It is most portable to use only letters, digits, dashes, and underscores in
header file names, and at most one dot.
Read more about using header files in official GCC documentation:
* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html

+ 46
- 0
visca_controller/lib/README View File

@ -0,0 +1,46 @@
This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into executable file.
The source code of each library should be placed in a an own separate directory
("lib/your_library_name/[here are source files]").
For example, see a structure of the following two libraries `Foo` and `Bar`:
|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c
and a contents of `src/main.c`:
```
#include <Foo.h>
#include <Bar.h>
int main (void)
{
...
}
```
PlatformIO Library Dependency Finder will find automatically dependent
libraries scanning project source files.
More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html

+ 15
- 0
visca_controller/platformio.ini View File

@ -0,0 +1,15 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:uno]
platform = atmelavr
board = uno
framework = arduino
monitor_speed = 115200

+ 235
- 203
visca_controller/src/visca_controller.cpp View File

@ -1,276 +1,308 @@
#include "visca_controller.h"
//LiquidCrystal_I2C lcd(0x27,20,4);
SoftwareSerial visca(VISCARX, VISCATX);
SoftwareSerial viscaOutput(VISCARX, VISCATX);
void setup() {
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
for (uint8_t i = 4; i <= 12; i++) {
pinMode(i,INPUT);
}
visca.begin(9600);
initCameras();
Serial.println("Started");
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
for (uint8_t i = 4; i <= 12; i++) {
pinMode(i,INPUT);
}
viscaOutput.begin(9600);
initCameras();
Serial.println("Started");
}
void loop() {
handleSerialControl();
receiveViscaData();
handleHardwareControl();
handleSerialControl();
receiveViscaData();
handleHardwareControl();
}
unsigned long time_now = 0;
bool panIdle = true;
bool tiltIdle = true;
int ptLow = 441;
int ptHight = 581;
bool zoomIdle = true;
int analogLowThreshold = 441;
int analogHighThreshold = 581;
int ptMaxSpeed = 5;
void handleHardwareControl() {
processPan(analogRead(PAN));
processPan(analogRead(PAN));
processTilt(analogRead(TILT));
processTilt(analogRead(TILT));
processButtons();
processZoom(analogRead(ZOOM));
processButtons();
}
void receiveViscaData() {
static byte ndx = 0;
byte rc;
while (visca.available() > 0) {
rc = visca.read();
if (rc != 0xFF) {
viscaMessage[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
for (uint8_t i = 0; i < ndx; i++) {
Serial.print("0x");
Serial.print(viscaMessage[i], HEX);
Serial.print(" ");
}
Serial.println("0xFF");
ndx = 0;
byte packet[3] = { 0x10, 0x41, 0xFF };
visca.write(packet, 3);
static byte ndx = 0;
byte rc;
while (viscaOutput.available() > 0) {
rc = viscaOutput.read();
if (rc != 0xFF) {
viscaMessage[ndx] = rc;
ndx++;
if (ndx >= maxViscaMessageSize) {
ndx = maxViscaMessageSize - 1;
}
}
else {
if(DEBUG_VISCA == 1) {
for (uint8_t i = 0; i < ndx; i++) {
Serial.print("0x");
Serial.print(viscaMessage[i], HEX);
Serial.print(" ");
}
Serial.println("0xFF");
}
ndx = 0;
byte packet[3] = { 0x10, 0x41, 0xFF };
viscaOutput.write(packet, 3);
}
}
}
}
void handleSerialControl() {
if (Serial.available() > 0)
{
char inChar = Serial.read(); // read incoming serial data:
switch(inChar) {
// General/Toggles
case '1':
initCameras();
break;
case '2':
toggleFocusControl();
break;
case '8':
sendViscaPacket(callLedOn, sizeof(callLedOn));
break;
case '9':
sendViscaPacket(callLedBlink, sizeof(callLedBlink));
break;
case '0':
sendViscaPacket(callLedOff, sizeof(callLedOff));
break;
if (Serial.available() > 0)
{
char inChar = Serial.read(); // read incoming serial data:
switch(inChar) {
case '1':
initCameras();
break;
case '2':
toggleFocusControl();
break;
case '8':
sendViscaPacket(callLedOn, sizeof(callLedOn));
break;
case '9':
sendViscaPacket(callLedBlink, sizeof(callLedBlink));
break;
case '0':
sendViscaPacket(callLedOff, sizeof(callLedOff));
break;
}
}
}
}
void processButtons() {
int globalSpeed = analogRead(ZOOM);
int zoomSpeed = map(globalSpeed, 0, 1023, 0, 15);
if(millis() > time_now + 100) {
time_now = millis();
collectCurrentButtonStatus();
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));
}
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));
}
int btn5 = buttons[4];
if(buttonPressed(btn5) == true) {
setButtonStatus(btn5, true);
initCameras();
} else if(buttonReleased(btn5) == true) {
setButtonStatus(btn5, false);
int globalSpeed = analogRead(AUX1);
int buttonZoomSpeed = map(globalSpeed, 0, 1023, 0, 15);
if(millis() > time_now + 100) {
time_now = millis();
collectCurrentButtonStatus();
int btn1 = buttons[0];
if(buttonPressed(btn1) == true) {
setButtonStatus(btn1, true);
sendZoomPacket(0x20, buttonZoomSpeed);
} else if(buttonReleased(btn1) == true) {
setButtonStatus(btn1, false);
sendViscaPacket(zoomStop, sizeof(zoomStop));
}
int btn2 = buttons[1];
if(buttonPressed(btn2) == true) {
setButtonStatus(btn2, true);
sendZoomPacket(0x30, buttonZoomSpeed);
} else if(buttonReleased(btn2) == true) {
setButtonStatus(btn2, false);
sendViscaPacket(zoomStop, sizeof(zoomStop));
}
int btn5 = buttons[4];
if(buttonPressed(btn5) == true) {
setButtonStatus(btn5, true);
initCameras();
} else if(buttonReleased(btn5) == true) {
setButtonStatus(btn5, false);
}
}
}
}
void collectCurrentButtonStatus() {
for (int i = 0; i < sizeof(buttons) / sizeof (buttons [0]); i++) {
bitWrite(buttonCurrentStatus, buttons[i], (int) digitalRead(buttons[i]));
}
for (int i = 0; i < (int) (sizeof(buttons) / sizeof (buttons [0])); i++) {
bitWrite(buttonCurrentStatus, buttons[i], (int) digitalRead(buttons[i]));
}
}
bool buttonPressed(uint8_t button) {
return getCurrentButtonStatus(button) == true && getPreviousButtonStatus(button) == false;
return getCurrentButtonStatus(button) == true && getPreviousButtonStatus(button) == false;
}
bool buttonReleased(uint8_t button) {
return getCurrentButtonStatus(button) == false && getPreviousButtonStatus(button) == true;
return getCurrentButtonStatus(button) == false && getPreviousButtonStatus(button) == true;
}
bool getCurrentButtonStatus(uint8_t button) {
return (bool) bitRead(buttonCurrentStatus, 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));
uint8_t zoomDirSpeed = (uint8_t) zoomDir + zoomSpeed;
zoomCommand[4] = zoomDirSpeed;
sendViscaPacket(zoomCommand, sizeof(zoomCommand));
}
bool getPreviousButtonStatus(uint8_t input) {
return (bool) bitRead(buttonPreviousStatus, input);
return (bool) bitRead(buttonPreviousStatus, input);
}
void setButtonStatus(uint8_t input, bool status) {
bitWrite(buttonPreviousStatus, input, (int) status);
bitWrite(buttonPreviousStatus, input, (int) status);
}
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;
void processZoom(int zoom) {
int zoomMaxSpeed = 15;
if(zoom < analogLowThreshold || analogHighThreshold < zoom) {
uint8_t zoomSpeed;
byte zoomDir;
if(zoom < analogLowThreshold) {
// Zoom Out
zoomSpeed = map(zoom, analogLowThreshold, 0, 0, zoomMaxSpeed);
zoomDir = 0x30;
} else {
// Zoom In
zoomSpeed = map(zoom, analogHighThreshold, 1018, 0, zoomMaxSpeed);
zoomDir = 0x20;
}
uint8_t zoomDirSpeed = (uint8_t) zoomDir + zoomSpeed;
if(zoomCommand[4] != zoomDirSpeed) {
zoomCommand[4] = zoomDirSpeed;
sendViscaPacket(zoomCommand, sizeof(zoomCommand));
}
zoomIdle = false;
} else {
panSpeed = map(pan, ptHight, 1018, 0, ptMaxSpeed);
// Right
panTilt[6] = 0x02;
// Stop Zoom
if(zoomIdle == false) {
sendViscaPacket(zoomStop, sizeof(zoomStop));
zoomIdle = true;
}
}
}
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 processPan(int pan) {
if(pan < analogLowThreshold || analogHighThreshold < pan) {
uint8_t panSpeed;
if(pan < analogLowThreshold) {
// Left
panSpeed = map(pan, analogLowThreshold, 0, 0, ptMaxSpeed);
panTilt[6] = 0x01;
} else {
panSpeed = map(pan, analogHighThreshold, 1018, 0, ptMaxSpeed);
// Right
panTilt[6] = 0x02;
}
if(panTilt[4] != panSpeed) {
panTilt[4] = panSpeed;
sendViscaPacket(panTilt, sizeof(panTilt));
}
panIdle = false;
} else {
// Stop Pan
panTilt[4] = 0x00;
panTilt[6] = 0x03;
if(panIdle == false) {
sendViscaPacket(panTilt, sizeof(panTilt));
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;
if(tilt < analogLowThreshold || analogHighThreshold < tilt) {
uint8_t tiltSpeed;
if(tilt < analogLowThreshold) {
// Down
tiltSpeed = map(tilt, analogLowThreshold, 0, 0, ptMaxSpeed);
panTilt[7] = 0x02;
} else {
// Up
tiltSpeed = map(tilt, analogHighThreshold, 1018, 0, ptMaxSpeed);
panTilt[7] = 0x01;
}
if(panTilt[5] != tiltSpeed) {
panTilt[5] = tiltSpeed;
sendViscaPacket(panTilt, sizeof(panTilt));
}
tiltIdle = false;
} else {
// Up
tiltSpeed = map(tilt, ptHight, 1018, 0, ptMaxSpeed);
panTilt[7] = 0x01;
// Stop Tilt
panTilt[5] = 0x00;
panTilt[7] = 0x03;
if(tiltIdle == false) {
sendViscaPacket(panTilt, sizeof(panTilt));
tiltIdle = true;
}
}
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;
}
}
}
void toggleFocusControl() {
sendViscaPacket(focusModeInq, sizeof(focusModeInq));
delay(100);
receiveViscaData();
Serial.print("Current Focus Status: ");
if(viscaMessage[2] == 2) {
Serial.println("Auto, Toggling to manual");
sendViscaPacket(focusManual, sizeof(focusManual));
} else {
Serial.println("Manual, Toggling to auto");
sendViscaPacket(focusAuto, sizeof(focusAuto));
}
sendViscaPacket(focusModeInq, sizeof(focusModeInq));
delay(100);
receiveViscaData();
Serial.print("Current Focus Status: ");
if(viscaMessage[2] == 2) {
Serial.println("Auto, Toggling to manual");
sendViscaPacket(focusManual, sizeof(focusManual));
} else {
Serial.println("Manual, Toggling to auto");
sendViscaPacket(focusAuto, sizeof(focusAuto));
}
}
void sendViscaPacket(byte* packet, int byteSize, bool echoCommand, bool sendPacket) {
if(echoCommand == true) {
Serial.print("Sending:");
}
for (int i = 0; i < byteSize; i++)
{
if(echoCommand == true) {
Serial.print(" 0x");
Serial.print(packet[i], HEX);
void sendViscaPacket(byte *packet, int byteSize) {
if(DEBUG_VISCA == 1) {
Serial.print("Sending:");
}
for (int i = 0; i < byteSize; i++)
{
if(DEBUG_VISCA == 1) {
Serial.print(" 0x");
Serial.print(packet[i], HEX);
}
viscaOutput.write(packet[i]);
}
if(DEBUG_VISCA == 1) {
Serial.println();
}
if(sendPacket == true) {
visca.write(packet[i]);
}
}
if(echoCommand == true) {
Serial.println();
}
}
void initCameras() {
//Send Address command
Serial.println("Setting addresses...");
sendViscaPacket(address_command, sizeof(address_command));
delay(delayTime); //delay to allow camera time for next command
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
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
receiveViscaData();
//Send Address command
Serial.println("Setting addresses...");
sendViscaPacket(address_command, sizeof(address_command));
delay(delayTime); //delay to allow camera time for next command
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
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
receiveViscaData();
}

+ 28
- 12
visca_controller/src/visca_controller.h View File

@ -2,21 +2,21 @@
#define VISCACONTROLLER
#include <Arduino.h>
#include <EEPROM.h>
//#include <EEPROM.h>
//#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
// Pin assignments from arduno shield.
// Toggle for echoing VISCA commands sent/received over serial monitor.
#define DEBUG_VISCA 1
// Pin assignments from arduno shield.
// Analog inputs
#define PAN 0
#define TILT 1
#define ZOOM 2
#define AUX1 3
#define AUX2 4
#define AUX3 5
// Serial I/O for VISCA
// Serial I/O for VISCA communication
#define VISCARX 2
#define VISCATX 3
@ -45,8 +45,8 @@ int buttons[] = {
};
const byte numChars = 16;
byte viscaMessage[numChars];
const byte maxViscaMessageSize = 16;
byte viscaMessage[maxViscaMessageSize];
byte buttonPreviousStatus = 0x00;
byte buttonCurrentStatus = 0x00;
@ -68,12 +68,12 @@ byte panTiltPosReq[5] = { 0x81, 0x09, 0x06, 0x12, 0xff }; // Resp: y0 50 0p 0q 0
// Zoom
// Tele: 8x 01 04 07 2p ff
// Wide: 8x 01 04 07 2p ff
byte zoom[6] = { 0x81, 0x01, 0x04, 0x07, 0x2F, 0xff }; // 8x 01 04 07 2p ff
byte zoomTele[6] = { 0x81, 0x01, 0x04, 0x07, 0x2F, 0xff }; // 8x 01 04 07 2p ff
byte zoomCommand[6] = {0x81, 0x01, 0x04, 0x07, 0x2F, 0xff }; // 8x 01 04 07 2p ff
byte zoomTele[6] = { 0x81, 0x01, 0x04, 0x07, 0x2F, 0xff }; // 8x 01 04 07 2p ff
byte zoomWide[6] = { 0x81, 0x01, 0x04, 0x07, 0x3F, 0xff }; // 8x 01 04 07 3p ff
byte zoomStop[6] = { 0x81, 0x01, 0x04, 0x07, 0x00, 0xff };
byte zoomDirect[9] = { 0x81, 0x01, 0x04, 0x47, 0x00, 0x00, 0x00, 0x00, 0xff }; // 8x 01 04 47 0p 0q 0r 0s ff pqrs: zoom position
byte zoomPosReq[5] = { 0x81, 0x09, 0x04, 0x47, 0xff }; // Resp: y0 50 0p 0q 0r 0s ff
byte zoomDirect[9] = { 0x81, 0x01, 0x04, 0x47, 0x00, 0x00, 0x00, 0x00, 0xff }; // 8x 01 04 47 0p 0q 0r 0s ff pqrs: zoomCommand position
byte zoomPosReq[5] = { 0x81, 0x09, 0x04, 0x47, 0xff }; // Resp: y0 50 0p 0q 0r 0s ff
// Focus
@ -107,6 +107,22 @@ byte callLedBlink[6] = { 0x81, 0x01, 0x33, 0x01, 0x02, 0xff};
int delayTime = 500; //Time between commands
void sendViscaPacket(byte* packet, int byteSize, bool echoCommand = true, bool sendPacket = true);
void sendViscaPacket(byte *packet, int byteSize);
void handleHardwareControl();
void receiveViscaData();
void handleSerialControl();
void processButtons();
void collectCurrentButtonStatus();
bool buttonPressed(uint8_t button);
bool buttonReleased(uint8_t button);
bool getCurrentButtonStatus(uint8_t button);
void sendZoomPacket(byte zoomDir, int zoomSpeed);
bool getPreviousButtonStatus(uint8_t input);
void setButtonStatus(uint8_t input, bool status);
void processPan(int pan);
void processTilt(int tilt);
void processZoom(int zoom);
void toggleFocusControl();
void initCameras();
#endif

+ 11
- 0
visca_controller/test/README View File

@ -0,0 +1,11 @@
This directory is intended for PlatformIO Unit Testing and project tests.
Unit Testing is a software testing method by which individual units of
source code, sets of one or more MCU program modules together with associated
control data, usage procedures, and operating procedures, are tested to
determine whether they are fit for use. Unit testing finds problems early
in the development cycle.
More information about PlatformIO Unit Testing:
- https://docs.platformio.org/page/plus/unit-testing.html

Loading…
Cancel
Save