Skip to content

Commit

Permalink
Added mirror mode for repeater
Browse files Browse the repository at this point in the history
  • Loading branch information
pietwauters committed Oct 6, 2024
1 parent f2abc11 commit 520e9ba
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 40 deletions.
6 changes: 5 additions & 1 deletion FencingStateMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ void FencingStateMachine::TransmitFullStateToDisplay (RepeaterSender *TheRepeate

TheRepeater->update(this,MakeTimerEvent());

TheRepeater->update(this,EVENT_UW2F_TIMER | (m_UW2FSeconds/60)<<16 | (m_UW2FSeconds%60)<<8);


//TheRepeater->update(this,EVENT_UW2F_TIMER | (m_UW2FSeconds/60)<<16 | (m_UW2FSeconds%60)<<8);
}

void FencingStateMachine::update (UDPIOHandler *subject, uint32_t eventtype)
Expand Down Expand Up @@ -308,6 +310,7 @@ void FencingStateMachine::update (UDPIOHandler *subject, uint32_t eventtype)
card_event = m_RedCardLeft | EVENT_RED_CARD_LEFT;
StateChanged(card_event );
m_UW2FTimer.Reset();
m_UW2FSeconds = 0;
StateChanged(EVENT_UW2F_TIMER);
//m_ScoreRight++;
incrementScoreAndCheckForMinuteBreak(false);
Expand All @@ -319,6 +322,7 @@ void FencingStateMachine::update (UDPIOHandler *subject, uint32_t eventtype)
card_event = m_RedCardRight | EVENT_RED_CARD_RIGHT;
StateChanged(card_event );
m_UW2FTimer.Reset();
m_UW2FSeconds = 0;
StateChanged(EVENT_UW2F_TIMER);
//m_ScoreLeft++;
incrementScoreAndCheckForMinuteBreak(true);;
Expand Down
89 changes: 88 additions & 1 deletion RepeaterReceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <Preferences.h>
#include "network.h"
using namespace std;
#define MASK_REVERSE_COLORS 0x00000001

RepeaterReceiver::RepeaterReceiver()
{
Expand All @@ -18,6 +19,91 @@ RepeaterReceiver::~RepeaterReceiver()
//dtor
}

void changeEventMainType(uint32_t *event, uint32_t newType){
*event &= SUB_TYPE_MASK; // clear original type
*event |= newType; // set new type
}

uint32_t swapLights(const uint32_t &event) {
uint32_t result = EVENT_LIGHTS || MASK_REVERSE_COLORS;
if(event & MASK_RED)
result |= MASK_GREEN;

if(event & MASK_GREEN)
result |= MASK_RED;

if(event & MASK_WHITE_L)
result |= MASK_WHITE_R;

if(event & MASK_WHITE_R)
result |= MASK_WHITE_L;

if(event & MASK_ORANGE_L)
result |= MASK_ORANGE_R;

if(event & MASK_ORANGE_R)
result |= MASK_ORANGE_L;

return result;
}
void RepeaterReceiver::StateChanged (uint32_t event)
{
// we have to modify the event in case of mirroring
if(!m_Mirror){
notify(event);
return;
}
uint32_t event_data = event & SUB_TYPE_MASK;
uint32_t maineventtype = event & MAIN_TYPE_MASK ;
uint32_t tempevent = event;
switch(maineventtype)
{
case EVENT_LIGHTS:
tempevent = swapLights(event);
break;
case EVENT_SCORE_LEFT:
changeEventMainType(&tempevent,EVENT_SCORE_RIGHT);
break;

case EVENT_SCORE_RIGHT:
changeEventMainType(&tempevent,EVENT_SCORE_LEFT);
break;

case EVENT_PRIO:

break;

case EVENT_YELLOW_CARD_LEFT:
changeEventMainType(&tempevent,EVENT_YELLOW_CARD_RIGHT);
break;

case EVENT_YELLOW_CARD_RIGHT:
changeEventMainType(&tempevent,EVENT_YELLOW_CARD_LEFT);
break;

case EVENT_RED_CARD_LEFT:
changeEventMainType(&tempevent,EVENT_RED_CARD_RIGHT);
break;

case EVENT_RED_CARD_RIGHT:
changeEventMainType(&tempevent,EVENT_RED_CARD_LEFT);
break;


case EVENT_P_CARD:
//StateChanged(EVENT_P_CARD | m_PCardLeft | m_PCardRight << 8);
mix_t PCardInfo;
PCardInfo.theDWord = event_data;
uint8_t temp = PCardInfo.theBytes[0];
PCardInfo.theBytes[0] = PCardInfo.theBytes[1];
PCardInfo.theBytes[1] = temp;
tempevent = PCardInfo.theDWord | EVENT_P_CARD;

break;

}
notify(tempevent);
}


void RepeaterReceiver::RegisterRepeater(uint8_t *broadcastAddress)
Expand All @@ -36,6 +122,7 @@ void RepeaterReceiver::begin(esp_now_recv_cb_t theCallBack)
Preferences networkpreferences;
networkpreferences.begin("scoringdevice", false);
m_MasterPiste = networkpreferences.getInt("MasterPiste", -1);
m_Mirror = networkpreferences.getBool("MirrorLights", true);

