JeVois Tutorials  1.22
JeVois Smart Embedded Machine Vision Tutorials
 
Share this page:
Loading...
Searching...
No Matches
ardublink.C
Go to the documentation of this file.
1// JeVois + Arduino blink for X
2
3// Pin for LED, will turn on as we detect the desired object:
4#define LEDPIN 17
5
6// Serial port to use: on chips with USB (e.g., 32u4), that usually is Serial1.
7// On chips without USB, use Serial:
8#define SERIAL Serial1
9
10// Buffer for received serial port bytes:
11#define INLEN 256
12char instr[INLEN + 1];
13
14// Our desired object: should be one of the 1000 ImageNet category names
15#define CATEGORY "computer_keyboard"
16
17void setup()
18{
19 SERIAL.begin(115200);
20 SERIAL.setTimeout(500);
21
22 pinMode(LEDPIN, OUTPUT);
23 digitalWrite(LEDPIN, HIGH);
24}
25
26void loop()
27{
28 byte len = SERIAL.readBytesUntil('\n', instr, INLEN);
29 instr[len] = 0;
30
31 char * tok = strtok(instr, " \r\n");
32 int state = 0, i; float score;
33
34 while (tok)
35 {
36 // State machine:
37 // 0: start parsing; if we get DO, move to state 1, otherwise state 1000
38 // 1: got DO, decode category name; if it is the one we want, move to state 2, otherwise stay in state 1
39 // 2: got DO followed by the category we are interested in - it's a hit!
40 // 1000: did not get DO, we stay in this state until we run out of tokens
41 switch (state)
42 {
43 // First token should be: DO
44 case 0:
45 if (strcmp(tok, "DO") == 0) state = 1; else state = 1000;
46 // We are done with this token. Break from the switch() statement
47 break;
48
49 // Second token should be: category:score
50 case 1:
51 // Find the ':' between category and score:
52 i = strlen(tok) - 1;
53 while (i >= 0 && tok[i] != ':') --i;
54
55 // If i is >= 0, we found a ':'; terminate the tok string at that ':':
56 if (i >= 0)
57 {
58 tok[i] = '\0';
59
60 // Note: we don't care about score here, but it could be obtained as:
61 score = atof(&tok[i+1]);
62 }
63
64 // Is the category name what we want?
65 if (strcmp(tok, CATEGORY) == 0) state = 2;
66
67 // We are done with this token. Break from the switch() statement
68 break;
69
70 // In any state other than 0 or 1: do nothing
71 default:
72 break;
73 }
74
75 // Move to the next token:
76 tok = strtok(0, " \r\n");
77 }
78
79 // If any of the category names in the message was the one we want, then we are in state 2 now.
80 // Otherwise we are in some other state (most likely 1).
81 if (state == 2)
82 digitalWrite(LEDPIN, LOW); // turn LED on (it has inverted logic)
83 else
84 digitalWrite(LEDPIN, HIGH); // turn LED off
85}