-
Notifications
You must be signed in to change notification settings - Fork 2
/
dy01.cpp
71 lines (66 loc) · 1.27 KB
/
dy01.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
#include "dy01.h"
DY01::DY01(int pin, int transmitCount) :
mPin(pin),
mTransmitCount(transmitCount)
{
pinMode(mPin, OUTPUT);
}
void DY01::transmitBit(uint8_t b) {
digitalWrite(mPin, HIGH);
delayMicroseconds(150 + 300*b);
digitalWrite(mPin, LOW);
delayMicroseconds(450 - 300*b);
}
void DY01::control(uint16_t socketNumber, char state) {
uint16_t tmp;
int i, j;
for(j = 0; j < mTransmitCount; ++j) {
tmp = socketNumber;
// Channel number
for(i = 0; i < 5; ++i) {
transmitBit((tmp / 512) % 2);
transmitBit(1);
tmp *= 2;
}
transmitBit(0);
// Socket ID
for(i = 0; i < 5; ++i) {
transmitBit((tmp / 512) % 2);
transmitBit(0);
tmp *= 2;
}
// State
transmitBit(state);
transmitBit(0);
transmitBit(!state);
transmitBit(0);
delayMicroseconds(4600);
}
}
void DY01::control(uint16_t channelNumber, char socketLetter, char state) {
channelNumber *= 32;
switch(socketLetter) {
case 'A':
case 'a':
channelNumber += 31-16;
break;
case 'B':
case 'b':
channelNumber += 31-8;
break;
case 'C':
case 'c':
channelNumber += 31-4;
break;
case 'D':
case 'd':
channelNumber += 31-2;
break;
case 'E':
case 'e':
channelNumber += 31-1;
break;
}
Serial.println(channelNumber);
control(channelNumber, state);
}