-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerI2C.cpp
110 lines (98 loc) · 3.25 KB
/
ServerI2C.cpp
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
#include <Arduino.h>
#include <Wire.h>
#include "ServerI2C.h"
#include "Params.h"
#include "I2CMessage.h"
namespace Ktne
{
ServerI2C::ServerI2C(uint8_t pinSsRight, uint8_t pinSsBottom) :
_pinSsRight(pinSsRight),
_pinSsBottom(pinSsBottom) { }
void ServerI2C::Setup()
{
pinMode(this->_pinSsRight, OUTPUT);
pinMode(this->_pinSsBottom, OUTPUT);
digitalWrite(this->_pinSsRight, LOW);
digitalWrite(this->_pinSsBottom, LOW);
Wire.begin();
}
ServerModule* ServerI2C::Init()
{
ServerModule* ret = NULL;
ServerModule* cur = NULL;
uint8_t id = 0x40;
ServerModule* lineParent = NULL;
for(uint8_t y=0; y<MAX_VERTICAL_COUNT_MODULE; y++)
{
ServerModule* parent = lineParent;
lineParent = NULL;
for(uint8_t x=0; x<MAX_HORIZONTAL_COUNT_MODULE; x++)
{
if (x + y == 0) continue; // Pas d'init pour le master
// On test le prochain module. Et on lui assigne un ID
ServerModule* next = this->CheckModule(x, y, id, parent);
#ifdef DEBUG
Serial.write("x=");
Serial.write(x);
Serial.write("y=");
Serial.write(y);
Serial.write("id=");
Serial.write(id);
Serial.write("result=");
Serial.write(next==NULL?"NULL":"Success");
#endif
if (x==0) lineParent = next; // Enregistre le parent de la prochaine ligne
if (next != NULL)
{
id++;
if (ret == NULL)
ret = next;
else
cur->next = next;
cur = next;
parent = next;
}
else break; // Dernier module de la ligne.
}
if (y>1 && lineParent==NULL) break;
}
return ret;
}
ServerModule* ServerI2C::CheckModule(uint8_t x, uint8_t y, uint8_t id, ServerModule* parent)
{
// Activation du SS
if (parent==NULL)
{
if (x!=0)
digitalWrite(this->_pinSsRight, HIGH);
else
digitalWrite(this->_pinSsBottom, HIGH);
}
else
this->SendMessageActiveSs(parent, x!=0);
delay(100);
return (this->SendMessageSetInfo(x, y, id)) ?
new ServerModule(x, y, id) :
NULL;
}
void ServerI2C::SendMessageActiveSs(ServerModule* dest, bool horizontal)
{
Wire.beginTransmission(dest->id);
Wire.write(ID_ACTIVE_SS);
Wire.write(horizontal?1:0);
Wire.endTransmission();
}
bool ServerI2C::SendMessageSetInfo(uint8_t x, uint8_t y,uint8_t id)
{
Wire.beginTransmission(0);
Wire.write(ID_SET_INFO);
Wire.write(x);
Wire.write(y);
Wire.write(id);
Wire.endTransmission(false);
Wire.requestFrom(id, (uint8_t)1);
if (1 >= Wire.available())
return Wire.read()==1;
return false;
}
};