-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor2_n_udp.c
170 lines (135 loc) · 4.53 KB
/
color2_n_udp.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
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
164
165
166
167
168
169
170
#include <string.h>
#include <stdio.h>
#include <letmecreate/letmecreate.h>
#include "contiki.h"
#include "net/ip/uip.h"
#include "net/ipv6/uip-ds6.h"
#include "net/ip/uip-udp-packet.h"
#include "net/rpl/rpl.h"
PROCESS(ewdemo_process, "EW Demo");
AUTOSTART_PROCESSES(&ewdemo_process);
//---
#define LED1_PORT B
#define LED1_PIN 1
#define LED2_PORT B
#define LED2_PIN 2
//----
static char currentColour[20];
//0-none(belt/red), 1-yellow, 2-green, 3-blue, 4-orange, 5-Purple
static int8_t lastColour = 0;
static int waitTime = 1000;
bool nearColorComponent(float value, float expected) {
static const float diff = 10;
return (value >= (expected - diff)) && (value <= (expected + diff));
}
bool nearLumosityComponent(float value, float expected) {
static const float diff = 0.03f;
return (value >= (expected - diff)) && (value <= (expected + diff));
}
bool nearExtPattern(float r, float g, float b, float lum, float expR, float expG, float expB, float expLum) {
return nearColorComponent(r, expR) && nearColorComponent(g, expG) && nearColorComponent(b, expB) &&
nearLumosityComponent(lum, expLum);
}
bool nearPattern(float r, float g, float b, float expR, float expG, float expB) {
return nearColorComponent(r, expR) && nearColorComponent(g, expG) && nearColorComponent(b, expB);
}
static struct uip_udp_conn *client_conn;
static uip_ipaddr_t server_ipaddr;
void readColor() {
uint16_t clear, red, green, blue;
if (color_click_get_color(&clear, &red, &green, &blue) != 0) {
return;
}
uint16_t max = red;
max = max < green ? green : max;
max = max < blue ? blue : max;
float rcoff = 100 * ((float)red) / max;
float gcoff = 100 * ((float)green) / max;
float bcoff = 100 * ((float)blue) / max;
float lum = 0.2126f * (((float)red) / 65535.0f) +
0.7152f * (((float)green) / 65535.0f) +
0.0722f * (((float)blue) / 65535.0f);
int8_t prevCol = lastColour;
char tmpColor[15];
memset(tmpColor, 0, sizeof(tmpColor));
//detect colours
if (nearPattern(rcoff, gcoff, bcoff, 100, 56, 27)) {
//strcpy(currentColour, "yellow");
strcpy(tmpColor, "yellow");
lastColour = 1;
} else if (nearPattern(rcoff, gcoff, bcoff, 44, 100, 49)) {
//strcpy(currentColour, "green");
strcpy(tmpColor, "green");
lastColour = 2;
} else if (nearPattern(rcoff, gcoff, bcoff, 66, 88, 100)) {
//strcpy(currentColour, "blue");
strcpy(tmpColor, "blue");
lastColour = 3;
} else if (nearPattern(rcoff, gcoff, bcoff, 100, 15, 14)) {
//strcpy(currentColour, "orange");
strcpy(tmpColor, "orange");
lastColour = 4;
} else if (nearExtPattern(rcoff, gcoff, bcoff, lum, 100, 30, 30, 0.109f)) {
//strcpy(currentColour, "purple");
strcpy(tmpColor, "purple");
lastColour = 5;
} else {
//strcpy(currentColour, "none");
strcpy(tmpColor, "none");
lastColour = 0;
}
if (prevCol != lastColour) {
uip_udp_packet_sendto(client_conn, tmpColor, strlen(tmpColor), &server_ipaddr, UIP_HTONS(9955));
}
}
static void tcpip_handler(void) {
if(uip_newdata()) {
//ignore incomming data
}
}
PROCESS_THREAD(ewdemo_process, ev, data)
{
PROCESS_BEGIN();
uip_ip6addr(&server_ipaddr, GLOBAL_IPv6_ADDR1, GLOBAL_IPv6_ADDR2, GLOBAL_IPv6_ADDR3,
GLOBAL_IPv6_ADDR4, GLOBAL_IPv6_ADDR5, GLOBAL_IPv6_ADDR6, GLOBAL_IPv6_ADDR7,
1);
uip_ds6_defrt_add(&server_ipaddr, 0);
uip_nameserver_update(&server_ipaddr, UIP_NAMESERVER_INFINITE_LIFETIME);
client_conn = udp_new(NULL, UIP_HTONS(9955), NULL);
udp_bind(client_conn, UIP_HTONS(9955));
// Set colour LED to white
gpio_init(GPIO_AN);
gpio_init(GPIO_CS);
gpio_init(GPIO_PWM);
gpio_set_direction(GPIO_AN, GPIO_OUTPUT);
gpio_set_direction(GPIO_CS, GPIO_OUTPUT);
gpio_set_direction(GPIO_PWM, GPIO_OUTPUT);
gpio_set_value(GPIO_AN, 1);
gpio_set_value(GPIO_CS, 1);
gpio_set_value(GPIO_PWM, 1);
strcpy(currentColour, "none");
GPIO_CONFIGURE_AS_DIGITAL(LED1_PORT, LED1_PIN);
GPIO_CONFIGURE_AS_OUTPUT(LED1_PORT, LED1_PIN);
GPIO_CONFIGURE_AS_DIGITAL(LED2_PORT, LED2_PIN);
GPIO_CONFIGURE_AS_OUTPUT(LED2_PORT, LED2_PIN);
static struct etimer et;
i2c_init();
static int enableResult = 0;
enableResult = color_click_enable();
if (enableResult != 0) {
GPIO_SET(LED1_PORT, LED1_PIN);
GPIO_SET(LED2_PORT, LED2_PIN);
for(;;){}
}
while (1)
{
waitTime = 300;
etimer_set(&et, (waitTime * CLOCK_SECOND) / 1000);
PROCESS_YIELD();
if(ev == tcpip_event) {
tcpip_handler();
}
readColor();
}
PROCESS_END();
}