-
Notifications
You must be signed in to change notification settings - Fork 319
/
_P100_SRF01.ino
121 lines (106 loc) · 3.98 KB
/
_P100_SRF01.ino
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
//#######################################################################################################
//#################### Plugin 100 SRF01 Ultrasonic Distance Sensor ######################################
//#######################################################################################################
#include <SoftwareSerial.h>
#define PLUGIN_100
#define PLUGIN_ID_100 100
#define PLUGIN_NAME_100 "Ultrasonic Sensor - SRF01"
#define PLUGIN_VALUENAME1_100 "Distance"
#define PLUGIN_100_srfAddress 0x01 // Address of the SFR01, default ist 0x01
#define PLUGIN_100_getSoft 0x5D // Byte to tell SRF01 we wish to read software version
#define PLUGIN_100_getRange 0x54 // Byte used to get range from SRF01 in cm
#define PLUGIN_100_getStatus 0x5F // Byte used to get the status of the transducer
SoftwareSerial *Plugin_100_SRF;
uint8_t Plugin_100_SRF_Pin;
boolean Plugin_100(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
byte timeout;
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_100;
Device[deviceCount].Type = DEVICE_TYPE_SINGLE;
Device[deviceCount].VType = SENSOR_TYPE_SINGLE;
Device[deviceCount].Ports = 0;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = true;
Device[deviceCount].ValueCount = 1;
Device[deviceCount].SendDataOption = true;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_100);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_100));
break;
}
case PLUGIN_INIT:
{
addLog(LOG_LEVEL_INFO, (char*)"INIT : SRF01");
Plugin_100_SRF_Pin = Settings.TaskDevicePin1[event->TaskIndex];
Plugin_100_SRF = new SoftwareSerial(Plugin_100_SRF_Pin, Plugin_100_SRF_Pin);
Plugin_100_SRF01_Cmd(PLUGIN_100_srfAddress, PLUGIN_100_getSoft);
for (timeout = 0; timeout < 10; timeout++) {
if (Plugin_100_SRF->available() < 1 ) {
delay(10);
} else {
int softVer = Plugin_100_SRF->read();
String log = F("SRF01 : Firmware-Version: ");
log += softVer;
addLog(LOG_LEVEL_INFO, log);
success = true;
return success;
};
};
addLog(LOG_LEVEL_ERROR, (char*)"SRF01-Init: protocol timeout!");
success = false;
break;
}
case PLUGIN_READ:
{
Plugin_100_SRF01_Cmd(PLUGIN_100_srfAddress, PLUGIN_100_getRange);
for (timeout = 0; timeout < 10; timeout++) {
if (Plugin_100_SRF->available() < 2 ) {
delay(10);
} else {
byte highByte = Plugin_100_SRF->read();
byte lowByte = Plugin_100_SRF->read();
int dist = ((highByte << 8) + lowByte);
UserVar[event->BaseVarIndex] = dist;
String log = F("SRF1 : Distance: ");
log += dist;
addLog(LOG_LEVEL_INFO, log);
success = true;
return success;
};
};
addLog(LOG_LEVEL_ERROR, (char*)"SRF01 : protocol timeout!");
UserVar[event->BaseVarIndex] = NAN;
success = false;
break;
}
}
return success;
}
//**************************************************************************/
// Send SRF01 Command
//**************************************************************************/
void Plugin_100_SRF01_Cmd(byte Address, byte cmd) {
Plugin_100_SRF->flush();
pinMode(Plugin_100_SRF_Pin, OUTPUT);
digitalWrite(Plugin_100_SRF_Pin, LOW);
delay(2);
digitalWrite(Plugin_100_SRF_Pin, HIGH);
delay(1);
Plugin_100_SRF->write(Address);
Plugin_100_SRF->write(cmd);
pinMode(Plugin_100_SRF_Pin, INPUT);
Plugin_100_SRF->flush();
}