RSeries astromech firmware
DomeSensorRingSerialListener.h
Go to the documentation of this file.
1 #ifndef DomeSensorRingSerialListener_h
2 #define DomeSensorRingSerialListener_h
3 
4 #include "ReelTwo.h"
5 #include "core/AnimatedEvent.h"
8 
9 #ifndef DOMESENSOR_BAUD_RATE
10 #define DOMESENSOR_BAUD_RATE 57600 /* default */
11 #endif
12 
13 #ifdef USE_DOME_SENSOR_SERIAL_DEBUG
14 #define DOME_SENSOR_SERIAL_PRINT(s) DEBUG_PRINT(s)
15 #define DOME_SENSOR_SERIAL_PRINTLN(s) DEBUG_PRINTLN(s)
16 #define DOME_SENSOR_SERIAL_PRINT_HEX(s) DEBUG_PRINT_HEX(s)
17 #define DOME_SENSOR_SERIAL_PRINTLN_HEX(s) DEBUG_PRINTLN_HEX(s)
18 #else
19 #define DOME_SENSOR_SERIAL_PRINT(s)
20 #define DOME_SENSOR_SERIAL_PRINTLN(s)
21 #define DOME_SENSOR_SERIAL_PRINT_HEX(s)
22 #define DOME_SENSOR_SERIAL_PRINTLN_HEX(s)
23 #endif
24 
26 {
27 public:
28  DomeSensorRingSerialListener(Stream& serial) :
29  fStream(&serial)
30  {
31  }
32 
33  inline unsigned getErrorCount()
34  {
35  return fErrorCount;
36  }
37 
38  virtual bool ready() override
39  {
40  return (fPosition != -1);
41  }
42 
43  virtual int getAngle() override
44  {
45  return fPosition;
46  }
47 
48  virtual void animate() override
49  {
50  // append commands to command buffer
51  while (fStream->available())
52  {
53  int ch = fStream->read();
54  if (ch == '\r' || ch == '\n')
55  {
56  if (fState == 4)
57  {
58  // Update position
59  fSamples.append(fValue);
60  if (fSampleCount < 6)
61  {
62  fPosition = fValue;
63  fSampleCount++;
64  }
65  else
66  {
67  // Return the filtered angle
68  fPosition = fSamples.median();
69  }
71  DOME_SENSOR_SERIAL_PRINTLN(fPosition);
72  }
73  fState = 0;
74  if (fStream->available() < 10)
75  return;
76  continue;
77  }
78  else
79  {
80  DOME_SENSOR_SERIAL_PRINT((char)ch);
81  }
82  if (fState == -1)
83  continue;
84  switch (fState)
85  {
86  case 0:
87  fState = (ch == '#') ? fState+1 : -1;
88  break;
89  case 1:
90  fState = (ch == 'D') ? fState+1 : -1;
91  break;
92  case 2:
93  fState = (ch == 'P') ? fState+1 : -1;
94  break;
95  case 3:
96  fState = (ch == '@') ? fState+1 : -1;
97  fValue = 0;
98  break;
99  case 4:
100  if (ch >= '0' && ch <= '9')
101  {
102  fValue = fValue * 10 + (ch - '0');
103  }
104  else
105  {
106  fState = -1;
107  }
108  break;
109  }
110  if (fState == -1)
111  {
112  // ERROR: Ignore remaining input
113  DEBUG_PRINTLN("[DOME SENSOR] ERROR READING POSITION");
114  fErrorCount++;
115  }
116  }
117  }
118 
119 private:
120  Stream* fStream;
121  int fPosition = -1;
122  int8_t fState = 0;
123  int fValue = 0;
124  int fSampleCount = 0;
125  unsigned fErrorCount = 0;
127 };
128 
129 #endif
DomeSensorRingSerialListener::getAngle
virtual int getAngle() override
Definition: DomeSensorRingSerialListener.h:43
ReelTwo.h
AnimatedEvent
Base class for all animated devices. AnimatedEvent::animate() is called for each device once through ...
Definition: AnimatedEvent.h:18
DomeSensorRingSerialListener::animate
virtual void animate() override
Subclasses must implement this function to run through a single frame of animation/activity.
Definition: DomeSensorRingSerialListener.h:48
AnimatedEvent.h
DOME_SENSOR_SERIAL_PRINT
#define DOME_SENSOR_SERIAL_PRINT(s)
Definition: DomeSensorRingSerialListener.h:19
DomeSensorRingSerialListener
Definition: DomeSensorRingSerialListener.h:25
DEBUG_PRINTLN
#define DEBUG_PRINTLN(s)
Definition: ReelTwo.h:188
MedianSampleBuffer::append
void append(T val)
Definition: MedianSampleBuffer.h:21
DomePositionProvider
Definition: DomePositionProvider.h:4
DomeSensorRingSerialListener::getErrorCount
unsigned getErrorCount()
Definition: DomeSensorRingSerialListener.h:33
MedianSampleBuffer< short, 5 >
DOME_SENSOR_SERIAL_PRINTLN
#define DOME_SENSOR_SERIAL_PRINTLN(s)
Definition: DomeSensorRingSerialListener.h:20
DomePositionProvider.h
DomeSensorRingSerialListener::ready
virtual bool ready() override
Definition: DomeSensorRingSerialListener.h:38
MedianSampleBuffer::median
T median()
Definition: MedianSampleBuffer.h:31
DomeSensorRingSerialListener::DomeSensorRingSerialListener
DomeSensorRingSerialListener(Stream &serial)
Definition: DomeSensorRingSerialListener.h:28
MedianSampleBuffer.h