gusimplewhiteboard
Channels.hpp
Go to the documentation of this file.
1/*
2 * Channels.h
3 * gusimplewhiteboard
4 *
5 * Created by Josh Stover on 07/09/2015.
6 * Copyright © 2015 Josh Stover. 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 Channels_DEFINED
60#define Channels_DEFINED
61
62#include "wb_channels.h"
63#include "wb_io_pins.h"
64#ifdef WHITEBOARD_POSTER_STRING_CONVERSION
65#include <sstream>
66#include <ctype.h>
67#endif
68
69namespace guWhiteboard
70{
87 class Channels: public wb_channels
88 {
89 public:
91 bool waiting(int channel) { return CHANNEL_WAITING(this, channel); }
92
94 void send(int channel) { CHANNEL_SEND(this, channel); }
95
97 void clear(int channel) { CHANNEL_CLEAR(this, channel); }
98
100 void reset() { memset(this, 0, sizeof(*this)); }
101
103 bool operator==(const Channels &other) const { return memcmp(this, &other, sizeof(*this)) == 0; }
104
105#ifdef WHITEBOARD_POSTER_STRING_CONVERSION
107 Channels(const std::string &channel_values): wb_channels() { from_string(channel_values); }
108
110 std::string description() const
111 {
112 std::ostringstream ss;
113 bool first = true;
114 for (size_t i=0; i<CHANNELS_COUNT; i++) {
115 if (CHANNEL_WAITING(this, i)) {
116 ss << (first ? "" : ",") << i ;
117 first = false;
118 }
119 }
120 return ss.str();
121 }
122
124 void from_string(const std::string &str)
125 {
126 std::istringstream iss(str);
127 std::string token;
128 for (size_t i=0; i<CHANNELS_COUNT && getline(iss, token, ','); i++)
129 {
130 const int n = atoi(token.c_str());
131 if (n >= 0 && n < static_cast<int>(CHANNELS_COUNT))
132 send(n);
133 else if (-n >= 0 && -n < static_cast<int>(IO_PIN_BIT_SIZE))
134 clear(n);
135 }
136 }
137#endif
138 };
139}
140
141#endif /* Channels_DEFINED */
142
Class which emulates the 2-process channel synchronisation system from UPPAAL.
Definition: Channels.hpp:88
bool operator==(const Channels &other) const
comparison operator
Definition: Channels.hpp:103
Channels(const std::string &channel_values)
string constructor
Definition: Channels.hpp:107
void from_string(const std::string &str)
convert from a string
Definition: Channels.hpp:124
void reset()
reset all channel signals to zero
Definition: Channels.hpp:100
void send(int channel)
synchronise two machines: sender
Definition: Channels.hpp:94
std::string description() const
convert to a string
Definition: Channels.hpp:110
bool waiting(int channel)
check if a signal has been sent on a channel
Definition: Channels.hpp:91
void clear(int channel)
clear sync signal from a channel
Definition: Channels.hpp:97
/file APM_Interface.h
UPPAAL style synchronisation channels.
Definition: wb_channels.h:80
#define CHANNEL_WAITING(s, n)
Return the state of the channel.
Definition: wb_channels.h:74
#define CHANNELS_COUNT
channels available
Definition: wb_channels.h:65
#define CHANNEL_CLEAR(s, n)
Set channel state OFF.
Definition: wb_channels.h:71
#define CHANNEL_SEND(s, n)
Set channel state ON.
Definition: wb_channels.h:68
#define IO_PIN_BIT_SIZE
bits available
Definition: wb_io_pins.h:65