11#include  " ModeSharing.h" 
22
3+ #include  " ../../VortexEngine.h" 
34#include  " ../../Serial/ByteStream.h" 
45#include  " ../../Serial/Serial.h" 
56#include  " ../../Time/TimeControl.h" 
1516
1617ModeSharing::ModeSharing (const  RGBColor &col, bool  advanced) :
1718  Menu(col, advanced),
18-   m_sharingMode(ModeShareState::SHARE_RECEIVE),
19-   m_timeOutStartTime(0 )
19+   m_sharingMode(ModeShareState::SHARE_SEND_RECEIVE),
20+   m_timeOutStartTime(0 ),
21+   m_lastPercentChange(0 ),
22+   m_lastPercent(0 )
23+ 
2024{
2125}
2226
2327ModeSharing::~ModeSharing ()
2428{
29+   VLReceiver::endReceiving ();
30+   VortexEngine::toggleForceSleep (true );
2531}
2632
2733bool  ModeSharing::init ()
2834{
2935  if  (!Menu::init ()) {
3036    return  false ;
3137  }
38+   //  skip led selection
3239  if  (!m_advanced) {
3340    //  skip led selection
3441    m_ledSelected = true ;
3542  }
3643  //  start on receive because it's the more responsive of the two
3744  //  the odds of opening receive and then accidentally receiving
3845  //  a mode that is being broadcast nearby is completely unlikely
39-   beginReceiving ();
46+   VLReceiver::beginReceiving ();
47+   //  turn off force sleep while using modesharing
48+   VortexEngine::toggleForceSleep (false );
4049  DEBUG_LOG (" Entering Mode Sharing" 
4150  return  true ;
4251}
@@ -47,20 +56,24 @@ Menu::MenuAction ModeSharing::run()
4756  if  (result != MENU_CONTINUE) {
4857    return  result;
4958  }
50-   switch  (m_sharingMode) {
51-   case  ModeShareState::SHARE_SEND:
52-     //  render the 'send mode' lights
53-     showSendMode ();
54-     //  continue sending any data as long as there is more to send
55-     continueSending ();
56-     break ;
57-   case  ModeShareState::SHARE_RECEIVE:
58-     //  render the 'receive mode' lights
59-     showReceiveMode ();
60-     //  load any modes that are received
61-     receiveMode ();
62-     break ;
59+   if  (m_sharingMode == ModeShareState::SHARE_EXIT) {
60+     showExit ();
61+     return  MENU_CONTINUE;
62+   }
63+   //  if the button is held for at least a long click, then continuously send
64+   if  (g_pButton->isPressed () && g_pButton->holdDuration () >= CLICK_THRESHOLD) {
65+     //  exit is handled above so the only two options are normal or legacy
66+     if  (m_sharingMode == ModeShareState::SHARE_SEND_RECEIVE) {
67+       //  send normal
68+       VLSender::send (&m_previewMode);
69+     } else  {
70+       //  send legacy
71+       VLSender::sendLegacy (&m_previewMode);
72+     }
6373  }
74+   //  render the 'receive mode' lights whether legacy or not
75+   showReceiveMode ();
76+   receiveMode ();
6477  return  MENU_CONTINUE;
6578}
6679
@@ -78,12 +91,21 @@ void ModeSharing::onLedSelected()
7891//  handlers for clicks
7992void  ModeSharing::onShortClick ()
8093{
94+   if  (g_pButton->holdDuration () >= CLICK_THRESHOLD) {
95+     return ;
96+   }
8197  switch  (m_sharingMode) {
82-   case  ModeShareState::SHARE_RECEIVE:
83-     //  click while on receive -> end receive, start sending
84-     VLReceiver::endReceiving ();
85-     beginSending ();
86-     DEBUG_LOG (" Switched to send mode" 
98+   case  ModeShareState::SHARE_SEND_RECEIVE:
99+     //  stop receiving, send the mode, go back to receiving
100+     VLReceiver::setLegacyReceiver (true );
101+     m_sharingMode = ModeShareState::SHARE_SEND_RECEIVE_LEGACY;
102+     break ;
103+   case  ModeShareState::SHARE_SEND_RECEIVE_LEGACY:
104+     VLReceiver::setLegacyReceiver (false );
105+     m_sharingMode = ModeShareState::SHARE_EXIT;
106+     break ;
107+   case  ModeShareState::SHARE_EXIT:
108+     m_sharingMode = ModeShareState::SHARE_SEND_RECEIVE;
87109    break ;
88110  default :
89111    break ;
@@ -93,57 +115,30 @@ void ModeSharing::onShortClick()
93115
94116void  ModeSharing::onLongClick ()
95117{
96-   leaveMenu ();
97- }
98- 
99- void  ModeSharing::beginSending ()
100- {
101-   //  if the sender is sending then cannot start again
102-   if  (VLSender::isSending ()) {
103-     ERROR_LOG (" Cannot begin sending, sender is busy" 
104-     return ;
105-   }
106-   m_sharingMode = ModeShareState::SHARE_SEND;
107-   //  initialize it with the current mode data
108-   VLSender::loadMode (&m_previewMode);
109-   //  send the first chunk of data, leave if we're done
110-   if  (!VLSender::send ()) {
111-     //  when send has completed, stores time that last action was completed to calculate interval between sends
112-     beginReceiving ();
118+   if  (m_sharingMode == ModeShareState::SHARE_EXIT) {
119+     leaveMenu ();
113120  }
114121}
115122
116- void  ModeSharing::continueSending ()
117- {
118-   //  if the sender isn't sending then nothing to do
119-   if  (!VLSender::isSending ()) {
120-     return ;
121-   }
122-   if  (!VLSender::send ()) {
123-     //  when send has completed, stores time that last action was completed to calculate interval between sends
124-     beginReceiving ();
125-   }
126- }
127- 
128- void  ModeSharing::beginReceiving ()
129- {
130-   m_sharingMode = ModeShareState::SHARE_RECEIVE;
131-   VLReceiver::beginReceiving ();
132- }
133- 
134123void  ModeSharing::receiveMode ()
135124{
125+   uint32_t  now = Time::getCurtime ();
136126  //  if reveiving new data set our last data time
137127  if  (VLReceiver::onNewData ()) {
138-     m_timeOutStartTime = Time::getCurtime () ;
128+     m_timeOutStartTime = now ;
139129    //  if our last data was more than time out duration reset the recveiver
140-   } else  if  (m_timeOutStartTime > 0  && (m_timeOutStartTime + MAX_TIMEOUT_DURATION) < Time::getCurtime () ) {
130+   } else  if  (m_timeOutStartTime > 0  && (m_timeOutStartTime + MAX_TIMEOUT_DURATION) < now ) {
141131    VLReceiver::resetVLState ();
142132    m_timeOutStartTime = 0 ;
143133    return ;
144134  }
145135  //  check if the VLReceiver has a full packet available
146136  if  (!VLReceiver::dataReady ()) {
137+     uint8_t  percent = VLReceiver::percentReceived ();
138+     if  (percent != m_lastPercent) {
139+       m_lastPercent = percent;
140+       m_lastPercentChange = now;
141+     }
147142    //  nothing available yet
148143    return ;
149144  }
@@ -170,21 +165,20 @@ void ModeSharing::receiveMode()
170165  leaveMenu (true );
171166}
172167
173- void  ModeSharing::showSendMode ()
174- {
175-   //  show a dim color when not sending
176-   if  (!VLSender::isSending ()) {
177-     Leds::setAll (RGBColor (0 , 20 , 20 ));
178-   }
179- }
180- 
181168void  ModeSharing::showReceiveMode ()
182169{
170+   //  if the receiver is actively receiving right now then
183171  if  (VLReceiver::isReceiving ()) {
184-     //  using uint32_t to avoid overflow, the result should be within 10 to 255
185-     Leds::setIndex (LED_0, RGBColor (0 , VLReceiver::percentReceived (), 0 ));
172+     uint32_t  diff = (Time::getCurtime () - m_lastPercentChange);
173+     //  this generates the red flash when the receiver hasn't received something for
174+     //  some amount of time, 100 is just arbitray idk if it could be a better value
175+     if  (diff > 100 ) {
176+       Leds::setIndex (LED_0, RGB_RED3);
177+     } else  {
178+       Leds::setIndex (LED_0, RGBColor (0 , VLReceiver::percentReceived (), 0 ));
179+     }
186180    Leds::clearIndex (LED_1);
187181  } else  {
188-     Leds::setAll (RGB_WHITE0);
182+     Leds::setAll ((m_sharingMode == ModeShareState::SHARE_SEND_RECEIVE) ?  0x000F05  :  RGB_WHITE0);
189183  }
190184}
0 commit comments