This is an old version of the documentation. View the latest version here.
Control Surface  1.0.0
MIDI Control Surface library for Arduino
ExtendedInputOutput.cpp
Go to the documentation of this file.
2 #include "ExtendedIOElement.hpp"
3 #include <Arduino.h>
4 #include <Helpers/Error.hpp>
5 
7 
8 namespace ExtIO {
9 
10 template <class T>
11 bool inRange(T target, T start, T end) {
12  return target >= start && target < end;
13 }
14 
17  if (pin < el.getStart())
18  break;
19  else if (inRange(pin, el.getStart(), el.getEnd()))
20  return el;
21 
23  F("The given pin does not correspond to an Extended IO element."),
24  0x8888);
25 
26  // TODO: why doesn't this give a compilation error?
27  // No return statement. On desktop, FATAL_ERROR throws an exception, so
28  // I get why that works, but on Arduino, it just calls fatalErrorExit, which
29  // is marked 'noreturn'. However, if I remove the 'noreturn' attribute, and
30  // have it return immediately, it still compiles, without returning a valid
31  // reference.
32 }
33 
34 void pinMode(pin_t pin, uint8_t mode) {
35  // DEBUGFN(DEBUGVAR(pin) << '\t' << DEBUGVAR(mode));
36  if (pin < NUM_DIGITAL_PINS + NUM_ANALOG_INPUTS) {
37  ::pinMode(pin, mode);
38  } else {
40  el.pinMode(pin - el.getStart(), mode);
41  }
42 }
43 void pinMode(int pin, uint8_t mode) { pinMode((pin_t)pin, mode); }
44 
45 void digitalWrite(pin_t pin, uint8_t val) {
46  // DEBUGFN(DEBUGVAR(pin) << '\t' << DEBUGVAR(val));
47  if (pin < NUM_DIGITAL_PINS + NUM_ANALOG_INPUTS) {
48  ::digitalWrite(pin, val);
49  } else {
51  el.digitalWrite(pin - el.getStart(), val);
52  }
53 }
54 void digitalWrite(int pin, uint8_t val) { digitalWrite((pin_t)pin, val); }
55 
56 int digitalRead(pin_t pin) {
57  if (pin < NUM_DIGITAL_PINS + NUM_ANALOG_INPUTS) {
59  } else {
61  return el.digitalRead(pin - el.getStart());
62  }
63  return 0;
64 }
65 int digitalRead(int pin) { return digitalRead((pin_t)pin); }
66 
67 void shiftOut(pin_t dataPin, pin_t clockPin, uint8_t bitOrder, uint8_t val) {
68  uint8_t i;
69 
70  for (i = 0; i < 8; i++) {
71  if (bitOrder == LSBFIRST)
72  digitalWrite(dataPin, !!(val & (1 << i)));
73  else
74  digitalWrite(dataPin, !!(val & (1 << (7 - i))));
75 
76  digitalWrite(clockPin, HIGH);
77  digitalWrite(clockPin, LOW);
78  }
79 }
80 void shiftOut(int dataPin, int clockPin, uint8_t bitOrder, uint8_t val) {
81  shiftOut((pin_t)dataPin, (pin_t)clockPin, bitOrder, val);
82 }
83 
85  if (pin < NUM_DIGITAL_PINS + NUM_ANALOG_INPUTS) {
87  } else {
89  return el.analogRead(pin - el.getStart());
90  }
91  return 0;
92 }
93 analog_t analogRead(int pin) { return analogRead((pin_t)pin); }
94 
95 } // namespace ExtIO
96 
ExtendedIOElement.hpp
ExtIO::analogRead
analog_t analogRead(int pin)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:93
ExtIO::inRange
bool inRange(T target, T start, T end)
Definition: ExtendedInputOutput.cpp:11
ExtendedIOElement::pinMode
virtual void pinMode(pin_t pin, uint8_t mode)=0
Set the mode of a given pin.
Error.hpp
ExtIO::pinMode
void pinMode(pin_t pin, uint8_t mode)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:34
pin_t
uint16_t pin_t
The type for Arduino pins (and ExtendedIOElement pins).
Definition: Def.hpp:17
ExtendedInputOutput.hpp
HIGH
const uint8_t HIGH
Definition: ExtendedInputOutput.hpp:34
ExtIO::getIOElementOfPin
ExtendedIOElement & getIOElementOfPin(pin_t pin)
Find the IO element of a given extended IO pin number.
Definition: ExtendedInputOutput.cpp:15
ExtendedIOElement::getStart
pin_t getStart() const
Get the smallest global extended IO pin number that belongs to this extended IO element.
Definition: ExtendedIOElement.cpp:47
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: NamespaceSettings.hpp:9
analog_t
uint16_t analog_t
The type returned from analogRead and similar functions.
Definition: Def.hpp:15
ExtIO::digitalWrite
void digitalWrite(pin_t pin, uint8_t val)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:45
ExtIO::shiftOut
void shiftOut(pin_t dataPin, pin_t clockPin, uint8_t bitOrder, uint8_t val)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:67
ExtIO::digitalRead
int digitalRead(int pin)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:65
ExtendedIOElement::getAll
static DoublyLinkedList< ExtendedIOElement > & getAll()
Get the list of all Extended IO elements.
Definition: ExtendedIOElement.cpp:49
LOW
const uint8_t LOW
Definition: ExtendedInputOutput.hpp:35
ExtIO
A namespace with alternatives to the standard Arduino IO functions that can be used with extended IO ...
Definition: ExtendedInputOutput.cpp:8
ExtendedIOElement
An abstract base class for Extended Input/Output elements.
Definition: ExtendedIOElement.hpp:59
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: NamespaceSettings.hpp:10
ExtendedIOElement::digitalWrite
virtual void digitalWrite(pin_t pin, uint8_t state)=0
Set the output of the given pin to the given state.
ExtIO::digitalRead
int digitalRead(pin_t pin)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:56
FATAL_ERROR
#define FATAL_ERROR(x, e)
Definition: Error.hpp:35
ExtendedIOElement::digitalRead
virtual int digitalRead(pin_t pin)=0
Read the state of the given pin.
ExtendedIOElement::analogRead
virtual analog_t analogRead(pin_t pin)=0
Read the analog value of the given pin.
ExtIO::analogRead
analog_t analogRead(pin_t pin)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:84
MIDI_Notes::F
constexpr int8_t F
Definition: Notes.hpp:23