Advertisement
Guest User

8x8 painter -- main

a guest
Nov 30th, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.40 KB | None | 0 0
  1. #ifndef UNIT_TEST
  2.   #include <Arduino.h>
  3. #endif
  4.  
  5. const String wLanHostName="8x8painter";
  6.  
  7. #include <ESP8266WiFi.h>
  8. #include <DNSServer.h>
  9. #include <ESP8266WebServer.h>
  10. #include <WiFiManager.h>
  11. #include <ESP8266HTTPClient.h>
  12.  
  13. ESP8266WebServer server(80);
  14.  
  15. #include "index_html.h"
  16.  
  17. #include <Adafruit_GFX.h>
  18. #include <WEMOS_Matrix_GFX.h>
  19.  
  20. MLED matrix(7); //set intensity (7 >> maximum)
  21.  
  22. // Feedback for Config...
  23. #include <Ticker.h>
  24. Ticker ticker;
  25.  
  26. void tick(){
  27.   static uint8_t state=0;
  28.  
  29.   matrix.clear();
  30.  
  31.   switch(state++){
  32.     case 0:
  33.       matrix.drawPixel(0,0,LED_ON);  
  34.       matrix.drawPixel(0,7,LED_ON);  
  35.       matrix.drawPixel(7,0,LED_ON);  
  36.       matrix.drawPixel(7,7,LED_ON);  
  37.       break;
  38.     case 1:
  39.       matrix.drawPixel(1,1,LED_ON);  
  40.       matrix.drawPixel(1,6,LED_ON);  
  41.       matrix.drawPixel(6,1,LED_ON);  
  42.       matrix.drawPixel(6,6,LED_ON);  
  43.       break;
  44.     case 2:
  45.       matrix.drawPixel(2,2,LED_ON);  
  46.       matrix.drawPixel(2,5,LED_ON);  
  47.       matrix.drawPixel(5,2,LED_ON);  
  48.       matrix.drawPixel(5,5,LED_ON);  
  49.       break;
  50.     case 3:
  51.       matrix.drawPixel(3,3,LED_ON);  
  52.       matrix.drawPixel(3,4,LED_ON);  
  53.       matrix.drawPixel(4,3,LED_ON);  
  54.       matrix.drawPixel(4,4,LED_ON);  
  55.       break;
  56.   }
  57.  
  58.   if(state>3){
  59.     state=0;
  60.   }
  61.   matrix.writeDisplay();  
  62. }
  63.  
  64. void updatePanel(String allLeds){
  65.     Serial.print("Got Data: ");
  66.     Serial.println(allLeds);
  67.  
  68.   if(allLeds.length()!=64){
  69.     Serial.print("Data Len won't comply - try again");
  70.     return;
  71.   }
  72.  
  73.   matrix.clear();
  74.  
  75.   uint8_t pointer=0;
  76.  
  77.   // for(int k=7;k>=0;--k){
  78.   for(int k=0;k<=7;k++){
  79.     //for(int t=7;t>=0;--t){
  80.     for(int t=0;t<=7;t++){
  81.       if(allLeds.charAt(pointer++)=='1'){
  82.         matrix.drawPixel(t,k,LED_ON);  
  83.       }    
  84.     }
  85.   }
  86.   matrix.writeDisplay();
  87. }
  88.  
  89. void processIndex(){
  90.   server.sendHeader("Cache-Control","no-cache");
  91.   server.sendHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  92.  
  93.   if (server.args()>0){
  94.  
  95.     for (int i=0;i<server.args();i++){
  96.       if(server.argName(i)=="data"){
  97.                 updatePanel(server.arg(i));
  98.       }
  99.     }
  100.     server.send(200,"text/plain","OK");
  101.   }else{
  102.     server.send(200,"text/html",indexHTML);
  103.   }
  104. }
  105.  
  106. void setup(){  
  107.   Serial.begin(115200);
  108.   delay(200);
  109.   Serial.println("\n\n8x8 Painter --START --");
  110.  
  111.   ticker.attach(0.3,tick);
  112.  
  113.   WiFi.setOutputPower(20.5); // MAX
  114.   WiFi.setPhyMode(WIFI_PHY_MODE_11N);
  115.  
  116.   WiFiManager wifiManager;
  117.  
  118.   // wifiManager.resetSettings();
  119.  
  120.   wifiManager.setDebugOutput(true);
  121.   wifiManager.setAPStaticIPConfig(IPAddress(1,2,3,4),IPAddress(1,2,3,4),IPAddress(255,255,255,0));
  122.   wifiManager.setConfigPortalTimeout(240);
  123.   String wLANName=wLanHostName+" CONFIG";
  124.   if(!wifiManager.autoConnect(wLANName.c_str())){
  125.     ESP.restart();
  126.     delay(1000);
  127.   }
  128.  
  129.   WiFi.hostname(wLanHostName);
  130.  
  131.   Serial.print("We're connected to ");
  132.   Serial.println(WiFi.SSID());
  133.   Serial.print("IP address: ");
  134.   Serial.println(WiFi.localIP());
  135.  
  136.   Serial.print("\nCatch me on\nhttp://");
  137.   Serial.print(WiFi.hostname());
  138.   Serial.print("/\n..or..\nhttp://");
  139.   Serial.print(WiFi.localIP());
  140.   Serial.print("/\n\n");
  141.  
  142.   server.on("/",processIndex);
  143.  
  144.   server.begin();
  145.  
  146.   matrix.clear();
  147.   matrix.writeDisplay();  
  148.  
  149.   ticker.detach();
  150.  
  151.   Serial.println("Goin into LOOP...\n");
  152. }
  153.  
  154. void loop(){
  155.   server.handleClient();
  156. }
  157.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement