Exemplo n.º 1
0
int main(void) {
	armcmx_init();
	printf("Hello!\n");
	
	//setup
  GLCD.Init();   // initialise the library, non inverted writes pixels onto a clear screen
  if(GLCD.Height >= 64)   
    icon = ArduinoIcon64x64;  // the 64 pixel high icon
  else
    icon = ArduinoIcon64x32;  // the 32 pixel high icon
  introScreen();
  GLCD.ClearScreen(); 

  GLCD.SelectFont(System5x7, BLACK); // font for the default text area
  GLCD.println("GLCD version ");
  GLCD.println(" armcmx ");
		
  GLCD.DrawRect(8,16,110,32);  // rounded rectangle around text area   

	/*
	srand(23);
	int x = 0, y = 0;
//	uint8 d;
//	uint16 color = 0xff;
	while (1) {
		x = rand() % 127;
		y = rand() % 63;
		delay_ms(20);
		GLCD.SetDot(x, y, 1);
//		x = ++x % 128;
//		if (x == 0) 
//			y = ++y % 64;
	}
	*/
	while (1) {
  iter = 0;
  startMillis = millis();
  while( millis() - startMillis < 1000){ // loop for one second
    GLCD.DrawRect(0, 0, 64, 61, BLACK); // rectangle in left side of screen
    GLCD.DrawRoundRect(68, 0, 58, 61, 5, BLACK);  // rounded rectangle around text area   
    for(int i=0; i < 62; i += 4)
      GLCD.DrawLine(1,1,63,i, BLACK);  // draw lines from upper left down right side of rectangle  
    GLCD.DrawCircle(32,31,30,BLACK);   // draw circle centered in the left side of screen  
    GLCD.FillRect(92,40,16,16, WHITE); // clear previous spinner position  
    drawSpinner(loops++,100,48);       // draw new spinner position
    //GLCD.FillRect(24,txtLINE3,14,14, WHITE);  // clear text area that will be drawn below 
    GLCD.CursorTo(5,5);               // locate curser for printing text
    GLCD.PrintNumber(++iter);         // print current iteration at the current cursor position 
  } 
  // display number of iterations in one second
  GLCD.ClearScreen();               // clear the screen  
  GLCD.CursorTo(14,2);              // positon cursor  
  GLCD.Puts("FPS= ");               // print a text string
  GLCD.PrintNumber(iter);           // print a number 
	}
}
boolean WiFiManager::hasConnected(void)
{
  for(int c = 0; c < 50; c++)
  {
    if (WiFi.status() == WL_CONNECTED)
      return true;
    delay(200);
    DEBUG_PRINT(".");
    display.clear();
    display.drawXbm(34,10, 60, 36, WiFi_Logo_bits);
    display.setColor(INVERSE);
    display.fillRect(10, 10, 108, 44);
    display.setColor(WHITE);
    drawSpinner(4, c % 4);
    display.display();
  }
  DEBUG_PRINT("");
  DEBUG_PRINT("Could not connect to WiFi");
  display.print("No connection");
  return false;
}
// Scan APs, use configured if found, otherwise try each open AP to get through, finally open soft AP with config page.
boolean WiFiManager::findOpenAP(const char *szUrl)
{
    int nOpen = 0;
    begin("ESP8266");
    int nScan = WiFi.scanNetworks();
    bool bFound = false;
    Serial.println("scan done");
    String sSSID = getSSID();
    int ind = 0;

    display.setFontScale2x2(false);
    if (nScan == 0) {
        Serial.println( "No APs found" );
        display.print("No APs Found");
    }
    else {
        for (int i = 0; i < nScan; ++i) // try each public AP
        {
            Serial.print(WiFi.SSID(i));
            Serial.print(" ");
            Serial.println(WiFi.encryptionType(i));
            display.print(WiFi.SSID(i));
            if(WiFi.encryptionType(i) == 7 /*&& strncmp(WiFi.SSID(i),"Chromecast",6) != 0*/)
            {
              display.drawString(128-8, 56, "O");
              nOpen++;
            }
            else if( sSSID == WiFi.SSID(i) ){ // The saved WiFi was found
              bFound  = true;
              display.drawString(128-8, 56, "<");
              Serial.println(" Cfg AP found");
            }
        }
  }

  if(nOpen == 0 && !bFound)
  {
    display.print("No open AP found");
    if(sSSID == "")
    {
       display.print("Switch to SoftAP");
       display.print("Hotspot: ESP8266");
       display.print("Goto 192.168.4.1");
    }else{
       display.print("Switching to");
       display.print(sSSID);
    }
  }

  delay(2000); // delay for reading

  if(nOpen && !bFound)
  {
    WiFi.mode(WIFI_STA);
    int counter = 0;
    for (int i = 0; i < nScan; ++i)
    {
      if(WiFi.encryptionType(i) == 7)   // run through open APs and try to connect
      {
        Serial.print("Attempting ");
        Serial.print(WiFi.SSID(i));
        display.print(String(WiFi.SSID(i)) + "...");
        WiFi.begin(WiFi.SSID(i));
        for(int n = 0; n < 50 && WiFi.status() != WL_CONNECTED; n++)
        {
          delay(200);
          Serial.print(".");
          display.clear();
          display.drawXbm(34,10, 60, 36, WiFi_Logo_bits);
          display.setColor(INVERSE);
          display.fillRect(10, 10, 108, 44);
          display.setColor(WHITE);
          drawSpinner(4, n % 4);
          display.display();
        }
        if(WiFi.status() == WL_CONNECTED)
        {
          Serial.println("Connected");
          display.print("Connected");
          if(attemptClient(szUrl))    // attemp port 80 and 8080
          {
            display.drawString(64-8, 56, "!");
            break;
          }else{
            display.drawString(64-8, 56, "X");
            WiFi.disconnect();
          }
        }
        counter++;
      }
    }
    if (WiFi.status() != WL_CONNECTED)
    {
      Serial.println("Open WiFi failed");
      Serial.println("Switch to SoftAP");
      display.print("Open WiFi failed");
      display.print("Switch to SoftAP");
      autoConnect("ESP8266");
    }
  }
  else
  {
    autoConnect("ESP8266");
  }
  return true;
}