-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpinexa.c
More file actions
94 lines (77 loc) · 1.86 KB
/
pinexa.c
File metadata and controls
94 lines (77 loc) · 1.86 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
/*
* pinexa
* Copyright (C) 2017 Anders Bergh
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <wiringPi.h>
static void sendBit(int pin, int bit) {
if (bit) {
digitalWrite(pin, HIGH);
delayMicroseconds(200);
digitalWrite(pin, LOW);
delayMicroseconds(1240);
} else {
digitalWrite(pin, HIGH);
delayMicroseconds(200);
digitalWrite(pin, LOW);
delayMicroseconds(200);
}
}
/* 1 is encoded as 10, 0 as 01 */
static void sendPair(int pin, int bit) {
if (bit) {
sendBit(pin, 1);
sendBit(pin, 0);
} else {
sendBit(pin, 0);
sendBit(pin, 1);
}
}
int main(int argc, char *argv[]) {
int ledPin = 24;
int txPin = 25;
wiringPiSetupGpio();
pinMode(txPin, OUTPUT);
pinMode(ledPin, OUTPUT);
uint32_t id = 0x147914a;
int group = 0;
int onoff = 0;
int device = 1;
/* ./onoff [group] [onoff] [device] [ID] */
if (argc >= 4) {
group = atoi(argv[1]) & 0x1;
onoff = atoi(argv[2]) & 0x1;
device = atoi(argv[3]) & 0xf;
}
if (argc >= 5) {
id = atoi(argv[4]) & 0x3ffffff;
}
uint32_t msg = id << 6 | group << 5 | onoff << 4 | device;
printf("sending:\n");
printf(" id = 0x%x\n", id);
printf(" group = %s\n", group == 1 ? "yes" : "no");
printf(" on/off = %s\n", onoff == 1 ? "on" : "off");
printf(" device = %d\n", device);
/* not entirely correct but it works for me */
digitalWrite(ledPin, HIGH);
for (int i = 0; i < 4; i++) {
/* latch 1 */
digitalWrite(txPin, HIGH);
delayMicroseconds(220);
digitalWrite(txPin, LOW);
delayMicroseconds(10450);
/* latch 2 */
digitalWrite(txPin, HIGH);
delayMicroseconds(220);
digitalWrite(txPin, LOW);
delayMicroseconds(2650);
/* message */
for (int i = 0; i < 32; i++) {
sendPair(txPin, (msg >> (31-i)) & 0x1);
}
}
digitalWrite(ledPin, LOW);
return 0;
}