Skip to content

Commit

Permalink
Added skyzone onboard ESP32-PICO target (#71)
Browse files Browse the repository at this point in the history
* Added skyzone onboard ESP32-PICO target

* Pass current time into module loop to avoid extra calls to millis()

* Rename DIY skyzone target to avoid confusion

* Undef redefined boot delay to avoid throwing compiler warning
  • Loading branch information
wvarty authored Aug 29, 2022
1 parent 7101075 commit 394eff5
Show file tree
Hide file tree
Showing 19 changed files with 274 additions and 30 deletions.
3 changes: 2 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ extra_configs =
targets/betafpv_tx.ini
targets/dupletx_tx.ini
targets/radiomaster_tx.ini
targets/hdzero.ini
targets/hdzero.ini
targets/skyzone.ini
7 changes: 5 additions & 2 deletions src/Vrx_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include "fusion.h"
#elif defined(HDZERO_BACKPACK)
#include "hdzero.h"
#elif defined(SKYZONE_MSP_BACKPACK)
#include "skyzone_msp.h"
#endif

/////////// DEFINES ///////////
Expand Down Expand Up @@ -98,6 +100,8 @@ VrxBackpackConfig config;
Fusion vrxModule;
#elif defined(HDZERO_BACKPACK)
HDZero vrxModule(&Serial);
#elif defined(SKYZONE_MSP_BACKPACK)
SkyzoneMSP vrxModule(&Serial);
#endif

/////////// FUNCTION DEFS ///////////
Expand Down Expand Up @@ -202,15 +206,13 @@ void ProcessMSPPacket(mspPacket_t *packet)
break;
case MSP_ELRS_BACKPACK_SET_RECORDING_STATE:
DBGLN("Processing MSP_ELRS_BACKPACK_SET_RECORDING_STATE...");
#if defined(HDZERO_BACKPACK)
{
uint8_t state = packet->readByte();
uint8_t lowByte = packet->readByte();
uint8_t highByte = packet->readByte();
uint16_t delay = lowByte | highByte << 8;
vrxModule.SetRecordingState(state, delay);
}
#endif
break;
default:
DBGLN("Unknown command from ESPNOW");
Expand Down Expand Up @@ -405,6 +407,7 @@ void loop()
uint32_t now = millis();

devicesUpdate(now);
vrxModule.Loop(now);

#if defined(PLATFORM_ESP8266) || defined(PLATFORM_ESP32)
// If the reboot time is set and the current time is past the reboot time then reboot.
Expand Down
2 changes: 1 addition & 1 deletion src/fusion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ GENERIC_CRC8 crsf_crc(CRSF_CRC_POLY);
void
Fusion::Init()
{
delay(VRX_BOOT_DELAY);
ModuleBase::Init();
DBGLN("Fusion backpack init complete");
}

Expand Down
5 changes: 4 additions & 1 deletion src/fusion.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#pragma once

#include "module_base.h"
#include <Arduino.h>

#undef VRX_BOOT_DELAY
#define VRX_BOOT_DELAY 1000

#define VRX_UART_BAUD 500000 // fusion uses 500k baud between the ESP8266 and the STM32

const uint16_t frequencyTable[48] = {
Expand All @@ -14,7 +17,7 @@ const uint16_t frequencyTable[48] = {
5333, 5373, 5413, 5453, 5493, 5533, 5573, 5613 // L
};

class Fusion
class Fusion : public ModuleBase
{
public:
void Init();
Expand Down
2 changes: 1 addition & 1 deletion src/hdzero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ HDZero::HDZero(Stream *port)
void
HDZero::Init()
{
delay(VRX_BOOT_DELAY);
ModuleBase::Init();
}

void
Expand Down
5 changes: 4 additions & 1 deletion src/hdzero.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

#include "msp.h"
#include "msptypes.h"
#include "module_base.h"
#include <Arduino.h>

#undef VRX_BOOT_DELAY
#define VRX_BOOT_DELAY 7000

#define VRX_RESPONSE_TIMEOUT 500
#define VRX_UART_BAUD 115200 // hdzero uses 115k baud between the ESP8285 and the STM32

Expand All @@ -13,7 +16,7 @@
#define VRX_DVR_RECORDING_INACTIVE 0
#define VRX_DVR_RECORDING_UNKNOWN 255

class HDZero
class HDZero : public ModuleBase
{
public:
HDZero(Stream *port);
Expand Down
22 changes: 22 additions & 0 deletions src/module_base.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "module_base.h"

void
ModuleBase::Init()
{
delay(VRX_BOOT_DELAY);
}

void
ModuleBase::SendIndexCmd(uint8_t index)
{
}

void
ModuleBase::SetRecordingState(uint8_t recordingState, uint16_t delay)
{
}

void
ModuleBase::Loop(uint32_t now)
{
}
14 changes: 14 additions & 0 deletions src/module_base.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include <Arduino.h>

#define VRX_BOOT_DELAY 0

class ModuleBase
{
public:
void Init();
void SendIndexCmd(uint8_t index);
void SetRecordingState(uint8_t recordingState, uint16_t delay);
void Loop(uint32_t now);
};
2 changes: 1 addition & 1 deletion src/rapidfire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
void
Rapidfire::Init()
{
delay(VRX_BOOT_DELAY);
ModuleBase::Init();

EnableSPIMode(); // https://github.com/ExpressLRS/ExpressLRS/pull/1489 & https://github.com/ExpressLRS/Backpack/pull/65

Expand Down
4 changes: 3 additions & 1 deletion src/rapidfire.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#pragma once

#include "module_base.h"
#include <Arduino.h>

#undef VRX_BOOT_DELAY
#define VRX_BOOT_DELAY 2000

#define BIT_BANG_FREQ 1000
Expand All @@ -13,7 +15,7 @@
#define RF_API_CHANNEL_CMD 0x43 // 'C'
#define RF_API_BAND_CMD 0x42 // 'B'

class Rapidfire
class Rapidfire : public ModuleBase
{
public:
void Init();
Expand Down
2 changes: 2 additions & 0 deletions src/rx5808.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
void
RX5808::Init()
{
ModuleBase::Init();

pinMode(PIN_MOSI, INPUT);
pinMode(PIN_CLK, INPUT);
pinMode(PIN_CS, INPUT);
Expand Down
3 changes: 2 additions & 1 deletion src/rx5808.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "module_base.h"
#include <Arduino.h>

#define BIT_BANG_FREQ 10000
Expand Down Expand Up @@ -32,7 +33,7 @@ const uint16_t frequencyTable[48] = {
5333, 5373, 5413, 5453, 5493, 5533, 5573, 5613 // L
};

class RX5808
class RX5808 : public ModuleBase
{
public:
void Init();
Expand Down
129 changes: 129 additions & 0 deletions src/skyzone_msp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#include "skyzone_msp.h"
#include "logging.h"
#include <Arduino.h>

SkyzoneMSP::SkyzoneMSP(Stream *port)
{
m_port = port;
}

void
SkyzoneMSP::Init()
{
ModuleBase::Init();
m_delay = 0;
}

void
SkyzoneMSP::SendIndexCmd(uint8_t index)
{
uint8_t retries = 3;
while (GetChannelIndex() != index && retries > 0)
{
SetChannelIndex(index);
retries--;
}
}

uint8_t
SkyzoneMSP::GetChannelIndex()
{
MSP msp;
mspPacket_t* packet = new mspPacket_t;
packet->reset();
packet->makeCommand();
packet->function = MSP_ELRS_BACKPACK_GET_CHANNEL_INDEX;

// Send request, then wait for a response back from the VRX
bool receivedResponse = msp.awaitPacket(packet, m_port, VRX_RESPONSE_TIMEOUT);

if (receivedResponse)
{
packet = msp.getReceivedPacket();
return packet->readByte();
}

DBGLN("Skyzone module: Exceeded timeout while waiting for channel index response");
return CHANNEL_INDEX_UNKNOWN;
}

void
SkyzoneMSP::SetChannelIndex(uint8_t index)
{
MSP msp;
mspPacket_t packet;
packet.reset();
packet.makeCommand();
packet.function = MSP_ELRS_BACKPACK_SET_CHANNEL_INDEX;
packet.addByte(index); // payload

msp.sendPacket(&packet, m_port);
}

uint8_t
SkyzoneMSP::GetRecordingState()
{
MSP msp;
mspPacket_t* packet = new mspPacket_t;
packet->reset();
packet->makeCommand();
packet->function = MSP_ELRS_BACKPACK_GET_RECORDING_STATE;

// Send request, then wait for a response back from the VRX
bool receivedResponse = msp.awaitPacket(packet, m_port, VRX_RESPONSE_TIMEOUT);

if (receivedResponse)
{
packet = msp.getReceivedPacket();
return packet->readByte() ? VRX_DVR_RECORDING_ACTIVE : VRX_DVR_RECORDING_INACTIVE;
}

DBGLN("Skyzone module: Exceeded timeout while waiting for recording state response");
return VRX_DVR_RECORDING_UNKNOWN;
}

void
SkyzoneMSP::SetRecordingState(uint8_t recordingState, uint16_t delay)
{
DBGLN("SetRecordingState = %d delay = %d", recordingState, delay);

m_recordingState = recordingState;
m_delay = delay * 1000; // delay is in seconds, convert to milliseconds
m_delayStartMillis = millis();

if (m_delay == 0)
{
SendRecordingState();
}
}

void
SkyzoneMSP::SendRecordingState()
{
DBGLN("SendRecordingState = %d delay = %d", m_recordingState, m_delay);

MSP msp;
mspPacket_t packet;
packet.reset();
packet.makeCommand();
packet.function = MSP_ELRS_BACKPACK_SET_RECORDING_STATE;
packet.addByte(m_recordingState);
packet.addByte(m_delay & 0xFF); // delay byte 1
packet.addByte(m_delay >> 8); // delay byte 2

msp.sendPacket(&packet, m_port);
}

void
SkyzoneMSP::Loop(uint32_t now)
{
// Handle delay timer for SendRecordingState()
if (m_delay != 0)
{
if (now - m_delayStartMillis >= m_delay)
{
SendRecordingState();
m_delay = 0;
}
}
}
38 changes: 38 additions & 0 deletions src/skyzone_msp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once

#include "msp.h"
#include "msptypes.h"
#include "module_base.h"
#include <Arduino.h>

#undef VRX_BOOT_DELAY
#define VRX_BOOT_DELAY 2000

#define VRX_RESPONSE_TIMEOUT 500
#define VRX_UART_BAUD 115200 // skyzone uses 115k baud between the ESP32-PICO and their MCU

#define CHANNEL_INDEX_UNKNOWN 255
#define VRX_DVR_RECORDING_ACTIVE 1
#define VRX_DVR_RECORDING_INACTIVE 0
#define VRX_DVR_RECORDING_UNKNOWN 255

class SkyzoneMSP : public ModuleBase
{
public:
SkyzoneMSP(Stream *port);
void Init();
void SendIndexCmd(uint8_t index);
uint8_t GetChannelIndex();
void SetChannelIndex(uint8_t index);
uint8_t GetRecordingState();
void SetRecordingState(uint8_t recordingState, uint16_t delay);
void Loop(uint32_t now);

private:
void SendRecordingState();

Stream *m_port;
uint8_t m_recordingState;
uint16_t m_delay;
uint32_t m_delayStartMillis;
};
2 changes: 2 additions & 0 deletions src/steadyview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
void
SteadyView::Init()
{
ModuleBase::Init();

pinMode(PIN_MOSI, OUTPUT);
pinMode(PIN_CLK, OUTPUT);
pinMode(PIN_CS, OUTPUT);
Expand Down
3 changes: 2 additions & 1 deletion src/steadyview.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "module_base.h"
#include <Arduino.h>

#define BIT_BANG_FREQ 10000
Expand Down Expand Up @@ -37,7 +38,7 @@ typedef enum {
ModeDiversity
} videoMode_t;

class SteadyView
class SteadyView : public ModuleBase
{
public:
void Init();
Expand Down
Loading

0 comments on commit 394eff5

Please sign in to comment.