summaryrefslogtreecommitdiffstats
path: root/source/qFix/qfixSlaveBoard.h
blob: ad781de552f8ea3925dc115963a37c6ce51735d5 (plain)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//------------------------------------------------------------------
// qfixSlaveBoard.h
//
// This file contains the class SlaveBoard which represents a kind
// of remote control of a BobbyBoard via the I2C bus. The SlaveBoard
// can be used like a local BobbyBoard with all its inputs and outputs.
//
// Copyright 2005 by KTB mechatronics GmbH
// Author: Stefan Enderle
//------------------------------------------------------------------


#ifndef qfixSlaveBoard_h
#define qfixSlaveBoard_h

#include "qfixI2CMaster.h"
#include "qfixI2CDefs.h"


const uint8_t CMD_LEDON    = 0;
const uint8_t CMD_LEDOFF   = 1;
const uint8_t CMD_LEDSOFF  = 2;
const uint8_t CMD_POWERON  = 3;
const uint8_t CMD_POWEROFF = 4;
const uint8_t CMD_MOTOR    = 5;
const uint8_t CMD_BUTTON   = 6;
const uint8_t CMD_ANALOG   = 7;
const uint8_t CMD_DIGITAL  = 8;


//----------------------------------------



class SlaveBoard : I2CMaster
{
public:
  SlaveBoard();
  SlaveBoard(uint8_t logicalID);

  /** Puts on LED i 
  */
  void ledOn(uint8_t i);

  /** Puts off LED i
  */
  void ledOff(uint8_t i);

  /** Puts off all LEDs
  */
  void ledsOff();

  /** Puts the power output i on
  */
  void powerOn(uint8_t i);

  /** Puts the power output i off
  */
  void powerOff(uint8_t i);

  /** Sets motor i to the given speed. -255 <= speed <= 255
  */
  void motor(uint8_t i, int speed);

  /** Checks the state of button i. If it is pressed, true is returned,
   *  else false.
  */
  bool button(uint8_t i);

  /** returns the value of the analog port i. 0 <= value <= 255
  */
  int analog(uint8_t i);

  /** returns true if the digital port is logical high, else false.
  */
  bool digital(uint8_t i);
};


SlaveBoard::SlaveBoard() : I2CMaster(I2C_ADDR_BOBBYBOARD)
{
}


SlaveBoard::SlaveBoard(uint8_t logicalID) 
: I2CMaster(I2C_ADDR_BOBBYBOARD, logicalID)
{
}


void SlaveBoard::ledOn(uint8_t i)
{
  if  (i>3) return;
  command(CMD_LEDON, i);
}


void SlaveBoard::ledOff(uint8_t i)
{
  if  (i>3) return;
  command(CMD_LEDOFF, i);
}


void SlaveBoard::ledsOff()
{
  command(CMD_LEDSOFF);
}


void SlaveBoard::powerOn(uint8_t i)
{
  if  (i>7) return;
  command(CMD_POWERON, i);
}


void SlaveBoard::powerOff(uint8_t i)
{
  if  (i>7) return;
  command(CMD_POWEROFF, i);
}


void SlaveBoard::motor(uint8_t i, int speed)
{
  if (i>1) return;
  uint8_t neg=(speed<0)?1:0;
  uint8_t s;
  if (speed>=0) s=uint8_t(speed); else s=-uint8_t(speed);
  command(CMD_MOTOR, i, neg, s);
}


bool SlaveBoard::button(uint8_t i)
{
  if  (i>3) return false;    // better error handling!
  return (request(CMD_BUTTON, i) == 1);
}


int SlaveBoard::analog(uint8_t i)
{
  if  (i>3) return -1;    // better error handling!
  return (request(CMD_ANALOG, i));
}


bool SlaveBoard::digital(uint8_t i)
{
  if  (i>3) return false;    // better error handling!
  return (request(CMD_DIGITAL, i) == 1);
}


#endif