Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.07 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <DNSServer.h>
  3. #include <ESP8266WebServer.h>
  4. #include <Adafruit_NeoPixel.h>
  5.  
  6. #define PIN_BUTTON D2
  7. #define PIN_LED D1
  8. #define NUM_LEDS 256
  9. #define ROWS 8
  10. #define COLS 32
  11.  
  12. #define MODE_COUNT 2
  13. #define MODE_DRAW 0
  14. #define MODE_OFFLINE 1
  15.  
  16. // Neopixel object
  17. Adafruit_NeoPixel leds = Adafruit_NeoPixel(NUM_LEDS, PIN_LED, NEO_GRB + NEO_KHZ800);
  18.  
  19. int mode_index = 0;
  20.  
  21. byte brightness = 80; // default brightness: 48
  22. uint32_t pixels[NUM_LEDS]; // pixel buffer. this buffer allows you to set arbitrary
  23. // brightness without destroying the original color values
  24.  
  25. unsigned long button_last = 0;
  26. bool initial_demo = true;
  27. bool enableLEDs = false;
  28.  
  29. // Wifi Setup
  30. // Based on stuff from https://www.hackster.io/rayburne/esp8266-captive-portal-5798ff
  31. const bool apMode = true;
  32. const char * ap_name = "DAX";
  33. const byte DNS_PORT = 53; // Capture DNS requests on port 53
  34. IPAddress apIP(1, 3, 3, 7); // Private network for server
  35. DNSServer dnsServer; // Create the DNS object
  36. ESP8266WebServer server(80); // HTTP server
  37.  
  38. // Wifi setup for connecting to an access point
  39. char* ssid = "DAX";
  40. char* password = "Something1";
  41. bool staticIP = false;
  42. IPAddress ip(10, 42, 0, 200); // where xx is the desired IP Address
  43. IPAddress gateway(10, 42, 0, 1); // set gateway to match your network
  44. IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your
  45.  
  46.  
  47.  
  48. // Forward declare functions
  49. void button_handler();
  50. void on_status();
  51. void on_change_color();
  52. void on_homepage();
  53. void show_leds();
  54.  
  55.  
  56. const byte buffer_eyes[] = {
  57. 0, 0, 0, 0, 0, 0, 0, 0,
  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0, 0, 0,
  97. 0, 0, 0, 0, 0, 0, 0, 0,
  98. 0, 0, 0, 0, 0, 0, 0, 0,
  99. 0, 0, 0, 0, 0, 0, 0, 0,
  100. 0, 0, 0, 0, 0, 0, 0, 0,
  101. 0, 0, 0, 0, 0, 0, 0, 0,
  102. 0, 0, 0, 0, 0, 0, 0, 0,
  103. 0, 0, 0, 0, 0, 0, 0, 0,
  104. 0, 0, 0, 0, 0, 0, 0, 0,
  105. 0, 0, 0, 0, 0, 0, 0, 0,
  106. 0, 0, 0, 0, 0, 0, 0, 0,
  107. 0, 0, 0, 0, 0, 0, 0, 0,
  108. 0, 0, 0, 0, 0, 0, 0, 0,
  109. 0, 0, 0, 0, 0, 0, 0, 0,
  110. 0, 0, 1, 0, 0, 1, 0, 0,
  111. 0, 1, 1, 1, 1, 1, 1, 0,
  112. 0, 1, 1, 1, 1, 1, 1, 0,
  113. 0, 1, 1, 1, 1, 1, 1, 0,
  114. 0, 0, 1, 1, 1, 1, 0, 0,
  115. 0, 0, 0, 1, 1, 0, 0, 0,
  116. 0, 0, 0, 0, 0, 0, 0, 0,
  117. 0, 0, 0, 0, 0, 0, 0, 0,
  118. 0, 0, 0, 0, 0, 0, 0, 0,
  119. 0, 0, 0, 0, 0, 0, 0, 0,
  120. 0, 0, 0, 0, 0, 0, 0, 0,
  121. 0, 0, 0, 0, 0, 0, 0, 0,
  122. 0, 0, 0, 0, 0, 0, 0, 0,
  123. 0, 0, 0, 0, 0, 0, 0, 0
  124. };
  125.  
  126.  
  127. const byte buffer_question[] = {
  128. 0, 0, 0, 0, 0, 0, 0, 0,
  129. 0, 0, 0, 0, 0, 0, 0, 0,
  130. 0, 0, 0, 0, 0, 0, 0, 0,
  131. 0, 0, 0, 0, 0, 0, 0, 0,
  132. 0, 0, 0, 0, 0, 0, 0, 0,
  133. 0, 0, 0, 0, 0, 0, 0, 0,
  134. 0, 0, 0, 0, 0, 0, 0, 0,
  135. 0, 0, 0, 0, 0, 0, 0, 0,
  136. 0, 0, 0, 0, 0, 0, 0, 0,
  137. 0, 0, 0, 0, 0, 0, 0, 0,
  138. 0, 0, 0, 0, 0, 0, 0, 0,
  139. 0, 0, 0, 0, 0, 0, 0, 0,
  140. 0, 0, 0, 0, 0, 0, 0, 0,
  141. 0, 0, 0, 0, 0, 0, 0, 0,
  142. 0, 0, 1, 1, 1, 1, 0, 0,
  143. 0, 1, 1, 1, 1, 1, 1, 0,
  144. 0, 1, 1, 0, 0, 1, 1, 0,
  145. 0, 1, 1, 0, 0, 1, 1, 0,
  146. 0, 0, 0, 0, 0, 1, 1, 0,
  147. 0, 0, 0, 1, 1, 1, 1, 0,
  148. 0, 0, 0, 1, 1, 1, 0, 0,
  149. 0, 0, 0, 1, 1, 0, 0, 0,
  150. 0, 0, 0, 1, 1, 0, 0, 0,
  151. 0, 0, 0, 0, 0, 0, 0, 0,
  152. 0, 0, 0, 1, 1, 0, 0, 0,
  153. 0, 0, 0, 1, 1, 0, 0, 0,
  154. 0, 0, 0, 0, 0, 0, 0, 0,
  155. 0, 0, 0, 0, 0, 0, 0, 0,
  156. 0, 0, 0, 0, 0, 0, 0, 0,
  157. 0, 0, 0, 0, 0, 0, 0, 0,
  158. 0, 0, 0, 0, 0, 0, 0, 0,
  159. 0, 0, 0, 0, 0, 0, 0, 0
  160. };
  161.  
  162. const byte buffer_exclamation[] = {
  163. 0, 0, 0, 0, 0, 0, 0, 0,
  164. 0, 0, 0, 0, 0, 0, 0, 0,
  165. 0, 0, 0, 0, 0, 0, 0, 0,
  166. 0, 0, 0, 0, 0, 0, 0, 0,
  167. 0, 0, 0, 0, 0, 0, 0, 0,
  168. 0, 0, 0, 0, 0, 0, 0, 0,
  169. 0, 0, 0, 0, 0, 0, 0, 0,
  170. 0, 0, 0, 0, 0, 0, 0, 0,
  171. 0, 0, 0, 0, 0, 0, 0, 0,
  172. 0, 0, 0, 0, 0, 0, 0, 0,
  173. 0, 0, 0, 0, 0, 0, 0, 0,
  174. 0, 0, 0, 0, 0, 0, 0, 0,
  175. 0, 0, 0, 0, 0, 0, 0, 0,
  176. 0, 0, 0, 1, 1, 0, 0, 0,
  177. 0, 0, 0, 1, 1, 0, 0, 0,
  178. 0, 0, 0, 1, 1, 0, 0, 0,
  179. 0, 0, 0, 1, 1, 0, 0, 0,
  180. 0, 0, 0, 1, 1, 0, 0, 0,
  181. 0, 0, 0, 1, 1, 0, 0, 0,
  182. 0, 0, 0, 1, 1, 0, 0, 0,
  183. 0, 0, 0, 1, 1, 0, 0, 0,
  184. 0, 0, 0, 1, 1, 0, 0, 0,
  185. 0, 0, 0, 1, 1, 0, 0, 0,
  186. 0, 0, 0, 1, 1, 0, 0, 0,
  187. 0, 0, 0, 0, 0, 0, 0, 0,
  188. 0, 0, 0, 0, 0, 0, 0, 0,
  189. 0, 0, 0, 1, 1, 0, 0, 0,
  190. 0, 0, 0, 1, 1, 0, 0, 0,
  191. 0, 0, 0, 0, 0, 0, 0, 0,
  192. 0, 0, 0, 0, 0, 0, 0, 0,
  193. 0, 0, 0, 0, 0, 0, 0, 0,
  194. 0, 0, 0, 0, 0, 0, 0, 0
  195. };
  196.  
  197.  
  198. const byte buffer_sad[] = {
  199. 0, 0, 0, 0, 0, 0, 0, 0,
  200. 0, 0, 0, 0, 0, 0, 0, 0,
  201. 0, 0, 0, 0, 0, 0, 0, 0,
  202. 0, 0, 0, 0, 0, 0, 0, 0,
  203. 0, 0, 0, 0, 0, 0, 0, 0,
  204. 0, 0, 0, 0, 0, 0, 0, 0,
  205. 0, 0, 0, 0, 0, 0, 0, 0,
  206. 0, 0, 0, 0, 0, 0, 0, 0,
  207. 0, 0, 0, 0, 0, 0, 0, 0,
  208. 0, 0, 0, 0, 0, 0, 0, 0,
  209. 0, 0, 0, 0, 0, 0, 0, 0,
  210. 0, 0, 0, 0, 0, 0, 0, 0,
  211. 0, 0, 0, 0, 0, 0, 0, 0,
  212. 0, 0, 0, 0, 0, 0, 0, 0,
  213. 0, 0, 1, 0, 0, 1, 0, 0,
  214. 0, 1, 0, 0, 0, 0, 1, 0,
  215. 1, 0, 0, 0, 0, 0, 0, 1,
  216. 0, 0, 0, 0, 0, 0, 0, 0,
  217. 1, 1, 0, 0, 0, 0, 1, 1,
  218. 1, 1, 0, 0, 0, 0, 1, 1,
  219. 1, 1, 0, 0, 0, 0, 1, 1,
  220. 1, 1, 0, 0, 0, 0, 1, 1,
  221. 1, 1, 0, 0, 0, 0, 1, 1,
  222. 1, 1, 0, 0, 0, 0, 1, 1,
  223. 1, 1, 0, 0, 0, 0, 1, 1,
  224. 0, 0, 0, 0, 0, 0, 0, 0,
  225. 0, 0, 0, 0, 0, 0, 0, 0,
  226. 0, 0, 0, 0, 0, 0, 0, 0,
  227. 0, 0, 0, 0, 0, 0, 0, 0,
  228. 0, 0, 0, 0, 0, 0, 0, 0,
  229. 0, 0, 0, 0, 0, 0, 0, 0,
  230. 0, 0, 0, 0, 0, 0, 0, 0
  231. };
  232.  
  233. void show_leds() {
  234. uint32_t r, g, b;
  235. uint32_t c;
  236. for (int i = 0; i < NUM_LEDS; i++) {
  237. r = (pixels[i] >> 16) & 0xFF;
  238. g = (pixels[i] >> 8) & 0xFF;
  239. b = (pixels[i]) & 0xFF;
  240. r = r * brightness / 255;
  241. g = g * brightness / 255;
  242. b = b * brightness / 255;
  243. c = (r << 16) + (g << 8) + b;
  244. leds.setPixelColor(i, c);
  245. }
  246. if (enableLEDs) {
  247. leds.show();
  248. }
  249. }
  250.  
  251. void setup() {
  252. Serial.begin(115200);
  253. Serial.println();
  254. Serial.println("Led Drawing 16x16");
  255.  
  256. // Delay for recovery
  257. delay(3000);
  258.  
  259. // Set pin mode
  260. pinMode(PIN_BUTTON, INPUT_PULLUP);
  261. pinMode(PIN_LED, OUTPUT);
  262.  
  263. // If button is pressed when starting. Dont show LEDs.
  264. // Useful for flashing by USB where you dont want to connect 5V
  265. enableLEDs = digitalRead(PIN_BUTTON); // PULLUP: Normally High
  266.  
  267. if (enableLEDs) {
  268. Serial.println("LED output is enabled");
  269. } else {
  270. Serial.println("Button pressed! LED output is disabled");
  271. }
  272.  
  273. // Initialize LEDs
  274. if (enableLEDs) {
  275. leds.begin();
  276. leds.show();
  277. }
  278.  
  279. // Set WiFi
  280. if (apMode) {
  281. WiFi.mode(WIFI_AP);
  282. WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
  283. WiFi.softAP(ap_name, password);
  284.  
  285. // if DNSServer is started with "*" for domain name, it will reply with
  286. // provided IP to all DNS request
  287. dnsServer.start(DNS_PORT, "*", apIP);
  288. } else {
  289. if (staticIP) {
  290. Serial.print(F("Setting static ip to: "));
  291. Serial.println(ip);
  292. WiFi.config(ip, gateway, subnet);
  293. }
  294.  
  295. WiFi.mode(WIFI_STA);
  296. Serial.printf("Connecting to %s\n", ssid);
  297. if (String(WiFi.SSID()) != String(ssid)) {
  298. WiFi.begin(ssid, password);
  299. }
  300.  
  301. while (WiFi.status() != WL_CONNECTED) {
  302. delay(500);
  303. Serial.print(".");
  304. }
  305.  
  306. Serial.print("Connected! Open http://");
  307. Serial.print(WiFi.localIP());
  308. Serial.println(" in your browser");
  309. }
  310.  
  311.  
  312. // Set server callback functions
  313. server.onNotFound(on_homepage);
  314. server.on("/", on_homepage);
  315. server.on("/js", on_status);
  316. server.on("/cc", on_change_color);
  317. server.begin();
  318.  
  319. // Set button handler
  320. attachInterrupt(PIN_BUTTON, button_handler, FALLING);
  321.  
  322. }
  323.  
  324. // The variable below is modified by interrupt service routine
  325. // so declare it as volatile
  326. volatile boolean button_clicked = false;
  327.  
  328. void loop() {
  329. if (apMode) {
  330. dnsServer.processNextRequest();
  331. }
  332.  
  333. server.handleClient();
  334.  
  335. if (initial_demo) {
  336. display_artifactory_logo();
  337. initial_demo = false;
  338. }
  339.  
  340. if (initial_demo || mode_index == MODE_OFFLINE) {
  341. static int delayer = 100;
  342. delayer -= 1;
  343. if (delayer == 0) {
  344. display_artifactory_logo();
  345. delayer = 100;
  346. }
  347. delayMicroseconds(100);
  348. }
  349.  
  350. if (button_clicked) {
  351. if (abs(millis() - button_last) > 1000) {
  352. mode_index += 1;
  353. mode_index %= MODE_COUNT;
  354. Serial.print("Mode changed to ");
  355. Serial.println(mode_index);
  356. button_last = millis();
  357. }
  358. button_clicked = false;
  359. }
  360. }
  361.  
  362. uint32_t wheel(int WheelPos) {
  363. WheelPos = 255 - WheelPos;
  364. if (WheelPos < 85) {
  365. return leds.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  366. }
  367. if (WheelPos < 170) {
  368. WheelPos -= 85;
  369. return leds.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  370. }
  371. WheelPos -= 170;
  372. return leds.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  373. }
  374.  
  375. // Take an index for a normal c++ array layout and
  376. // flip it to match led connection order
  377. int remap_led_index(int input_index) {
  378. // Alternate rows are connected in different direction
  379. // (snake scheme)
  380. int c = 15 - input_index / 16, r = input_index % 16;
  381. int index = 0;
  382. if (r % 2 == 1) {
  383. index = c + r * 16;
  384. } else {
  385. index = (15 - c) + r * 16;
  386. }
  387. return index;
  388. }
  389.  
  390. void display_buffer(String buffer)
  391. {
  392. for (int r = 0; r < ROWS; r++)
  393. {
  394. for (int c = 0; c < COLS; c++)
  395. {
  396. uint32_t index = c*ROWS+r;
  397. uint32_t cyan = leds.Color(0, 255, 255);
  398. uint32_t red = leds.Color(255, 0, 0);
  399. uint32_t purple = leds.Color(255, 136, 255);
  400.  
  401. if (buffer == "eyes" && buffer_eyes[index]) leds.setPixelColor(index, cyan);
  402. if (buffer == "heart" && buffer_heart[index]) leds.setPixelColor(index, purple);
  403. if (buffer == "question" && buffer_question[index]) leds.setPixelColor(index, cyan);
  404. if (buffer == "exclamation" && buffer_exclamation[index]) leds.setPixelColor(index, red);
  405. if (buffer == "sad" && buffer_sad[index]) leds.setPixelColor(index, cyan);
  406. }
  407. }
  408.  
  409. if (enableLEDs) {
  410. leds.show();
  411. }
  412. }
  413.  
  414. void display_artifactory_logo() {
  415. display_buffer("eyes");
  416. }
  417.  
  418. /* ----------------
  419. WebServer for Drawing
  420. ---------------- */
  421.  
  422. #include "html.h"
  423. void on_homepage() {
  424. String html = FPSTR(index_html);
  425. if (mode_index == MODE_DRAW) {
  426. server.send(200, "text/html", html);
  427. } else if (mode_index == MODE_OFFLINE) {
  428. server.send(200, "text/html", "Artifactory Draw is Offline");
  429. }
  430. }
  431.  
  432. // this returns device variables in JSON, e.g.
  433. // {"pixels":xxxx,"blink":1}
  434. void on_status() {
  435. String html = "";
  436. html += "{\"brightness\":";
  437. html += brightness;
  438. html += "}";
  439. server.send(200, "text/html", html);
  440. }
  441.  
  442. void on_change_color() {
  443.  
  444. uint16_t i;
  445. if (server.hasArg("pixels")) {
  446. String val = server.arg("pixels");
  447. display_buffer(val);
  448. }
  449. }
  450. if (server.hasArg("clear")) {
  451. for (i = 0; i < NUM_LEDS; i++) {
  452. pixels[i] = 0;
  453. }
  454. }
  455. /*if(server.hasArg("brightness")) {
  456. brightness = server.arg("brightness").toInt();
  457. }*/
  458.  
  459. initial_demo = false;
  460. show_leds();
  461.  
  462. server.send(200, "text/html", "{\"result\":1}");
  463. }
  464.  
  465. char dec2hex(byte dec) {
  466. if (dec < 10) return '0' + dec;
  467. else return 'A' + (dec - 10);
  468. }
  469.  
  470. void button_handler() {
  471. button_clicked = true;
  472. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement