gusimplewhiteboard
IOPins.hpp
Go to the documentation of this file.
1/*
2 * IOPins.h
3 * gusimplewhiteboard / arduino simulator
4 *
5 * Created by Rene Hexel on 5/08/2015.
6 * Copyright © 2015 Rene Hexel. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials
18 * provided with the distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgement:
22 *
23 * This product includes software developed by Rene Hexel.
24 *
25 * 4. Neither the name of the author nor the names of contributors
26 * may be used to endorse or promote products derived from this
27 * software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
33 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
34 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
35 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
36 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
37 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
38 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * -----------------------------------------------------------------------
42 * This program is free software; you can redistribute it and/or
43 * modify it under the above terms or under the terms of the GNU
44 * General Public License as published by the Free Software Foundation;
45 * either version 2 of the License, or (at your option) any later version.
46 *
47 * This program is distributed in the hope that it will be useful,
48 * but WITHOUT ANY WARRANTY; without even the implied warranty of
49 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50 * GNU General Public License for more details.
51 *
52 * You should have received a copy of the GNU General Public License
53 * along with this program; if not, see http://www.gnu.org/licenses/
54 * or write to the Free Software Foundation, Inc., 51 Franklin Street,
55 * Fifth Floor, Boston, MA 02110-1301, USA.
56 *
57 */
58
59#ifndef IOPins_DEFINED
60#define IOPins_DEFINED
61
62#include "wb_io_pins.h"
63#ifdef WHITEBOARD_POSTER_STRING_CONVERSION
64#include <sstream>
65#include <ctype.h>
66#endif
67
68namespace guWhiteboard
69{
74 class IOPins: public wb_io_pins
75 {
76 public:
78 bool get(int pin) const { return IO_PIN_GET(this, pin); }
79
81 void set(int pin) { IO_PIN_SET(this, pin); }
82
84 void clr(int pin) { IO_PIN_CLR(this, pin); }
85
87 void set(int pin, bool value) { value ? set(pin) : clr(pin); }
88
90 void reset(int value = 0) { memset(this, value, sizeof(*this)); }
91
93 bool operator==(const IOPins &other) const { return memcmp(this, &other, sizeof(*this)) == 0; }
94
95#ifdef WHITEBOARD_POSTER_STRING_CONVERSION
97 IOPins(const std::string &pinvalues): wb_io_pins() { from_string(pinvalues); }
98
100 std::string description() const
101 {
102 std::ostringstream ss;
103 bool first = true;
104 for (size_t i = 0; i < IO_PIN_BIT_SIZE; i++)
105 {
106 if (IO_PIN_GET(this, i))
107 {
108 ss << (first ? "" : ",") << i;
109 first = false;
110 }
111 }
112 return ss.str();
113 }
114
116 void from_string(const std::string &str)
117 {
118 std::istringstream iss(str);
119 std::string token;
120 for (int i = 0; i < static_cast<int>(IO_PIN_BIT_SIZE) && getline(iss, token, ','); i++)
121 {
122 const int n = atoi(token.c_str());
123 if (n >= 0 && n < static_cast<int>(IO_PIN_BIT_SIZE))
124 set(n);
125 else if (-n >= 0 && -n < static_cast<int>(IO_PIN_BIT_SIZE))
126 clr(n);
127 }
128 }
129#endif
130 };
131}
132
133#endif /* IOPins_DEFINED */
Class for controlling and getting the status of IO pins.
Definition: IOPins.hpp:75
std::string description() const
convert to a string
Definition: IOPins.hpp:100
IOPins(const std::string &pinvalues)
string constructor
Definition: IOPins.hpp:97
bool get(int pin) const
pin value setter
Definition: IOPins.hpp:78
void set(int pin, bool value)
set the given pin to the given value
Definition: IOPins.hpp:87
void reset(int value=0)
set all pins to the given value
Definition: IOPins.hpp:90
void from_string(const std::string &str)
convert from a string
Definition: IOPins.hpp:116
bool operator==(const IOPins &other) const
comparison operator
Definition: IOPins.hpp:93
void set(int pin)
pin value setter
Definition: IOPins.hpp:81
void clr(int pin)
clear the given pin
Definition: IOPins.hpp:84
/file APM_Interface.h
Simulated I/O pins.
Definition: wb_io_pins.h:81
#define IO_PIN_SET(s, n)
Definition: wb_io_pins.h:68
#define IO_PIN_CLR(s, n)
Definition: wb_io_pins.h:67
#define IO_PIN_GET(s, n)
Definition: wb_io_pins.h:69
#define IO_PIN_BIT_SIZE
bits available
Definition: wb_io_pins.h:65