-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpower433ctrlite.c
More file actions
163 lines (138 loc) · 3.5 KB
/
power433ctrlite.c
File metadata and controls
163 lines (138 loc) · 3.5 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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sched.h>
#include <wiringPi.h>
#include "power433_lib.h"
struct cmd {
unsigned int sys, dev, oper;
};
/* Show help */
void help(char *progname)
{
printf("Usage:\n\t%s {gpio} {oper0} [oper1] ...\n\n", progname);
puts("Where:");
puts("\tgpio\t - GPIO pin with external RF transmitter connected (mandatory)");
puts("\toper\t - operation defined as system:device:{on|off} (at least one)");
}
/* change sheduling priority */
int changeSched(void)
{
struct sched_param s;
s.sched_priority = 0;
if(sched_setscheduler(0, SCHED_BATCH, &s))
return -1;
return 0;
}
/* finds first ':' in s, terminates s and returns pointer to rest of string */
char *splitArg(char *s)
{
int i;
for(i = 0; i < strlen(s); i++)
if (s[i] == ':') {
s[i] = 0;
if (s[i+1])
return s+i+1;
else
return NULL;
}
return s;
}
/* read number from string into variable - it supports binary as well */
unsigned int getSysNum(char *s)
{
unsigned int a;
if (strlen(s) == 5)
a = ((s[0] - '0') << 4) + ((s[1] - '0') << 3) + \
((s[2] - '0') << 2) + ((s[3] - '0') << 1) + \
(s[4] - '0');
else if (s[1] == 'x' || s[1] == 'X')
/* hex */
sscanf(s+2, "%x", &a);
else
/* decimal */
sscanf(s, "%d", &a);
return a;
}
/* read number from string into variable - it supports binary as well */
unsigned int getDevMask(char *s)
{
int i;
unsigned int a;
a = 0;
for(i = 0; i < strlen(s); i++)
a |= 1 << ((s[i] & 0x20 ? 'e' : 'E') - s[i]);
return a;
}
/* ********** */
/* * MAIN * */
/* ********** */
int main(int argc, char *argv[])
{
int i, gpio, ncode;
struct cmd *codes;
char parmbuf[16];
char *pdev, *pbtn;
/* show help */
if (argc < 3) {
help(argv[0]);
exit(0);
}
/* get parameters */
sscanf(argv[1], "%d", &gpio);
/* get command codes */
ncode = 0;
codes = (struct cmd *)malloc((argc - 2) * sizeof(struct cmd));
for(i = 0; i < argc - 2; i++) {
memset(parmbuf, 0, 16);
strncpy(parmbuf, argv[i + 2], 15);
pdev = splitArg(parmbuf);
if (!pdev)
continue;
pbtn = splitArg(pdev);
if (!pbtn)
continue;
codes[ncode].sys = getSysNum(parmbuf);
codes[ncode].dev = getDevMask(pdev);
if ((pbtn[0] | 0x20) == 'o') {
if ((pbtn[1] | 0x20) == 'n' && !pbtn[2])
codes[ncode].oper = POWER433_BUTTON_ON;
else if ((pbtn[1] | 0x20) == 'f' &&
(pbtn[2] | 0x20) == 'f')
codes[ncode].oper = POWER433_BUTTON_OFF;
else
continue;
} else
continue;
ncode++;
}
/* change scheduling priority */
if (changeSched()) {
fprintf(stderr, "Unable to change process scheduling priority: %s\n",
strerror (errno));
exit(-1);
}
/* initialize WiringPi library - use BCM GPIO numbers */
wiringPiSetupGpio();
/* set transmission only */
Power433_init(gpio, -1);
/* sending codes */
for(i = 0; i < ncode; i++) {
printf("Setting socket %s%s%s%s%s in group %d to %s.\n",
codes[i].dev & POWER433_DEVICE_A ? "A" : "",
codes[i].dev & POWER433_DEVICE_B ? "B" : "",
codes[i].dev & POWER433_DEVICE_C ? "C" : "",
codes[i].dev & POWER433_DEVICE_D ? "D" : "",
codes[i].dev & POWER433_DEVICE_E ? "E" : "",
codes[i].sys, codes[i].oper ? "ON" : "OFF");
Power433_sendCommand(codes[i].sys, codes[i].dev, codes[i].oper);
}
free(codes);
}