/*
 * File:   main.c
 * Author: Michel Osee
 *
 * Created on August 20, 2025, 12:17 PM
 */


// Distance between the top of 2 consecutive disks, in motor turns
#define DISK_PITCH  (11)
// Distance excess used in dropDisk to be sure the disk drops, in motor turns
#define DROP_EXTRA  (2)
// number of turns for a cleaning, in motor turns
#define CLEAN_PITCH (6)
// number of disks to drop
#define DISK_NB     (12)
// Number of days between the start of the system and the first drop
#define INITIAL_DELAY   (10)
// Number of days between 2 drops
#define DROP_DELAY      (30)


//#define  _XTAL_FREQ 500000
#include <xc.h>
#include "pins.h"
#include "motor.h"
#include "dateTime.h"
#include "nvm.h"
#include "logging.h"


dateTime_t dropDates[DISK_NB];

void cleaning(void);
void dropDisk(void);


void __interrupt() isrHandler() {
    if (TMR1IF) {
        TMR1IF = 0;
        dateTime_update();
    }
}


void main(void) {
  dateTime_t curDateTime, cleaningDateTime;
  uint8_t diskCount;
    
    pin_init();
    NVM_Initialize();
    mot_init();
    mot_stop();
    dateTime_init();
    diskCount = 0;
    
    /* Disc drop's timing (Normal code) 
     * First disc is dropped after INITIAL_DELAY days after boot
     * Next discs are dropped DROP_DELAY days after the previous one */
    for (uint8_t i=0; i<DISK_NB; i++) {
        dropDates[i].months = (INITIAL_DELAY + i*DROP_DELAY) / 30;
        dropDates[i].days = (INITIAL_DELAY + i*DROP_DELAY) % 30;
        dropDates[i].hours = 0;
        dropDates[i].minutes = 0;
        dropDates[i].seconds = 0;
    }
    /* Disc drop's timing (Test code)
     * First disc is dropped after 1 min after boot
     * Next discs are dropped 30 sec after the previous one 
    for (uint8_t i=0; i<DISK_NB; i++) {
        dropDates[i].months = 0;
        dropDates[i].days = 0;
        dropDates[i].hours = 0;
        dropDates[i].minutes = (i+1)/2;
        dropDates[i].seconds = (30 * (i+1)) % 60;
    }
*/    
    // make a first cleaning to show everything is OK
    cleaning();
    // Get the current time and log it in the NVM
    curDateTime = dateTime_get();
    log_cleaning(curDateTime);
    // Set the next cleaning in one day
    cleaningDateTime.months = 0;
    cleaningDateTime.days = 1;
    cleaningDateTime.hours = 0;
    cleaningDateTime.minutes = 0;
    cleaningDateTime.seconds = 0;

    // Drop 12 disks at the given dates
    while(diskCount < DISK_NB) {
        // low power mode for 2 sec
        SLEEP();
        // get the current time
        curDateTime = dateTime_get();
        // if it's cleaning time
        if ( dateTime_compare(curDateTime, dropDates[diskCount]) == 1) {
            dropDisk();
            log_drop(curDateTime);
            diskCount++;
        } else if ( dateTime_compare(curDateTime, cleaningDateTime) ) {
            cleaning();
            log_cleaning(curDateTime);
            cleaningDateTime.days += 2;
            if (cleaningDateTime.days > 30) {
                cleaningDateTime.months++;
                cleaningDateTime.days -= 30;
            }
        }
    }

    while(1);
}


void cleaning(void) {
    mot_up_turn(CLEAN_PITCH);
    mot_down_turn(CLEAN_PITCH);
}


void dropDisk(void) {
    mot_down_turn(DISK_PITCH + DROP_EXTRA);
    mot_up_turn(DROP_EXTRA);
}
