-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmasterWire.cpp
More file actions
170 lines (146 loc) · 5.16 KB
/
Copy pathmasterWire.cpp
File metadata and controls
170 lines (146 loc) · 5.16 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
#include"masterWire.h"
int deviceAddressArray[MAX_SLAVES];
byte readBuf[32]; // max data
masterWire::masterWire(){
}
void masterWire::begin(){
}
void masterWire::begin(int sda, int scl){
}
void masterWire::reset(){
}
void masterWire::ScanDevices(uint8_t addrToScan){
uint8_t arrayPtr=0;
if(addrToScan>127){
addrToScan=127;
}
for(int i=1 ; i<=addrToScan ; i++){ //address 0 for master
Wire.beginTransmission(i);
Wrie.write(ALIVE_BEAT_REQUEST_CMD);
Wire.endTransmission();
Wire.requestFrom(i, 1 , true);
uint8_t Bit = Wire.read();
if(Bit==ALIVE_BEAT_RECEIVE_CMD){
if(arrayPtr>(MAX_SLAVES-1)){
break;
}
deviceAddressArray[arrayPtr]=i;
arrayPtr++;
}
}
}
bool masterWire::isDeviceAlive(uint8_t devID){ // devID is deviceAddressArray's pointer rainging from 1 t0 MAX_SLAVES
bool status = false;
if((devID>MAX_SLAVES)||(devID==0))return status;
Wire.beginTransmission(deviceAddressArray[devID-1]);
Wrie.write(ALIVE_BEAT_REQUEST_CMD);
Wire.endTransmission();
Wire.requestFrom(deviceAddressArray[devID-1], 1 , true);
uint8_t Bit = Wire.read();
if(Bit==ALIVE_BEAT_RECEIVE_CMD){
status=true;
}
return status;
}
//=================================================
// relay status commands
// ================================================
void masterWire::setRelayStatus(uint8_t devID, uint8_t relayID, bool status ){
}
void masterWire::getRelayStatus(uint8_t devID, uint8_t relayID){
}
void masterWire::setRelayON_AutoTurnOffTimer(uint8_t devID, uint8_t relayID, unsigned int minutes){
}
void masterWire::setRelayON_AutoTurnOffEnergy(uint8_t devID, uint8_t relayID, unsigned int KWh){
}
//=================================================
// relay energy commands specially designed for implementing in for loops
// ================================================
float masterWire::getRelayVoltage(uint8_t devID, uint8_t relayID){
uint8_t readAddress = VOLTAGE_POINTER+relayID-1;
sendData_request(devID,READ,POWER,readAddress);
}
float masterWire::getRelayCurrent(uint8_t devID, uint8_t relayID){
uint8_t readAddress = CURRENT_POINTER+relayID-1;
}
float masterWire::getRelayPower(uint8_t devID, uint8_t relayID, uint8_t powerType){
uint8_t readAddress = POWER_POINTER+powerType+(relayID-1)*3;
}
float masterWire::getRelayPowerFactor(uint8_t devID, uint8_t relayID){
uint8_t readAddress = POWER_FACTOR_POINTER+relayID-1;
}
float masterWire::getRelayHarmonics(uint8_t devID, uint8_t relayID, int harmonicType){
uint8_t readAddress = HARMONICS_POINTER+harmonicType+(relayID-1)*2;
}
unsigned long masterWire::getRelayEnergy(uint8_t devID, uint8_t relayID){
uint8_t readAddress = relayID;
}
float masterWire::getTotalPower(uint8_t devID, uint8_t powerType){
uint8_t readAddress = TOTAL_POWER_POINTER + powerType;
}
float masterWire::getTotalPowerfactor(uint8_t devID){
uint8_t readAddress = TOTAL_POWER_FACTOR;
}
unsigned long masterWire::getTotalEnergy(uint8_t devID){
uint8_t readAddress = TOTAL_ENERGY;
}
bool masterWire::checkNuteralFaliure(uint8_t devID){
// uint8_t readAddress = xyz;
}
void masterWire::sendData(uint8_t devID, uint8_t command, uint8_t regtype, int regAddr, unsigned long value, uint8_t numbytes){
byte _sendBytes[13];
_sendBytes[0]= 0x4B;
_sendBytes[1]= 0X55;
_sendBytes[2]= command;
_sendBytes[3]= regtype;
_sendBytes[4]= regAddr;
_sendBytes[5]= numbytes;
_sendBytes[6]= 0x00;
if(numbytes>2){
byte A= (value&0x000000FF);
byte B= (value&0x0000FF00)>>8;
byte C= (value&0x00FF0000)>>16;
byte D= (value&0xFF000000)>>24;
_sendBytes[7]= D;
_sendBytes[8]= C;
_sendBytes[9]= B;
_sendBytes[10]=A;
}else if(numbytes>1){
byte A= (value&0x00FF);
byte B= (value&0xFF00)>>8;
_sendBytes[7]=0x00;
_sendBytes[8]=0x00;
_sendBytes[9]= B;
_sendBytes[10]=A;
}else {
_sendBytes[7]=0x00;
_sendBytes[8]=0x00;
_sendBytes[9]=0x00;
_sendBytes[10]=value;
}
int checksum= 0x15; // will write checkum calculator @ end
_sendBytes[11]=checksum;
sendi2cCommand(devID,command,12);
}
void masterWire::sendData_request(uint8_t devID,uint8_t command, int regtype, int regAddr){
sendData( devID, command, regtype, regAddr , 0, 0);
delay(50); //safety
requestData(devID);
}
//=================================================
// sendData_request(devID,READ,POWER,readAddress,2);
// above are globlal functions below local functions
// ================================================
void masterWire::sendi2cCommand(uint8_t devID, int command[],int datalen){
Wire.beginTransmission(deviceAddressArray[devID-1]);
for(int i=0;i<datalen;i++){
Wrie.write(command[i]);
}
Wire.endTransmission();
}
void masterWire::requestData(uint8_t devID){
Wire.requestFrom(deviceAddressArray[devID-1], true);
while(Wire.available()>0){
readBuf[i] = Wire.read();
}
}