v1.1.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Modules Pages
cube.h
1 /* -*- mode: C; c-basic-offset: 4; intent-tabs-mode: nil -*-
2  *
3  * Sifteo SDK
4  *
5  * Copyright <c> 2012 Sifteo, Inc.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  */
25 
26 #pragma once
27 #ifdef NOT_USERSPACE
28 # error This is a userspace-only header, not allowed by the current build.
29 #endif
30 
31 #include <sifteo/limits.h>
32 #include <sifteo/macros.h>
33 #include <sifteo/math.h>
34 #include <sifteo/array.h>
35 
36 namespace Sifteo {
37 
54 enum Side {
55  TOP = 0,
56  LEFT,
60  NO_SIDE = -1
61 };
62 
70 typedef _SYSCubeID PCubeID;
71 
85 struct CubeID {
86  _SYSCubeID sys;
87 
89  static const _SYSCubeID NUM_SLOTS = _SYS_NUM_CUBE_SLOTS;
90 
92  static const _SYSCubeID UNDEFINED = _SYS_CUBE_ID_INVALID;
93 
99  CubeID() : sys(UNDEFINED) {}
100 
105  CubeID(_SYSCubeID sys) : sys(sys) {}
106 
110  operator _SYSCubeID() const {
111  return sys;
112  }
113 
121  _SYSCubeIDVector bit() const {
122  return 0x80000000 >> sys;
123  }
124 
129  bool isDefined() const {
130  return sys != UNDEFINED;
131  }
132 
144  Byte3 accel() const {
145  ASSERT(sys < NUM_SLOTS);
146  _SYSByte4 v;
147  v.value = _SYS_getAccel(*this);
148  return vec(v.x, v.y, v.z);
149  }
150 
155  bool isTouching() const {
156  ASSERT(sys < NUM_SLOTS);
157  return _SYS_isTouching(*this);
158  }
159 
168  uint64_t hwID() const {
169  ASSERT(sys < NUM_SLOTS);
170  return _SYS_getCubeHWID(*this);
171  }
172 
181  void detachVideoBuffer() const {
182  ASSERT(sys < NUM_SLOTS);
183  _SYS_finish();
184  _SYS_setVideoBuffer(*this, 0);
185  }
186 
194  void detachMotionBuffer() const {
195  ASSERT(sys < NUM_SLOTS);
196  _SYS_setMotionBuffer(*this, 0);
197  }
198 
213  float batteryLevel() const {
214  ASSERT(sys < NUM_SLOTS);
215  return _SYS_cubeBatteryLevel(*this) / float(_SYS_BATTERY_MAX);
216  }
217 
223  void unpair() const {
224  ASSERT(sys < NUM_SLOTS);
225  _SYS_unpair(*this);
226  }
227 
228  CubeID operator ++() { return ++sys; }
229  CubeID operator ++(int) { return sys++; }
230  CubeID operator --() { return --sys; }
231  CubeID operator --(int) { return sys--; }
232 };
233 
234 
242 class CubeSet : public BitArray<_SYS_NUM_CUBE_SLOTS> {
243 public:
245  operator _SYSCubeIDVector() const {
246  return words[0];
247  }
248 
250  CubeSet(const BitArray<_SYS_NUM_CUBE_SLOTS> &bits) : BitArray<_SYS_NUM_CUBE_SLOTS>(bits) {}
251 
253  explicit CubeSet() : BitArray<_SYS_NUM_CUBE_SLOTS>() {}
254 
256  explicit CubeSet(CubeID cube) : BitArray<_SYS_NUM_CUBE_SLOTS>(cube) {}
257 
263  explicit CubeSet(CubeID begin, CubeID end) : BitArray<_SYS_NUM_CUBE_SLOTS>(begin, end) {}
264 
270  uint32_t mask() const {
271  return words[0];
272  }
273 
280  void setMask(uint32_t mask) {
281  ASSERT( (mask & ((1<<(32-CUBE_ALLOCATION))-1)) == 0 );
282  words[0] = mask;
283  }
284 
298  static CubeSet connected() {
299  CubeSet result;
300  result.words[0] = _SYS_getConnectedCubes();
301  return result;
302  }
303 };
304 
305 
314 struct NeighborID {
315  _SYSNeighborID sys;
316 
320  NeighborID() : sys(_SYS_NEIGHBOR_NONE) {}
321 
325  NeighborID(_SYSNeighborID sys) : sys(sys) {}
326 
330  operator _SYSNeighborID() const {
331  return sys;
332  }
333 
335  bool isCube() const {
336  return sys < _SYS_NUM_CUBE_SLOTS;
337  }
338 
340  bool isBase() const {
341  return (sys & _SYS_NEIGHBOR_TYPE_MASK) == _SYS_NEIGHBOR_BASE;
342  }
343 
345  bool isEmpty() const {
346  return sys == _SYS_NEIGHBOR_NONE;
347  }
348 
354  CubeID cube() const {
355  return isCube() ? CubeID(sys) : CubeID();
356  }
357 };
358 
359 
368 struct Neighborhood {
369  _SYSNeighborState sys;
370 
375 
379  Neighborhood(_SYSNeighborState sys) : sys(sys) {}
380 
385  operator _SYSNeighborState& () {
386  return sys;
387  }
388 
397  ASSERT(cube < cube.NUM_SLOTS);
398  sys.value = _SYS_getNeighbors(cube);
399  }
400 
404  NeighborID neighborAt(Side side) const {
405  ASSERT(side >= 0 && side < NUM_SIDES);
406  return sys.sides[side];
407  }
408 
415  CubeID cubeAt(Side side) const {
416  return neighborAt(side).cube();
417  }
418 
423  bool hasNeighborAt(Side side) const {
424  return !neighborAt(side).isEmpty();
425  }
426 
431  bool hasCubeAt(Side side) const {
432  return neighborAt(side).isCube();
433  }
434 
442  Side sideOf(CubeID cube) const {
443  for (Side side = (Side)0; side < NUM_SIDES; ++side)
444  if (sys.sides[side] == cube)
445  return side;
446  return NO_SIDE;
447  }
448 };
449 
454 }; // namespace Sifteo
NeighborID()
Default constructor. Initializes an empty NeighborID.
Definition: cube.h:320
CubeSet()
Create an empty CubeSet.
Definition: cube.h:253
static const _SYSCubeID NUM_SLOTS
The maximum number of distinct CubeIDs.
Definition: cube.h:89
float batteryLevel() const
Get this cube's battery level.
Definition: cube.h:213
An unordered set of cubes.
Definition: cube.h:242
Byte3 accel() const
Return the physical accelerometer state, as a signed byte-vector.
Definition: cube.h:144
static CubeSet connected()
Return a CubeSet containing all connected cubes which are visible to the current application.
Definition: cube.h:298
bool hasCubeAt(Side side) const
Is there a cube at this side? This is equivalent to calling isCube() on the result of neighborAt()...
Definition: cube.h:431
CubeID cube() const
Convert this NeighborID to a CubeID.
Definition: cube.h:354
#define ASSERT(_x)
Runtime debug assertion.
Definition: macros.h:205
Neighborhood()
Default constructor. Leaves the Neighborhood uninitialized.
Definition: cube.h:374
bool hasNeighborAt(Side side) const
Is there a neighbor at this side? This is equivalent to calling !isEmpty() on the result of neighborA...
Definition: cube.h:423
CubeID(_SYSCubeID sys)
Initialize a CubeID with a concrete slot value. Slots are numbered from 0 to NUM_SLOTS - 1...
Definition: cube.h:105
void unpair() const
Remove the persistent pairing association between this cube and the current Base. ...
Definition: cube.h:223
Side
An enumeration which names the four sides of a Sifteo cube.
Definition: cube.h:54
uint64_t hwID() const
Return the cube's unique 64-bit hardware ID. This ID uniquely identifies the cube that this slot is p...
Definition: cube.h:168
void setMask(uint32_t mask)
Setter for the underlying bitmask.
Definition: cube.h:280
bool isEmpty() const
Is there nothing neighbored at all?
Definition: cube.h:345
A lightweight identifier for one neighbored object.
Definition: cube.h:314
Side sideOf(CubeID cube) const
Search for a CubeID in this Neighborhood.
Definition: cube.h:442
bool isTouching() const
Is this cube being touched right now? Return the current state of the touch sensor.
Definition: cube.h:155
CubeSet(CubeID begin, CubeID end)
Create a new CubeSet with a range of cubes.
Definition: cube.h:263
#define CUBE_ALLOCATION
Compile-time cube limit for the current application.
Definition: limits.h:54
Nil value (-1)
Definition: cube.h:60
Bottom side (Y+)
Definition: cube.h:57
NeighborID(_SYSNeighborID sys)
Initialize a NeighborID with a concrete value.
Definition: cube.h:325
A lightweight identifier for one Sifteo cube.
Definition: cube.h:85
_SYSCubeID PCubeID
Alternate POD type for CubeID storage.
Definition: cube.h:70
_SYSCubeIDVector bit() const
Return the _SYSCubeIDVector bit associated with this ID.
Definition: cube.h:121
Neighborhood(_SYSNeighborState sys)
Initialize a Neighborhood from a low-level _SYSNeighborState object.
Definition: cube.h:379
CubeID cubeAt(Side side) const
Return the neighbor at a particular side, as a CubeID.
Definition: cube.h:415
CubeSet(CubeID cube)
Create a CubeSet with a single CubeID in it.
Definition: cube.h:256
A Neighborhood is a description of all neighbors for a single cube, packed into a small value...
Definition: cube.h:368
CubeID()
Default constructor. By default, a CubeID is initialized to a special Undefined value. This value can be tested for with the isDefined() predicate.
Definition: cube.h:99
Definition: array.h:34
Left side (X-)
Definition: cube.h:56
Right side (X+)
Definition: cube.h:58
CubeSet(const BitArray< _SYS_NUM_CUBE_SLOTS > &bits)
Implicit conversion from a BitArray of the correct size.
Definition: cube.h:250
NeighborID neighborAt(Side side) const
Return the neighbor at a particular side, as a NeighborID.
Definition: cube.h:404
Total number of sides (4)
Definition: cube.h:59
iterator end() const
Return an STL-style iterator for this array.
Definition: array.h:499
bool isCube() const
Is this neighbor a cube?
Definition: cube.h:335
Top side (Y-)
Definition: cube.h:55
void detachMotionBuffer() const
Detach any motion buffer which was previously attached to this cube.
Definition: cube.h:194
void detachVideoBuffer() const
Detach any video buffer which was previously attached to this cube.
Definition: cube.h:181
bool isBase() const
Is this neighbor a base?
Definition: cube.h:340
iterator begin() const
Return an STL-style iterator for this array.
Definition: array.h:492
static const _SYSCubeID UNDEFINED
A reserved ID, used to mark undefined CubeIDs.
Definition: cube.h:92
bool isDefined() const
Is this a CubeID that was initialized with a valid slot number, and not one that was initialized as u...
Definition: cube.h:129
Neighborhood(CubeID cube)
Get a Neighborhood representing the physical neighbors for a cube.
Definition: cube.h:396
Vector2< T > vec(T x, T y)
Create a Vector2, from a set of (x,y) coordinates.
Definition: math.h:658
uint32_t mask() const
Getter for the underlying bitmask.
Definition: cube.h:270
A fixed-size array of bits, with compact storage and fast iteration.
Definition: array.h:231