Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.49 KB | None | 0 0
  1. #include <Time.h>
  2. #include <TimeLib.h>
  3.  
  4. #include "SSD1306Ascii.h"
  5. #include "Event.h"
  6. #include "SSD1306AsciiAvrI2c.h"
  7. #include "StringSplitter.h"
  8.  
  9. #define I2C_ADDRESS 0x3C
  10. #define RST_PIN 1
  11. #define RED 2
  12. #define GREEN 3
  13. #define BLUE 4
  14. #define MAX_EVENTS 10;
  15.  
  16.  
  17. #include <SoftwareSerial.h>
  18.  
  19.  
  20.  
  21.  
  22.  
  23. SoftwareSerial mySerial(10, 11); // RX, TX
  24. SSD1306AsciiAvrI2c oled;
  25.  
  26. unsigned long previousMillis = 0;
  27.  
  28. const long interval = 1000;
  29.  
  30. unsigned int eventCount = 0;
  31. Event events[5] = {
  32. Event(),
  33. Event(),
  34. Event(),
  35. Event(),
  36. Event()
  37. };
  38.  
  39. void setup()
  40. {
  41. startRGB();
  42. // Open serial communications and wait for port to open:
  43. Serial.begin(9600);
  44. Serial.println(0);
  45. initializeDisplay();
  46. while (!Serial) {
  47. ; // wait for serial port to connect. Needed for Leonardo only
  48. }
  49.  
  50. Serial.println("Goodnight moon!");
  51.  
  52. // set the data rate for the SoftwareSerial port
  53. mySerial.begin(9600);
  54. mySerial.println("Hello, world?");
  55. digitalWrite(RED, HIGH);
  56. }
  57.  
  58. void loop() // run over and over
  59. {
  60. handleSerial();
  61. if(heartbeat()){
  62. //renderClock();
  63. renderScreenEvents();
  64. }
  65. }
  66.  
  67. void startRGB(){
  68. pinMode(GREEN,OUTPUT);
  69. digitalWrite(GREEN, HIGH);
  70. pinMode(BLUE,OUTPUT);
  71. digitalWrite(BLUE, HIGH);
  72. pinMode(RED,OUTPUT);
  73. }
  74.  
  75.  
  76. void handleSerial(){
  77. if (mySerial.available()){
  78. String c = mySerial.readString();
  79. StringSplitter *splitter = new StringSplitter(c, '$', 10);
  80. int itemCount = splitter->getItemCount();
  81. Serial.println("Item count: " + String(itemCount));
  82.  
  83. for(int i = 0; i < itemCount; i++){
  84. String item = splitter->getItemAtIndex(i);
  85. if(item.length()>0){
  86. int index = item.indexOf("#");
  87. String buffer = item.substring(0,index);
  88. String tag = item.substring(index+1, c.length());
  89. parseCommand(tag, buffer);
  90. }
  91. }
  92.  
  93. }
  94. if (Serial.available())
  95. mySerial.write(Serial.read());
  96. }
  97. bool heartbeat(){
  98. unsigned long currentMillis = millis();
  99. if(currentMillis - previousMillis >=interval){
  100. previousMillis = currentMillis;
  101. return true;
  102. }
  103. return false;
  104. }
  105.  
  106.  
  107. void initializeDisplay(){
  108. oled.begin(&Adafruit128x64, I2C_ADDRESS);
  109. oled.setFont(Adafruit5x7);
  110. oled.println();
  111. oled.set2X();
  112. oled.println("\nWatch\n Assistant");
  113. delay(1500);
  114. oled.clear();
  115. }
  116.  
  117. void parseCommand(String tag, String buffer){
  118. Serial.println("Parsing command: {"+tag+","+buffer+"}");
  119. if(tag=="time"){
  120. timeCommand(buffer);
  121. } else if(tag=="event"){
  122. addEventCommand(buffer);
  123. }
  124. }
  125.  
  126. void renderClock(){
  127. oled.clear();
  128. oled.setTextSize(2);
  129. int8_t hour_time = hour();
  130. if(hour_time==0)
  131. hour_time = (uint8_t) 12;
  132. else if(hour_time>=13)
  133. hour_time -= (uint8_t) 12;
  134.  
  135. if(hour_time<=9)
  136. {
  137. oled.setCursor(30, 0);
  138. oled.print(hour_time);
  139. }
  140. else
  141. oled.print(hour_time);
  142.  
  143. oled.setCursor(50, 0);
  144. oled.print(":");
  145. oled.setCursor(70, 0);
  146. int8_t minute_time = minute();
  147.  
  148. if(minute_time<=9){
  149. oled.print("0");
  150. oled.setCursor(99, 0);
  151. oled.print(minute_time);
  152. }
  153. else
  154. oled.print(minute_time);
  155.  
  156. oled.setTextSize(2);
  157. oled.print("\n\n "+String(eventCount)+" Tareas");
  158. //char *wkdy[7] = {"SUN", "MON", "TUE", "WED", "THR", "FRI", "SAT"};
  159. //oled.setCursor(5, 43<);
  160. //oled.print(wkdy[weekday()-1]);
  161. //oled.setCursor(60, 43);
  162. //oled.print(month());
  163. //oled.setCursor(78, 43);
  164. //oled.print("/");
  165. //oled.setCursor(95, 43);
  166. //oled.print(day());
  167. }
  168.  
  169. void renderScreenEvents(){
  170. oled.clear();
  171. if(eventCount>0){
  172. Event e = getNextEvent();
  173. oled.setCursor(0,0);
  174. oled.setTextSize(2);
  175. oled.println(e.getName());
  176. oled.setTextSize(1);
  177. oled.println("\nQuedan: ");
  178. printHoursUntil(e);
  179. } else {
  180. renderClock();
  181. }
  182. }
  183.  
  184.  
  185. void timeCommand(String buffer){
  186. int pivot1 = buffer.indexOf('/');
  187. int pivot2 = buffer.indexOf('/',pivot1+1);
  188. int pivot3 = buffer.indexOf(' ', pivot2+1);
  189. int pivot4 = buffer.indexOf(':', pivot3+1);
  190. int pivot5 = buffer.indexOf(':', pivot4+1);
  191. int pivot6 = buffer.indexOf(' ', pivot5+1);
  192. int monthS = buffer.substring(0, pivot1).toInt();
  193. int dayS = buffer.substring(pivot1+1, pivot2).toInt();
  194. int yearS = buffer.substring(pivot2+1, pivot3).toInt();
  195. int hourS = buffer.substring(pivot3+1, pivot4).toInt();
  196. int minuteS = buffer.substring(pivot4+1, pivot5).toInt();
  197. int secondS = buffer.substring(pivot5+1, pivot6).toInt();
  198. String after = buffer.substring(pivot6+1);
  199. if(after=="PM") hourS+=12;
  200. Serial.println("Time is being set!:");
  201. Serial.println("{"+String(yearS)+","+String(monthS)+","+dayS+","+":"+hourS+","+minuteS+","+secondS+"}");
  202. setTime(hourS, minuteS, secondS, dayS, monthS, yearS);
  203. }
  204.  
  205.  
  206.  
  207.  
  208. void addEventCommand(String buffer){
  209. int data[12];
  210. int pastIndex = buffer.indexOf(',');
  211. String name = buffer.substring(0,pastIndex);
  212.  
  213. int pivot1 = buffer.indexOf('/');
  214. int pivot2 = buffer.indexOf('/',pivot1+1);
  215. int pivot3 = buffer.indexOf(' ', pivot2+1);
  216. int pivot4 = buffer.indexOf(':', pivot3+1);
  217. int pivot5 = buffer.indexOf(':', pivot4+1);
  218. int pivot6 = buffer.indexOf(' ', pivot5+1);
  219. int monthS = buffer.substring(pastIndex+1, pivot1).toInt();
  220. int dayS = buffer.substring(pivot1+1, pivot2).toInt();
  221. int yearS = buffer.substring(pivot2+1, pivot3).toInt();
  222. int hourS = buffer.substring(pivot3+1, pivot4).toInt();
  223. int minuteS = buffer.substring(pivot4+1, pivot5).toInt();
  224. int secondS = buffer.substring(pivot5+1, pivot6).toInt();
  225. String after = buffer.substring(pivot6+1);
  226. if(after=="PM") hourS+=12;
  227. oled.clear();
  228. oled.set2X();
  229. if(name.length()>10 && name.indexOf(' ')!=-1){
  230. name.replace(' ', '\n');
  231. }
  232. Event e = Event(name, yearS, monthS, dayS, hourS, minuteS, secondS, yearS, monthS, dayS, hourS, minuteS, secondS, 5.0f, 0.0f); //last 2 are offsetminute and offset start.
  233. events[eventCount] = e;
  234. eventCount = eventCount+1;
  235. oled.print(name);
  236. delay(5000);
  237.  
  238. }
  239.  
  240. Event getNextEvent(){
  241. Event e = events[0];
  242. if(e.dayS>day() || ){
  243.  
  244. }
  245. /*for(int i = 1;i<sizeof(events)/sizeof(events[0]);i++){
  246. if(events[i].timeRemaining()>0){
  247. if(e.timeRemaining()>events[i].timeRemaining()){
  248. e = events[i];
  249. }
  250. }
  251. }*/
  252. return e;
  253. }
  254.  
  255. void printHoursUntil(Event event){
  256. int hoursUntil = event.hoursRemaining();
  257. oled.setTextSize(2);
  258. if(hoursUntil<1){
  259. oled.println(String(event.minutesRemaining())+" Minutos");
  260. } else {
  261. oled.println(String(hoursUntil)+" Horas");
  262. }
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement