Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Time.h>
- #include <TimeLib.h>
- #include "SSD1306Ascii.h"
- #include "Event.h"
- #include "SSD1306AsciiAvrI2c.h"
- #include "StringSplitter.h"
- #define I2C_ADDRESS 0x3C
- #define RST_PIN 1
- #define RED 2
- #define GREEN 3
- #define BLUE 4
- #define MAX_EVENTS 10;
- #include <SoftwareSerial.h>
- SoftwareSerial mySerial(10, 11); // RX, TX
- SSD1306AsciiAvrI2c oled;
- unsigned long previousMillis = 0;
- const long interval = 1000;
- unsigned int eventCount = 0;
- Event events[5] = {
- Event(),
- Event(),
- Event(),
- Event(),
- Event()
- };
- void setup()
- {
- startRGB();
- // Open serial communications and wait for port to open:
- Serial.begin(9600);
- Serial.println(0);
- initializeDisplay();
- while (!Serial) {
- ; // wait for serial port to connect. Needed for Leonardo only
- }
- Serial.println("Goodnight moon!");
- // set the data rate for the SoftwareSerial port
- mySerial.begin(9600);
- mySerial.println("Hello, world?");
- digitalWrite(RED, HIGH);
- }
- void loop() // run over and over
- {
- handleSerial();
- if(heartbeat()){
- //renderClock();
- renderScreenEvents();
- }
- }
- void startRGB(){
- pinMode(GREEN,OUTPUT);
- digitalWrite(GREEN, HIGH);
- pinMode(BLUE,OUTPUT);
- digitalWrite(BLUE, HIGH);
- pinMode(RED,OUTPUT);
- }
- void handleSerial(){
- if (mySerial.available()){
- String c = mySerial.readString();
- StringSplitter *splitter = new StringSplitter(c, '$', 10);
- int itemCount = splitter->getItemCount();
- Serial.println("Item count: " + String(itemCount));
- for(int i = 0; i < itemCount; i++){
- String item = splitter->getItemAtIndex(i);
- if(item.length()>0){
- int index = item.indexOf("#");
- String buffer = item.substring(0,index);
- String tag = item.substring(index+1, c.length());
- parseCommand(tag, buffer);
- }
- }
- }
- if (Serial.available())
- mySerial.write(Serial.read());
- }
- bool heartbeat(){
- unsigned long currentMillis = millis();
- if(currentMillis - previousMillis >=interval){
- previousMillis = currentMillis;
- return true;
- }
- return false;
- }
- void initializeDisplay(){
- oled.begin(&Adafruit128x64, I2C_ADDRESS);
- oled.setFont(Adafruit5x7);
- oled.println();
- oled.set2X();
- oled.println("\nWatch\n Assistant");
- delay(1500);
- oled.clear();
- }
- void parseCommand(String tag, String buffer){
- Serial.println("Parsing command: {"+tag+","+buffer+"}");
- if(tag=="time"){
- timeCommand(buffer);
- } else if(tag=="event"){
- addEventCommand(buffer);
- }
- }
- void renderClock(){
- oled.clear();
- oled.setTextSize(2);
- int8_t hour_time = hour();
- if(hour_time==0)
- hour_time = (uint8_t) 12;
- else if(hour_time>=13)
- hour_time -= (uint8_t) 12;
- if(hour_time<=9)
- {
- oled.setCursor(30, 0);
- oled.print(hour_time);
- }
- else
- oled.print(hour_time);
- oled.setCursor(50, 0);
- oled.print(":");
- oled.setCursor(70, 0);
- int8_t minute_time = minute();
- if(minute_time<=9){
- oled.print("0");
- oled.setCursor(99, 0);
- oled.print(minute_time);
- }
- else
- oled.print(minute_time);
- oled.setTextSize(2);
- oled.print("\n\n "+String(eventCount)+" Tareas");
- //char *wkdy[7] = {"SUN", "MON", "TUE", "WED", "THR", "FRI", "SAT"};
- //oled.setCursor(5, 43<);
- //oled.print(wkdy[weekday()-1]);
- //oled.setCursor(60, 43);
- //oled.print(month());
- //oled.setCursor(78, 43);
- //oled.print("/");
- //oled.setCursor(95, 43);
- //oled.print(day());
- }
- void renderScreenEvents(){
- oled.clear();
- if(eventCount>0){
- Event e = getNextEvent();
- oled.setCursor(0,0);
- oled.setTextSize(2);
- oled.println(e.getName());
- oled.setTextSize(1);
- oled.println("\nQuedan: ");
- printHoursUntil(e);
- } else {
- renderClock();
- }
- }
- void timeCommand(String buffer){
- int pivot1 = buffer.indexOf('/');
- int pivot2 = buffer.indexOf('/',pivot1+1);
- int pivot3 = buffer.indexOf(' ', pivot2+1);
- int pivot4 = buffer.indexOf(':', pivot3+1);
- int pivot5 = buffer.indexOf(':', pivot4+1);
- int pivot6 = buffer.indexOf(' ', pivot5+1);
- int monthS = buffer.substring(0, pivot1).toInt();
- int dayS = buffer.substring(pivot1+1, pivot2).toInt();
- int yearS = buffer.substring(pivot2+1, pivot3).toInt();
- int hourS = buffer.substring(pivot3+1, pivot4).toInt();
- int minuteS = buffer.substring(pivot4+1, pivot5).toInt();
- int secondS = buffer.substring(pivot5+1, pivot6).toInt();
- String after = buffer.substring(pivot6+1);
- if(after=="PM") hourS+=12;
- Serial.println("Time is being set!:");
- Serial.println("{"+String(yearS)+","+String(monthS)+","+dayS+","+":"+hourS+","+minuteS+","+secondS+"}");
- setTime(hourS, minuteS, secondS, dayS, monthS, yearS);
- }
- void addEventCommand(String buffer){
- int data[12];
- int pastIndex = buffer.indexOf(',');
- String name = buffer.substring(0,pastIndex);
- int pivot1 = buffer.indexOf('/');
- int pivot2 = buffer.indexOf('/',pivot1+1);
- int pivot3 = buffer.indexOf(' ', pivot2+1);
- int pivot4 = buffer.indexOf(':', pivot3+1);
- int pivot5 = buffer.indexOf(':', pivot4+1);
- int pivot6 = buffer.indexOf(' ', pivot5+1);
- int monthS = buffer.substring(pastIndex+1, pivot1).toInt();
- int dayS = buffer.substring(pivot1+1, pivot2).toInt();
- int yearS = buffer.substring(pivot2+1, pivot3).toInt();
- int hourS = buffer.substring(pivot3+1, pivot4).toInt();
- int minuteS = buffer.substring(pivot4+1, pivot5).toInt();
- int secondS = buffer.substring(pivot5+1, pivot6).toInt();
- String after = buffer.substring(pivot6+1);
- if(after=="PM") hourS+=12;
- oled.clear();
- oled.set2X();
- if(name.length()>10 && name.indexOf(' ')!=-1){
- name.replace(' ', '\n');
- }
- 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.
- events[eventCount] = e;
- eventCount = eventCount+1;
- oled.print(name);
- delay(5000);
- }
- Event getNextEvent(){
- Event e = events[0];
- if(e.dayS>day() || ){
- }
- /*for(int i = 1;i<sizeof(events)/sizeof(events[0]);i++){
- if(events[i].timeRemaining()>0){
- if(e.timeRemaining()>events[i].timeRemaining()){
- e = events[i];
- }
- }
- }*/
- return e;
- }
- void printHoursUntil(Event event){
- int hoursUntil = event.hoursRemaining();
- oled.setTextSize(2);
- if(hoursUntil<1){
- oled.println(String(event.minutesRemaining())+" Minutos");
- } else {
- oled.println(String(hoursUntil)+" Horas");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement