-
-
Save maxpromer/7babfd78ce156f868d4fb63a0d51b1c8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
// ----- sscanf() function ----- | |
#include <stdio.h> | |
#include <stdarg.h> | |
#include <string.h> | |
#include <ctype.h> | |
#define MAXLN 200 | |
#define ISSPACE " \t\n\r\f\v" | |
// ----------------------------- | |
#include <Adafruit_NeoPixel.h> | |
#define PIN D1 | |
#define NUMPIXELS 8 | |
WiFiServer server(88); | |
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); | |
void setup() { | |
Serial.begin(115200); | |
WiFi.mode(WIFI_AP); | |
server.begin(); | |
pixels.begin(); | |
show(0, 0, 0); | |
} | |
void loop() { | |
WiFiClient client = server.available(); | |
if (client) { | |
Serial.println("New client"); | |
while (client.connected()) { | |
if (client.available()) { | |
String line = client.readStringUntil('\r'); | |
client.readStringUntil('\n'); | |
Serial.println(line); | |
int r = 0, g = 0, b = 0; | |
if (sscanf(line.c_str(), "%d,%d,%d", &r, &g, &b) > 0) | |
show(r, g, b); | |
} | |
delay(1); | |
} | |
delay(10); | |
client.stop(); | |
Serial.println("Client disconnect"); | |
} | |
delay(10); | |
} | |
void show(int r, int g, int b) { | |
for (int i=0;i<NUMPIXELS;i++) | |
pixels.setPixelColor(i, pixels.Color(r, g, b)); | |
pixels.show(); | |
} | |
size_t strcspn (const char *p, const char *s) { | |
int i, j; | |
for (i = 0; p[i]; i++) { | |
for (j = 0; s[j]; j++) { | |
if (s[j] == p[i]) | |
break; | |
} | |
if (s[j]) | |
break; | |
} | |
return (i); | |
} | |
char * _getbase(char *p, int *basep) { | |
if (p[0] == '0') { | |
switch (p[1]) { | |
case 'x': | |
*basep = 16; | |
break; | |
case 't': case 'n': | |
*basep = 10; | |
break; | |
case 'o': | |
*basep = 8; | |
break; | |
default: | |
*basep = 10; | |
return (p); | |
} | |
return (p + 2); | |
} | |
*basep = 10; | |
return (p); | |
} | |
/* | |
* _atob(vp,p,base) | |
*/ | |
int _atob (uint32_t *vp, char *p, int base) { | |
uint32_t value, v1, v2; | |
char *q, tmp[20]; | |
int digit; | |
if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { | |
base = 16; | |
p += 2; | |
} | |
if (base == 16 && (q = strchr (p, '.')) != 0) { | |
if (q - p > sizeof(tmp) - 1) | |
return (0); | |
strncpy (tmp, p, q - p); | |
tmp[q - p] = '\0'; | |
if (!_atob (&v1, tmp, 16)) | |
return (0); | |
q++; | |
if (strchr (q, '.')) | |
return (0); | |
if (!_atob (&v2, q, 16)) | |
return (0); | |
*vp = (v1 << 16) + v2; | |
return (1); | |
} | |
value = *vp = 0; | |
for (; *p; p++) { | |
if (*p >= '0' && *p <= '9') | |
digit = *p - '0'; | |
else if (*p >= 'a' && *p <= 'f') | |
digit = *p - 'a' + 10; | |
else if (*p >= 'A' && *p <= 'F') | |
digit = *p - 'A' + 10; | |
else | |
return (0); | |
if (digit >= base) | |
return (0); | |
value *= base; | |
value += digit; | |
} | |
*vp = value; | |
return (1); | |
} | |
/* | |
* atob(vp,p,base) | |
* converts p to binary result in vp, rtn 1 on success | |
*/ | |
int atob(uint32_t *vp, char *p, int base) { | |
uint32_t v; | |
if (base == 0) | |
p = _getbase (p, &base); | |
if (_atob (&v, p, base)) { | |
*vp = v; | |
return (1); | |
} | |
return (0); | |
} | |
/* | |
* vsscanf(buf,fmt,ap) | |
*/ | |
int vsscanf (const char *buf, const char *s, va_list ap) { | |
uint32_t count, noassign, width, base, lflag; | |
const char *tc; | |
char *t, tmp[MAXLN]; | |
count = noassign = width = lflag = 0; | |
while (*s && *buf) { | |
while (isspace (*s)) | |
s++; | |
if (*s == '%') { | |
s++; | |
for (; *s; s++) { | |
if (strchr ("dibouxcsefg%", *s)) | |
break; | |
if (*s == '*') | |
noassign = 1; | |
else if (*s == 'l' || *s == 'L') | |
lflag = 1; | |
else if (*s >= '1' && *s <= '9') { | |
for (tc = s; isdigit (*s); s++); | |
strncpy (tmp, tc, s - tc); | |
tmp[s - tc] = '\0'; | |
atob (&width, tmp, 10); | |
s--; | |
} | |
} | |
if (*s == 's') { | |
while (isspace (*buf)) | |
buf++; | |
if (!width) | |
width = strcspn (buf, ISSPACE); | |
if (!noassign) { | |
strncpy (t = va_arg (ap, char *), buf, width); | |
t[width] = '\0'; | |
} | |
buf += width; | |
} else if (*s == 'c') { | |
if (!width) | |
width = 1; | |
if (!noassign) { | |
strncpy (t = va_arg (ap, char *), buf, width); | |
t[width] = '\0'; | |
} | |
buf += width; | |
} else if (strchr ("dobxu", *s)) { | |
while (isspace (*buf)) | |
buf++; | |
if (*s == 'd' || *s == 'u') | |
base = 10; | |
else if (*s == 'x') | |
base = 16; | |
else if (*s == 'o') | |
base = 8; | |
else if (*s == 'b') | |
base = 2; | |
if (!width) { | |
if (isspace (*(s + 1)) || *(s + 1) == 0) | |
width = strcspn (buf, ISSPACE); | |
else | |
width = strchr (buf, *(s + 1)) - buf; | |
} | |
strncpy (tmp, buf, width); | |
tmp[width] = '\0'; | |
buf += width; | |
if (!noassign) | |
atob (va_arg (ap, uint32_t *), tmp, base); | |
} | |
if (!noassign) | |
count++; | |
width = noassign = lflag = 0; | |
s++; | |
} else { | |
while (isspace (*buf)) | |
buf++; | |
if (*s != *buf) | |
break; | |
else | |
s++, buf++; | |
} | |
} | |
return (count); | |
} | |
int sscanf (const char *buf, const char *fmt, ...) { | |
int count; | |
va_list ap; | |
va_start(ap, fmt); | |
count = vsscanf (buf, fmt, ap); | |
va_end(ap); | |
return count; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment