-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpins.cpp
More file actions
42 lines (36 loc) · 1.35 KB
/
pins.cpp
File metadata and controls
42 lines (36 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2017 Dimitry Ishenko
// Contact: dimitry (dot) ishenko (at) (gee) mail (dot) com
//
// Distributed under the GNU GPL license. See the LICENSE.md file for details.
////////////////////////////////////////////////////////////////////////////////
#include "firmata/pins.hpp"
#include <stdexcept>
////////////////////////////////////////////////////////////////////////////////
namespace firmata
{
////////////////////////////////////////////////////////////////////////////////
firmata::pin& pins::get(firmata::mode mode, firmata::pos pos)
{
return const_cast<firmata::pin&>(
static_cast<const pins&>(*this).get(mode, pos)
);
}
////////////////////////////////////////////////////////////////////////////////
const firmata::pin& pins::get(firmata::mode mode, firmata::pos pos) const
{
if(mode == analog_in)
{
// analog pins are treated specially,
// as they may be numbered differently
for(auto& pin : *this) if(pin.analog() == pos) return pin;
}
else
{
// other pins don't have specific numbers
for(auto& pin : *this) if(pin.supports(mode) && 0 == pos--) return pin;
}
throw std::out_of_range("Pin not found");
}
////////////////////////////////////////////////////////////////////////////////
}