gusimplewhiteboard
NXT_Interface.hpp
Go to the documentation of this file.
1
9#ifndef NXT_Interface_DEFINED
10#define NXT_Interface_DEFINED
11
12#include "wb_nxt_interface.h"
13
14#ifdef WHITEBOARD_POSTER_STRING_CONVERSION
15#include <sstream>
16#endif
17
18namespace guWhiteboard
19{
39 {
40 public:
41
45
46
48 inline bool operator == (const NXT_Interface &s)
49 {
50 for(int p = Port1; p < NUMBER_OF_NXT_PORTS; p++)
51 {
52 nxt_port_object o = this->objects(p);
53 nxt_port_object n = s.objects(p);
54 if(o.type() != n.type())
55 return false;
56#define PORT "(" << p << ")"
57
58#pragma clang diagnostic push
59#pragma clang diagnostic ignored "-Wswitch-enum"
60 switch (o.type())
61 {
62 case Touch_Sensor:
63 if(o.data().touch_sensor.pressed() != n.data().touch_sensor.pressed())
64 return false;
65 break;
66 case Sonar_Sensor:
67 if(o.data().sonar_sensor.distance() != n.data().sonar_sensor.distance())
68 return false;
69 break;
71 if(o.data().active_light_sensor.value() != n.data().active_light_sensor.value())
72 return false;
73 break;
75 if(o.data().passive_light_sensor.value() != n.data().passive_light_sensor.value())
76 return false;
77 break;
78 case Motor:
79 if(o.data().motor.speed() != n.data().motor.speed() || o.data().motor.enc_ticks() != n.data().motor.enc_ticks() || o.data().motor.speed_mask() != n.data().motor.speed_mask() || o.data().motor.enc_ticks_mask() != n.data().motor.enc_ticks_mask())
80 return false;
81 break;
82 default:
83 break;
84 }
85#pragma clang diagnostic pop
86 }
87 return true;
88 }
89
91 inline bool operator != (const NXT_Interface &s)
92 {
93 return !(*this == s);
94 }
95
96#ifdef WHITEBOARD_POSTER_STRING_CONVERSION
100 NXT_Interface(const std::string &str) { from_string(str); }
101
105 void from_string(const std::string &str)
106 {
107 //NYI
108 fprintf(stderr, "NXT_Interface doesn't implement from_string yet, have it back %s\n", str.c_str());
109 }
110
114 std::string description() const
115 {
116 std::stringstream ss;
117 for(int p = Port1; p < NUMBER_OF_NXT_PORTS; p++)
118 {
119 nxt_port_object o = objects(p);
120#define PORT "(" << p << ")"
121
122#pragma clang diagnostic push
123#pragma clang diagnostic ignored "-Wswitch-enum"
124 switch (o.type())
125 {
126 case Touch_Sensor:
127 ss << PORT << "Touch: " << o.data().touch_sensor.pressed() << ", ";
128 break;
129 case Sonar_Sensor:
130 ss << PORT << "Sonar: " << static_cast<int>(o.data().sonar_sensor.distance()) << ", ";
131 break;
133 ss << PORT << "Active Light: " << o.data().active_light_sensor.value() << ", ";
134 break;
136 ss << PORT << "Passive Light: " << o.data().passive_light_sensor.value() << ", ";
137 break;
138 case Motor:
139 ss << PORT << "Motor: (s: " << o.data().motor.speed() << ", enc: " << o.data().motor.enc_ticks() << "), ";
140 break;
141 default:
142 break;
143 }
144#pragma clang diagnostic pop
145 }
146 return ss.str();
147 }
148#endif // WHITEBOARD_POSTER_STRING_CONVERSION
149 };
150}
151
152#endif //NXT_Interface_DEFINED
#define PORT
Class for interacting with and reading the values of NXT sensors and motors.
void from_string(const std::string &str)
parse class properties from a string
NXT_Interface(const std::string &str)
string constructor (see from_string() below)
std::string description() const
pretty print method for showing the current property values
bool operator!=(const NXT_Interface &s)
return false if the two interfaces are the same
bool operator==(const NXT_Interface &s)
comparison operator
NXT_Interface()
default constructor
/file APM_Interface.h
An object that contains the type and data of an nxt sensor.
Interface for talking with an NXT.
@ Port1
@ NUMBER_OF_NXT_PORTS
@ Motor
@ Passive_Light_Sensor
@ Active_Light_Sensor
@ Sonar_Sensor
@ Touch_Sensor