summaryrefslogtreecommitdiffstats
path: root/source/qFix/qfixI2CSlave.h
blob: 30151d35da2f6e9c60338e3a38b966172280d099 (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
//------------------------------------------------------------------
// qfixI2CSlave.h
//
// This is a convenience class for writing I2C slave programs
// (like slaveBoard.cc).
// This class supports the qfix I2C protocol.
//
// Copyright 2005 by KTB mechatronics GmbH
// Author: Stefan Enderle
//------------------------------------------------------------------


#ifndef qfixI2CSlave_h
#define qfixI2CSlave_h

#include "qfixI2C.h"
#include "qfixI2CDefs.h"
#include "qfixI2CMaster.h" // Only for constants


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

class I2CSlave
{
public:
  I2C i2c;

  uint8_t brdID;  // board ID (what board)
  uint8_t logID;  // logical ID (which one of these boards)
  
  void (*commandPtr)(uint8_t cmd, uint8_t* params, uint8_t len);
  void (*requestPtr)(uint8_t cmd, uint8_t* params, uint8_t len);


  uint8_t params[100];
  uint8_t data[100];

public:
  /** boardID denotes the board (BobbyBoard, LCD, ...) that 
  *   shall be used. The logical ID is set to 0.
  */
  I2CSlave(uint8_t boardID);

  /** boardID denotes the board (BobbyBoard, LCD, ...) that 
  *   shall be used. logicalID is the sub-ID (e.g. which LCD).
  */
  I2CSlave(uint8_t boardID, uint8_t logicalID);

  void setLogicalID(uint8_t logicalID);
  
  void run();
};


I2CSlave::I2CSlave(uint8_t boardID)
: i2c(), brdID(boardID), logID(0)
{
  i2c.setSlaveAdress(boardID);
}


I2CSlave::I2CSlave(uint8_t boardID, uint8_t logicalID)
: i2c(), brdID(boardID), logID(logicalID)
{
  i2c.setSlaveAdress(boardID);
}


void I2CSlave::setLogicalID(uint8_t logicalID)
{
  logID = logicalID;
}

void I2CSlave::run()
{
  while (1) {
  
    // wait for action on the I2C bus //
    while (!i2c.isAction())   { }

    // was it a command (send) //
    if (i2c.isActionSend()) { 
	
	  // receive bytes: 0=logical ID, 1=command, 2..n=params
      int len = i2c.receive(data);

	  // if logical ID matches (or 255) -> execute //
      if ( (data[0] == logID) || (data[0]==255) ) {
	  
	    // is it the first part of a request ? -> save params //
        if (data[1] == CMD_PARAMS) {
          for (int i=0; i<len; i++) {
            params[i] = data[i];
          }
        }

        // it is a command -> call the given command function //
	    else commandPtr(data[1], data+2, len-2);
	  }
    }

    // or was it a request (get) //
    else if (i2c.isActionGet()) { 
	  requestPtr(params[2], params+3, 0);  // call given rqs function
    }

    // else ERROR !
  }
}


#endif