-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommonData.cpp
More file actions
82 lines (68 loc) · 2.35 KB
/
CommonData.cpp
File metadata and controls
82 lines (68 loc) · 2.35 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
/*
* Copyright (c) 2012 Yeelink.net by dapingliu <dapingliu@yeelink.net>
*
* This file contains the class for common data storage. The common data
* is used in the whole project to store public data and give access to
* all the components; also component can share data each other via this
* method.
*/
#include "CommonData.h"
#include <avr/EEPROM.h>
void CommonData::setup() {
//Read devicelist number from EEPROM
this->deviceCount = EepromManage::getEepromDeviceNum();
EepromManage::getEepromMac(this->mac);
EepromManage::getEepromBoxKey(this->key);
EepromManage::getEepromVersion(this->version);
//Update devicelist from EEPROM
if(this->deviceCount > 0){
eeprom_device_list_t deviceList[this->deviceCount];
EepromManage::getEepromDeviceList(deviceList, this->deviceCount);
for(unsigned short i=0; i<this->deviceCount; i++){
this->deviceList[i].type = deviceList[i].type;
strncpy(this->deviceList[i].MAC, (char*)(deviceList[i].mac), DEVICE_IP_LEN);
this->deviceList[i].R = 255;
this->deviceList[i].G = 255;
this->deviceList[i].B = 255;
this->deviceList[i].L = 100;
this->deviceList[i].effect = 0;
this->deviceList[i].online = false;
this->deviceList[i].powerStatus = 0;
this->deviceList[i].LQI = 0;
}
}
this->_waitStartTime = 0;
}
bool CommonData::addNewDevice(char* mac, uint8_t type){
eeprom_device_list_t deviceList;
// If max lamps exceed or invalid mac, just return
if(this->deviceCount >= MAX_SUPPORTED_LAMPS || mac == NULL)
return false;
// If the mac is already exists, just return
for(int i=0; i<this->deviceCount; i++) {
if(0 == strncmp(mac, this->deviceList[i].MAC, DEVICE_IP_LEN)) {
return false;
}
}
// If the mac is illegal,just return
for(int j = 0;j < DEVICE_IP_LEN; j++){
if(false == isNumorLetter(mac+j)){
return false;
}
}
// Add new device in DeviceList
(void)strncpy(this->deviceList[this->deviceCount].MAC, mac, E2DA_ZIGBEE_MAC_LEN);
this->deviceList[this->deviceCount].type = type;
this->deviceCount++;
// Add new device to EEPROM
deviceList.type = (unsigned short)type;
(void)strncpy((char*)deviceList.mac, mac, E2DA_ZIGBEE_MAC_LEN);
EepromManage::AddEepromDeviceList(deviceList);
return true;
}
bool CommonData::isNumorLetter(char * c){
if( (*c >= '0' && *c <= '9') || (*c >= 'a' && *c <= 'f') || (*c >= 'A' && *c <= 'F') )
return true;
else
return false;
}