networkpreferences.end();
// Init ESP-NOW
Expand Down Expand Up @@ -70,7 +157,7 @@ void RepeaterReceiver::begin(esp_now_recv_cb_t theCallBack)
void RepeaterReceiver::ResetWatchDog(){
m_WatchDogTriggerTime = millis() + m_WatchDogPeriod;
}

bool RepeaterReceiver::IsWatchDogTriggered(){
if(millis() > m_WatchDogTriggerTime)
{
Expand Down
4 changes: 3 additions & 1 deletion RepeaterReceiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,22 @@ class RepeaterReceiver : public Subject<RepeaterReceiver>
RepeaterReceiver(); // tickPeriod in miliseconds
/** Default destructor */
virtual ~RepeaterReceiver();
void StateChanged (uint32_t eventtype) {notify(eventtype);}
void StateChanged (uint32_t eventtype);
void RegisterRepeater(uint8_t *broadcastAddress);
void begin(esp_now_recv_cb_t theCallBack);
int32_t MasterPiste(){return m_MasterPiste;};
void StartWatchDog(long Period = FULL_STATUS_REPETITION_PERIOD*3);
void ResetWatchDog();
bool IsWatchDogTriggered();
bool Mirror(){return m_Mirror;};

protected:

private:
// private methods
int m_espnowchannel = -1;
int32_t m_MasterPiste =-1;
bool m_Mirror = false;
uint8_t m_senderAddress[6] = {0x24,0xDC,0xC3,0x45,0xCD,0xA0};
uint8_t m_broadcastAddress[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
esp_now_peer_info_t peerInfo;
Expand Down
85 changes: 53 additions & 32 deletions WS2812BLedStrip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,30 +55,39 @@ WS2812B_LedStrip::~WS2812B_LedStrip()
delete m_pixels;
}

void WS2812B_LedStrip::setRed(bool Value)
void WS2812B_LedStrip::setRed(bool Value, bool bReverse)
{
if(Value)
{
//m_pixels->fill(m_pixels->Color(0, 120, 0),0,NUMPIXELS/3);
//m_pixels->fill(m_Red,0,NUMPIXELS/2);
for(int i=0;i<64;i++)
{
uint32_t FillColor = m_Red;
if(bReverse)
FillColor = m_Green;

m_pixels->setPixelColor(i, m_Red); // Moderately bright green color.
}
}
else
{
m_pixels->fill(m_pixels->Color(0, 0, 0),0,64);
}
//m_pixels->show(); // Send the updated pixel colors to the hardware.
if(Value)
{
//m_pixels->fill(m_pixels->Color(0, 120, 0),0,NUMPIXELS/3);
//m_pixels->fill(m_Red,0,NUMPIXELS/2);
for(int i=0;i<64;i++)
{

m_pixels->setPixelColor(i, FillColor); // Moderately bright green color.
}
}
else
{
m_pixels->fill(m_pixels->Color(0, 0, 0),0,64);
}
//m_pixels->show(); // Send the updated pixel colors to the hardware.
}

void WS2812B_LedStrip::setRedPrio(bool Value)
void WS2812B_LedStrip::setRedPrio(bool Value, bool bReverse)
{
uint32_t FillColor = m_Red;
if(bReverse){
FillColor = m_Green;
}

if(Value)
{
m_pixels->fill(m_Red,0,8);
m_pixels->fill(FillColor,0,8);
}
else
{
Expand All @@ -87,11 +96,14 @@ void WS2812B_LedStrip::setRedPrio(bool Value)
//m_pixels->show(); // Send the updated pixel colors to the hardware.
}

void WS2812B_LedStrip::setGreen(bool Value)
void WS2812B_LedStrip::setGreen(bool Value, bool bReverse)
{
uint32_t FillColor = m_Green;
if(bReverse)
FillColor = m_Red;
if(Value)
{
m_pixels->fill(m_Green,64,64);
m_pixels->fill(FillColor,64,64);

}
else
Expand All @@ -101,11 +113,15 @@ void WS2812B_LedStrip::setGreen(bool Value)
//m_pixels->show(); // Send the updated pixel colors to the hardware.
}

void WS2812B_LedStrip::setGreenPrio(bool Value)
void WS2812B_LedStrip::setGreenPrio(bool Value, bool bReverse)
{
uint32_t FillColor = m_Green;
if(bReverse){
FillColor = m_Red;
}
if(Value)
{
m_pixels->fill(m_Green,64,8);
m_pixels->fill(FillColor,64,8);

}
else
Expand Down Expand Up @@ -473,14 +489,15 @@ void WS2812B_LedStrip::SetLedStatus(unsigned char val)
m_LedStatus = val;
}
bool ColoredOn = m_LedStatus & MASK_RED;
setRed(ColoredOn);
bool ReverseColor = m_LedStatus & MASK_REVERSE_COLORS;
setRed(ColoredOn,ReverseColor);
if(!ColoredOn)
{
setWhiteLeft(m_LedStatus & MASK_WHITE_L);
if(!(m_LedStatus & MASK_WHITE_L) ) // This is needed because I'm re-using the "white part" to show orange
{
setOrangeLeft(m_LedStatus & MASK_ORANGE_L);
setRedPrio(m_PrioLeft);
setRedPrio(m_PrioLeft,ReverseColor);
setYellowCardLeft(m_YellowCardLeft);
setRedCardLeft(m_RedCardLeft);
setUWFTimeLeft(m_UW2Ftens);
Expand All @@ -490,14 +507,15 @@ void WS2812B_LedStrip::SetLedStatus(unsigned char val)
}

ColoredOn = m_LedStatus & MASK_GREEN;
setGreen(ColoredOn);
setGreen(ColoredOn,ReverseColor);

if(!ColoredOn)
{
setWhiteRight(m_LedStatus & MASK_WHITE_R);
if(!(m_LedStatus & MASK_WHITE_R) )
{
setOrangeRight(m_LedStatus & MASK_ORANGE_R);
setGreenPrio(m_PrioRight);
setGreenPrio(m_PrioRight,ReverseColor);
setYellowCardRight(m_YellowCardRight);
setRedCardRight(m_RedCardRight);
setUWFTimeRight(m_UW2Ftens);
Expand Down Expand Up @@ -527,19 +545,20 @@ void WS2812B_LedStrip::AnimatePrio()
{
if(!m_Animating)
return;

if(millis() < m_NextTimeToTogglePrioLights)
return;

m_NextTimeToTogglePrioLights = millis() + 60 + m_counter * 15;
if(m_counter & 1)
{
setGreenPrio(false);
setRedPrio(true);
setGreenPrio(!m_ReverseColors,m_ReverseColors);
setRedPrio(m_ReverseColors,m_ReverseColors);
}
else
{
setGreenPrio(true);
setRedPrio(false);
setGreenPrio(m_ReverseColors,m_ReverseColors);
setRedPrio(!m_ReverseColors,m_ReverseColors);
}
m_pixels->show();
m_counter--;
Expand All @@ -559,23 +578,25 @@ void WS2812B_LedStrip::StartPrioAnimation(uint8_t prio)
case 0:
setGreenPrio(false);
setRedPrio(false);
m_pixels->show();
m_pixels->show(); // clear directly, no animation needed
m_PrioLeft = false;
m_PrioRight = false;
return;
break;
case 2:WS2812B_LedStrip::
case 2:
m_PrioLeft = false;
m_PrioRight = true;
m_targetprio = 1;
break;

case 1:
m_PrioLeft = true;
m_PrioRight = false;
m_targetprio = 2;
break;

}
m_targetprio = prio;
//_targetprio = prio;

m_counter = 17 + prio;
m_NextTimeToTogglePrioLights = millis() + 100 + m_counter * 15;
Expand Down
11 changes: 7 additions & 4 deletions WS2812BLedStrip.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#define MASK_WHITE_R 0x08
#define MASK_GREEN 0x04
#define MASK_BUZZ 0x02
#define MASK_REVERSE_COLORS 0x01

#define BRIGHTNESS_LOW 15
#define BRIGHTNESS_NORMAL 30
Expand All @@ -69,12 +70,12 @@ class WS2812B_LedStrip : public Observer<FencingStateMachine>,public Observer<Re
*/
void ClearAll();
void SetLedStatus(unsigned char val);
void setRed(bool Value);
void setRed(bool Value, bool bReverse = false);
void setWhiteLeft(bool Value, bool inverse = false);
void setOrangeLeft(bool Value);
void setOrangeRight(bool Value);
void setWhiteRight(bool Value, bool inverse = false);
void setGreen(bool Value);
void setGreen(bool Value, bool bReverse= false);
void setBuzz(bool Value);
//void setBuzz(bool Value){return;};
void myShow(){m_pixels->show();};
Expand All @@ -83,8 +84,8 @@ class WS2812B_LedStrip : public Observer<FencingStateMachine>,public Observer<Re
void update (RepeaterReceiver *subject, uint32_t eventtype);
void ProcessEvents ();
void ProcessEventsBlocking ();
void setGreenPrio(bool Value);
void setRedPrio(bool Value);
void setGreenPrio(bool Value, bool bReverse = false);
void setRedPrio(bool Value, bool bReverse = false);
void AnimatePrio();
void StartPrioAnimation(uint8_t prio);
void AnimateWarning();
Expand All @@ -101,6 +102,7 @@ class WS2812B_LedStrip : public Observer<FencingStateMachine>,public Observer<Re
void setUWFTimeLeft(uint8_t tens);
void setUWFTimeRight(uint8_t tens);
void begin();
void SetMirroring(bool value){m_ReverseColors = value;}


protected:
Expand All @@ -121,6 +123,7 @@ class WS2812B_LedStrip : public Observer<FencingStateMachine>,public Observer<Re
uint32_t m_Blue;
uint32_t m_Off;
uint32_t m_LastEvent = 0;
bool m_ReverseColors = false;
bool m_PrioLeft = false;
bool m_PrioRight = false;
bool m_YellowCardLeft = false;
Expand Down
Loading

0 comments on commit 520e9ba

Please sign in to comment.