-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.c
115 lines (105 loc) · 2.91 KB
/
api.c
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
/**
* Copyright (c) Project Iota
*
* WeIO_firmware is licensed under an Apache license, version 2.0 license.
* All rights not explicitly granted in the Apache license, version 2.0 are reserved.
* See the included LICENSE file for more details.
*
* @author Paul Rathgeb <[email protected]>
*/
#include "global.h"
#include "periph/gpio.h"
uint8_t API_MODE[4] = {GPIO_IN, GPIO_OUT, GPIO_IN_PD, GPIO_IN_PU};
/* This is a private function to send back the command status */
void SendStatus(API_STATUS status)
{
char* str = NULL;
switch(status) {
case STATUS_OK:
str = "{ \"method\": \"Status\" , \"params\": [0] }";
break;
case STATUS_KO:
str = "{ \"method\": \"Status\" , \"params\": [-1] }";
break;
case STATUS_NOT_IMPLEMENTED:
str = "{ \"method\": \"Status\" , \"params\": [-2] }";
break;
case STATUS_UNRECOGNIZED:
str = "{ \"method\": \"Status\" , \"params\": [-3] }";
break;
}
stream.write(str, strlen(str));
}
void RegisterFunctions(void) {
erpcAddFunction("pinMode", pinMode);
erpcAddFunction("digitalWrite", digitalWrite);
erpcAddFunction("digitalRead", digitalRead);
}
/**
* Implementation of the Electrolink protocol, described here :
* https://github.com/projectiota/electrolink/blob/master/electrolink-protocol.md
*/
void pinMode(int argc, JSMN_PARAMS_t argv)
{
if(argc != 2) {
SendStatus(STATUS_KO);
return;
}
uint8_t pin, port, mode = 0;
int error = 0;
pin = atoi((char*)argv[0]);
mode = atoi((char*)argv[1]);
if (pin >= 32) {
port = 1;
pin -= 32;
}
error = gpio_init(GPIO_PIN(port, pin), API_MODE[mode]);
#ifdef API_DEBUG
printf("pinMode : port %d, pin %d, mode %d\n", port, pin, API_MODE[mode]);
#endif
if (!error) {
SendStatus(STATUS_OK);
}
else {
SendStatus(STATUS_KO);
}
}
void digitalWrite(int argc, JSMN_PARAMS_t argv)
{
if(argc != 2) {
SendStatus(STATUS_KO);
return;
}
uint8_t pin, port, value = 0;
pin = atoi((char*)argv[0]);
value = atoi((char*)argv[1]);
if (pin >= 32) {
port = 1;
pin -= 32;
}
#ifdef API_DEBUG
printf("DigitalWrite : port %d, pin %d, value %d\n", port, pin, value);
#endif
gpio_write(GPIO_PIN(port, pin), value);
SendStatus(STATUS_OK);
}
void digitalRead(int argc, JSMN_PARAMS_t argv)
{
if(argc != 1) {
SendStatus(STATUS_KO);
return;
}
uint8_t pin, port = 0;
pin = atoi((char*)argv[0]);
if (pin >= 32) {
port = 1;
pin -= 32;
}
#ifdef API_DEBUG
printf("digitalRead : port %d, pin %d\n", port, pin);
#endif
uint8_t value = gpio_read(GPIO_PIN(port, pin));
char str [64];
sprintf((char*) str, "{ \"method\": \"digitalRead\", \"params\": [%s, %d] }",(char*)argv[0],value);
stream.write(str, strlen(str));
}