ubitx-iop/ubitx_iop/TxSwitch.h

250 lines
6.2 KiB
C++

//======================================================================
// TxSwitch.h
//======================================================================
#ifndef __TxSwitch_h__
#define __TxSwitch_h__
#include <Bounce2.h>
#define BOUNCE_WITH_PROMPT_DETECTION
#include "ubitx_iop.h"
#include "rig.h"
#define MIC_PTT_PIN 21
#define LINE_PTT_PIN 20
//----------------------------------------------------------------------
// ITxSwitch
//
// Interface for transmit (PTT, Key) switches. onPress() is called
// before transmission begins, and should return true if transmission
// should start. onRelease() is called before transmission ends, and
// should return true if transmission should in fact end.
//----------------------------------------------------------------------
class ITxSwitch
{
public:
virtual ~ITxSwitch() {}
// Called before beginning transmit; if false, transmit aborts before starting.
virtual bool onPress(IMode* m) = 0;
// Called before stopping tranmit; if false, transmit continues.
virtual bool onRelease(IMode* m) = 0;
void press(IMode* m, bool output_enable=true) {
if (onPress(m)) {
USBDEBUG("PTT pressed");
m->tx();
if (output_enable) {
setKeyDown(); // NOTE: could still make this more configurable...
}
}
}
void release(IMode* m, bool output_enable=true) {
if (onRelease(m)) {
USBDEBUG("PTT released");
if (output_enable) {
setKeyUp(); // NOTE: could still make this more configurable...
}
m->rx();
}
}
};
//----------------------------------------------------------------------
// CATSwitch
//
// Implementation of the ITxSwitch interface for the CAT control. In
// general, CAT cannot override any existing transmission, and cannot
// terminate an existing transmission.
//----------------------------------------------------------------------
class CATSwitch : public ITxSwitch
{
public:
CATSwitch(): _transmitting(false) {}
virtual bool onPress(IMode* m) {
// If another transmission is already occuring, abort... CAT can't
// interrupt transmissions already ongoing.
if (m->isRx()) {
USBDEBUG("CAT PTT pressed");
_transmitting = true;
return true;
} else {
return false;
}
}
virtual bool onRelease(IMode* m) {
// If CAT transmission is not occurring, abort... CAT can't stop
// transmissions initiated by other sources. We don't check if
// the mode is already transmitting, because it could be
// transmitting because of CAT.
if (_transmitting) {
USBDEBUG("CAT PTT released");
_transmitting = false;
return true;
} else {
return false;
}
}
private:
bool _transmitting; // CAT-specific transmission
};
/*
//----------------------------------------------------------------------
// MicSwitch
//
// Implementation of the ITxSwitch interface for the Mic (front panel)
// switch. Features:
// - In SSB, it will automatically select the Mic (front panel) input.
// - If already transmitting, press will have no effect.
// - If already transmitting (any mode, source), release will stop
// the transmission... a failsafe.
//----------------------------------------------------------------------
class MicSwitch : public ITxSwitch
{
public:
MicSwitch(): _ssb_mode(false) {}
inline void setSSBMode(bool flag) { _ssb_mode = flag; }
virtual bool onPress(IMode* m) {
if (m->isRx()) {
if (_ssb_mode) {
((SSBMode*)m)->setMicIn();
}
return true;
} else {
return false;
}
}
virtual bool onRelease(IMode* m) {
if (m->isTx()) {
return true;
} else {
return false;
}
}
private:
bool _ssb_mode;
};
//----------------------------------------------------------------------
// LineSwitch
//
// Implementation of the ITxSwitch interface for the Line (rear panel)
// switch. Features:
// - In SSB, it will automatically select the Line (rear panel) input.
// - If already transmitting, press will have no effect.
// - If already transmitting (any mode, source), release will stop
// the transmission... a failsafe.
//----------------------------------------------------------------------
class LineSwitch : public ITxSwitch
{
public:
LineSwitch(): _ssb_mode(false) {}
inline void setSSBMode(bool flag) { _ssb_mode = flag; }
virtual bool onPress(IMode* m) {
if (m->isRx()) {
if (_ssb_mode) {
((SSBMode*)m)->setLineIn();
}
return true;
} else {
return false;
}
}
virtual bool onRelease(IMode* m) {
if (m->isTx()) {
return true;
} else {
return false;
}
}
private:
bool _ssb_mode;
};
*/
//----------------------------------------------------------------------
// GPIOSwitch
//
// Class used to implement the physical transmit switches (i.e. the
// Mic and Line input PTTs which are connected to GPIO pins). Takes
// an object implementing the ITxSwitch interface, as well as info for
// setting up a debounced pin.
//----------------------------------------------------------------------
class GPIOSwitch : public ITxSwitch
{
public:
GPIOSwitch(bool is_mic, int pin, int msec=25): _is_mic(is_mic), _ssb_mode(false), _bounce() {
_bounce.attach(pin, INPUT_PULLUP);
_bounce.interval(msec);
}
inline void setSSBMode(bool flag) { _ssb_mode = flag; }
virtual bool onPress(IMode* m) {
if (m->isRx()) {
if (_ssb_mode) {
if (_is_mic) {
USBDEBUG("Mic PTT pressed");
((SSBMode*)m)->setMicIn();
} else {
USBDEBUG("Line PTT pressed");
((SSBMode*)m)->setLineIn();
}
}
return true;
} else {
return false;
}
}
virtual bool onRelease(IMode* m) {
if (m->isTx()) {
return true;
} else {
return false;
}
}
void update(IMode* m, bool output_enable=true) {
_bounce.update();
if (_bounce.fell()) {
press(m, output_enable);
} else if (_bounce.rose()) {
release(m, output_enable);
}
}
private:
bool _is_mic;
bool _ssb_mode;
Bounce _bounce;
};
#endif
//======================================================================
// EOF
//======================================================================