RSeries astromech firmware
ServoFeedback.h
Go to the documentation of this file.
1 #ifndef ServoFeedback_h
2 #define ServoFeedback_h
3 
4 #include "ReelTwo.h"
5 #include "core/SetupEvent.h"
6 #include "core/AnimatedEvent.h"
7 
18 template <uint8_t NUM_SERVOS>
20 {
21 public:
22  ServoFeedback(const uint8_t* analogPins /*PROGMEM*/) :
23  fPins(analogPins),
24  fCurrentServo(0),
25  fLastRead(0)
26  {
27  for (uint8_t i = 0; i < NUM_SERVOS; i++)
28  fPos[i] = 0;
29  }
30 
31  uint8_t numServos() const
32  {
33  return NUM_SERVOS;
34  }
35 
36  virtual void setup() override
37  {
38  for (uint8_t i = 0 ; i < NUM_SERVOS; i++)
39  {
40  uint8_t pin = pgm_read_byte(&fPins[i]);
41  pinMode(pin, INPUT);
42  }
43  }
44 
45  virtual void animate() override
46  {
47  if (fLastRead + 15 < millis())
48  {
49  if (fCurrentServo < NUM_SERVOS)
50  {
51  uint8_t pin = pgm_read_byte(&fPins[fCurrentServo]);
52  uint16_t val = analogRead(pin);
53  fPos[fCurrentServo] = map(val, 100, 512, 0, 255);
54  if (++fCurrentServo >= NUM_SERVOS)
55  fCurrentServo = 0;
56  }
57  fLastRead = millis();
58  }
59  }
60 
61  inline uint8_t getRaw(uint8_t servoNum)
62  {
63  return (servoNum <= NUM_SERVOS) ? fPos[servoNum] : 0;
64  }
65 
66  uint8_t getDegrees(uint8_t servoNum)
67  {
68  return map(getRaw(servoNum), 0, 254, 0, 180);
69  }
70 
71 private:
72  const uint8_t* fPins; /* PROGMEM */
73  uint8_t fPos[NUM_SERVOS];
74  uint8_t fCurrentServo;
75  uint32_t fLastRead;
76 };
77 
78 #endif
ReelTwo.h
SetupEvent.h
AnimatedEvent
Base class for all animated devices. AnimatedEvent::animate() is called for each device once through ...
Definition: AnimatedEvent.h:18
SetupEvent
Base class for all devices that require setup that cannot happen in the constructor....
Definition: SetupEvent.h:15
ServoFeedback::getRaw
uint8_t getRaw(uint8_t servoNum)
Definition: ServoFeedback.h:61
AnimatedEvent.h
ServoFeedback::getDegrees
uint8_t getDegrees(uint8_t servoNum)
Definition: ServoFeedback.h:66
ServoFeedback::setup
virtual void setup() override
Subclasses must implement this function to perform any necessary setup that cannot happen in the cons...
Definition: ServoFeedback.h:36
ServoFeedback
Provides an interface to read analog feedback from a set of servos.
Definition: ServoFeedback.h:19
ServoFeedback::numServos
uint8_t numServos() const
Definition: ServoFeedback.h:31
ServoFeedback::animate
virtual void animate() override
Subclasses must implement this function to run through a single frame of animation/activity.
Definition: ServoFeedback.h:45
ServoFeedback::ServoFeedback
ServoFeedback(const uint8_t *analogPins)
Definition: ServoFeedback.h:22