libpropeller
Making PropellerGCC Easier
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
i2cMOCK.h
Go to the documentation of this file.
1 #ifndef SRLM_PROPGCC_I2C_MOCK_H__
2 #define SRLM_PROPGCC_I2C_MOCK_H__
3 
4 #include <stdio.h>
5 
6 #ifdef UNIT_TEST
7 
8 /*
9 
10 Notes:
11 - This is a MOCK object to allow for unit testing of the L3GD20 object.
12 
13  */
14 class I2C {
15 public:
16 
17 
18  // static const int kAck = 1;
19  // static const int kNak = 0;
20 
21  char putStack[100];
22  int putStackTop;
23 
24 
25  char xyz[6];
26 
27  void Init(const int SCLPin, const int SDAPin) {
28  putStackTop = -1;
29  }
30 
31  bool Ping(unsigned char device) {
32  return true;
33  }
34 
35  bool Put(unsigned char device, unsigned char address, char byte) {
36  putStack[++putStackTop] = byte;
37  return true;
38  }
39 
40  //TODO(SRLM): For some reason, this function locks up in the for loop when optimized.
41 #pragma GCC optimize ("0")
42 
43  bool Get(unsigned char device, unsigned char address, char * bytes, int size) {
44  if (address == (0x28 | 0x80)) //OUT_X_L with autoincrement bit set
45  {
46  for (volatile int i = 0; i < size; ++i) {
47  bytes[i] = xyz[i];
48  // printf("\nbytes[%i] = 0x%X ", i, bytes[i]);
49  }
50  } else if (address == (0x03 | 0x80)) //OUT_X_L with autoincrement bit set
51  {
52  for (volatile int i = 0; i < size; ++i) {
53  bytes[i] = xyz[i];
54  // printf("\nbytes[%i] = 0x%X ", i, bytes[i]);
55  }
56  }
57  else {
58  return false;
59  }
60 
61  return true;
62 
63  }
64 
65  // -----------------------------------------------------------------------------
66 
67  int GetPutStack() {
68  if (putStackTop == -1) return -1;
69  else return putStack[putStackTop--];
70  }
71 
72  void SetXYZ(char * bytes, int size) {
73  for (int i = 0; i < size; i++) {
74  xyz[i] = bytes[i];
75  // printf("\nxyz[%i] = 0x%X ", i, xyz[i]);
76  }
77  }
78 };
79 
80 #endif // Unit_Test
81 
82 #endif // SRLM_PROPGCC_I2C_MOCK_H__