/*
 * Squirty Controller
 * File:   main.c
 * Author: mcgill
 *
 * Created on March 25, 2015, 3:15 PM
 * Squirty controller controls the sediment agitation pump on a McLane
 * Parflux sediment trap to prevent clogs. Squirty turns on the pump for
 * brief periods (e.g. 10 secs) a couple of time per day, and sleeps at very
 * low power the rest of the time.
 *
 * 31 kHz internal oscillator is very low power, but inaccurate (+/- 25%).
 * Timing rate can be adjusted through the _XTAL_FREQ constant, but precise
 * timing is not critical for this application.
 *
 * Target processor is PIC10F322
 * Compiled with XC8 compiler v2.32
 *
 * v1.0.0   Initial release - PRM
 */

/******************************************************************************/
/* Configuration bits                                                         */
/******************************************************************************/
#pragma config FOSC = INTOSC    // Oscillator Selection bits (INTOSC oscillator: CLKIN function disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON       // MCLR Pin Function Select bit (MCLR pin function is MCLR)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config LVP = ON         // Low-Voltage Programming Enable (Low-voltage programming enabled)
#pragma config LPBOR = OFF      // Brown-out Reset Selection bits (BOR disabled)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)

/******************************************************************************/
/* Files to Include                                                           */
/******************************************************************************/

#include <xc.h>            // XC8 General Include File
#include <stdint.h>        // for integer type definitions

/******************************************************************************/
/* Constants Definitions                                           */
/******************************************************************************/
#define PUMPONSECS  10U     // number of seconds that pump is on each cycle
#define PUMPOFFHRS  6U      // number of hours between pump cycles (no more than 18)
#define _XTAL_FREQ  29795U  // must define for delay function to work
                            //  adjust up if timing is fast, down if slow
#define PUMP        RA2     // this pin controls pump
#define LED         RA0     // this pin blinks 1 Hz LED plugged into ICSP port
#define ON          1
#define OFF         0

/******************************************************************************/
/* Main Program                                                               */
/******************************************************************************/

void main(void)
{
    uint16_t    secsElapsed;
    uint16_t    secsToSleep;

    OSCCON = 0x00;          // use 31 kHz internal osc
    ANSELA = 0x00;          // make all pins digital i/o
    TRISA = 0x00;           // make RA0, RA1, and RA2 outputs

    secsToSleep = PUMPOFFHRS * 3600;

    while(1) {              // loop forever
        secsElapsed = 0;    // restart elapsed time
        PUMP = ON;
        
        while(secsElapsed++ < secsToSleep) { // count seconds until time elapsed
            if(secsElapsed > PUMPONSECS) PUMP = OFF;
            LED = ON;       // blink LED for 10 ms at 1 Hz rate
            __delay_ms(10);
            LED = OFF;
            __delay_ms(990);
        }; // end while

    }; // end while
}; // end main
