Advertisement
luchino777

orologio_wroom32

Jul 20th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.09 KB | None | 0 0
  1. /**
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2018 by ThingPulse, Daniel Eichhorn
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. *
  24. * ThingPulse invests considerable time and money to develop these open source libraries.
  25. * Please support us by buying our products (and not the clones) from
  26. * https://thingpulse.com
  27. *
  28. */
  29.  
  30. #include <WiFi.h>
  31. #include "time.h"
  32.  
  33. #include <TimeLib.h>
  34.  
  35. // Include the correct display library
  36. // For a connection via I2C using Wire include
  37. #include <Wire.h> // Only needed for Arduino 1.6.5 and earlier
  38. #include "SSD1306Wire.h" // legacy include: `#include "SSD1306.h"`
  39. #include <WiFiUdp.h>
  40.  
  41. // Include the UI lib
  42. #include "OLEDDisplayUi.h"
  43.  
  44. // Include custom images
  45. #include "images.h"
  46.  
  47. // Initialize the OLED display using Wire library
  48. SSD1306Wire display(0x3c, 5, 4);
  49. // SH1106 display(0x3c, D3, D5);
  50.  
  51. OLEDDisplayUi ui ( &display );
  52.  
  53. //// Aggiungo conessione internet
  54.  
  55. const char* ssid = "foresto-salone";
  56. const char* password = "sweettrail508";
  57.  
  58. const char* ntpServer = "pool.ntp.org";
  59. const long gmtOffset_sec = 3600;
  60. const int daylightOffset_sec = 3600;
  61. ////fine aggiunta
  62.  
  63. int screenW = 128;
  64. int screenH = 64;
  65. int clockCenterX = screenW/2;
  66. int clockCenterY = ((screenH-16)/2)+16; // top yellow part is 16 px height
  67. int clockRadius = 23;
  68.  
  69. // utility function for digital clock display: prints leading 0
  70. String twoDigits(int digits){
  71. if(digits < 10) {
  72. String i = '0'+String(digits);
  73. return i;
  74. }
  75. else {
  76. return String(digits);
  77. }
  78. }
  79.  
  80. void clockOverlay(OLEDDisplay *display, OLEDDisplayUiState* state) {
  81.  
  82. }
  83.  
  84. void analogClockFrame(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) {
  85. // ui.disableIndicator();
  86.  
  87. // Draw the clock face
  88. // display->drawCircle(clockCenterX + x, clockCenterY + y, clockRadius);
  89. display->drawCircle(clockCenterX + x, clockCenterY + y, 2);
  90. //
  91. //hour ticks
  92. for( int z=0; z < 360;z= z + 30 ){
  93. //Begin at 0° and stop at 360°
  94. float angle = z ;
  95. angle = ( angle / 57.29577951 ) ; //Convert degrees to radians
  96. int x2 = ( clockCenterX + ( sin(angle) * clockRadius ) );
  97. int y2 = ( clockCenterY - ( cos(angle) * clockRadius ) );
  98. int x3 = ( clockCenterX + ( sin(angle) * ( clockRadius - ( clockRadius / 8 ) ) ) );
  99. int y3 = ( clockCenterY - ( cos(angle) * ( clockRadius - ( clockRadius / 8 ) ) ) );
  100. display->drawLine( x2 + x , y2 + y , x3 + x , y3 + y);
  101. }
  102.  
  103. // display second hand
  104. float angle = second() * 6 ;
  105. angle = ( angle / 57.29577951 ) ; //Convert degrees to radians
  106. int x3 = ( clockCenterX + ( sin(angle) * ( clockRadius - ( clockRadius / 5 ) ) ) );
  107. int y3 = ( clockCenterY - ( cos(angle) * ( clockRadius - ( clockRadius / 5 ) ) ) );
  108. display->drawLine( clockCenterX + x , clockCenterY + y , x3 + x , y3 + y);
  109. //
  110. // display minute hand
  111. angle = minute() * 6 ;
  112. angle = ( angle / 57.29577951 ) ; //Convert degrees to radians
  113. x3 = ( clockCenterX + ( sin(angle) * ( clockRadius - ( clockRadius / 4 ) ) ) );
  114. y3 = ( clockCenterY - ( cos(angle) * ( clockRadius - ( clockRadius / 4 ) ) ) );
  115. display->drawLine( clockCenterX + x , clockCenterY + y , x3 + x , y3 + y);
  116. //
  117. // display hour hand
  118. angle = hour() * 30 + int( ( minute() / 12 ) * 6 ) ;
  119. angle = ( angle / 57.29577951 ) ; //Convert degrees to radians
  120. x3 = ( clockCenterX + ( sin(angle) * ( clockRadius - ( clockRadius / 2 ) ) ) );
  121. y3 = ( clockCenterY - ( cos(angle) * ( clockRadius - ( clockRadius / 2 ) ) ) );
  122. display->drawLine( clockCenterX + x , clockCenterY + y , x3 + x , y3 + y);
  123. }
  124.  
  125. void digitalClockFrame(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) {
  126. String timenow = String(hour())+":"+twoDigits(minute());
  127. display->setTextAlignment(TEXT_ALIGN_CENTER);
  128. display->setFont(Rock_Salt_Regular_28);
  129. display->drawString(68, 2, timenow);
  130. }
  131.  
  132. // This array keeps function pointers to all frames
  133. // frames are the single views that slide in
  134. FrameCallback frames[] = { analogClockFrame, digitalClockFrame };
  135.  
  136. // how many frames are there?
  137. int frameCount = 2;
  138.  
  139. // Overlays are statically drawn on top of a frame eg. a clock
  140. OverlayCallback overlays[] = { clockOverlay };
  141. int overlaysCount = 1;
  142.  
  143. void printLocalTime()
  144. {
  145. struct tm timeinfo;
  146. if(!getLocalTime(&timeinfo)){
  147. Serial.println("Failed to obtain time");
  148. return;
  149. }
  150.  
  151. Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
  152.  
  153. }
  154.  
  155.  
  156. void setup() {
  157. Serial.begin(9600);
  158. Serial.println();
  159.  
  160. // The ESP is capable of rendering 60fps in 80Mhz mode
  161. // but that won't give you much time for anything else
  162. // run it in 160Mhz mode or just set it to 30 fps
  163. ui.setTargetFPS(60);
  164.  
  165. // Customize the active and inactive symbol
  166. ui.setActiveSymbol(activeSymbol);
  167. ui.setInactiveSymbol(inactiveSymbol);
  168.  
  169. // You can change this to
  170. // TOP, LEFT, BOTTOM, RIGHT
  171. ui.setIndicatorPosition(TOP);
  172.  
  173. // Defines where the first frame is located in the bar.
  174. ui.setIndicatorDirection(LEFT_RIGHT);
  175.  
  176. // You can change the transition that is used
  177. // SLIDE_LEFT, SLIDE_RIGHT, SLIDE_UP, SLIDE_DOWN
  178. ui.setFrameAnimation(SLIDE_LEFT);
  179.  
  180. // Add frames
  181. ui.setFrames(frames, frameCount);
  182.  
  183. // Add overlays
  184. ui.setOverlays(overlays, overlaysCount);
  185.  
  186. // Initialising the UI will init the display too.
  187. ui.init();
  188.  
  189. display.flipScreenVertically();
  190.  
  191. //connect to WiFi
  192. Serial.printf("Connecting to %s ", ssid);
  193. WiFi.begin(ssid, password);
  194. while (WiFi.status() != WL_CONNECTED) {
  195. delay(500);
  196. Serial.print(".");
  197. }
  198. Serial.println(" CONNECTED");
  199.  
  200. //init and get the time
  201.  
  202. configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  203. printLocalTime();
  204.  
  205.  
  206. //disconnect WiFi as it's no longer needed
  207. WiFi.disconnect(true);
  208. WiFi.mode(WIFI_OFF);
  209.  
  210. time_t now;
  211. time(&now);
  212. Serial.println(now);
  213. setTime(now+7200);
  214. }
  215.  
  216. void loop() {
  217. int remainingTimeBudget = ui.update();
  218.  
  219. if (remainingTimeBudget > 0) {
  220. // You can do some work here
  221. // Don't do stuff if you are below your
  222. // time budget.
  223. delay(remainingTimeBudget);
  224.  
  225. }
  226.  
  227.  
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement