#include "WaterPort.h"
// Hardware Unique Idenditfier
// Hash created from time and WWV
const String HWUID = "1234";
// Haardware Version
const String WWV = "0.01";
// Number of ports in the device
const byte WATER_PORTS = 2;
// Pins used
// OUTPUT
// DS Serial input
const byte DSP = 13;
// SHCP Serial clock
const byte SCHP = 11;
// STCP Storage (latch) pin
const byte STCP = 12;
// MR Reset (clear), active low. Connect to VCC if not used
const byte MRP = 10;
//Input
// Check water level
const byte WLP = 9;
// pulse width in microseconds
const int PULSE_WIDTH = 10;
// 500ms or HALF_SECOND trigger
bool HALF_SECOND = false;
// Millisecoends between changes to ports
const int FREQ_SET_PORTS = 500;
const int FREQ_CHK_PORTS = 1000;
WaterPort ports[WATER_PORTS];
unsigned long previousLoopTime = millis();
// TODO Delete when real code in t loop no longer requies it
int pattern = 10101010;
int timerCount = 1;
unsigned long ac = 0xFFFFFF00;
unsigned long check = 5;
void setup() {
Serial.begin( 115200);
pinMode(DSP, OUTPUT); // clock signal, idle LOW
pinMode(SCHP, OUTPUT); // latch (copy input into registers), idle HIGH
pinMode(STCP, OUTPUT);
pinMode(MRP, OUTPUT); // High to send power to level pins
pinMode(WLP, INPUT);
digitalWrite(MRP, HIGH);
}
void loop() {
//
// Check for server action
// This may be better as an inturupt or i2c function
//
// Check for next port actions
long curTime = millis();
for (int port = 0; port < WATER_PORTS; port++ ) {
if (ports[port].checkset_actions()){
do_port_action(port);
}
}
//
// Send actions to Server
//
// Check time between interatins of loop. To see if they are resonable.
Serial.println((String) "############### Loop - " + (millis() - previousLoopTime));
previousLoopTime = millis();
}
void do_port_action (int port){
//
// When calling check water level disable inturupts while the whire is active
}
// if (HALF_SECOND == true){
// Serial.print("*");
// HALF_SECOND = false;
// }
// if (time_to_set_ports()){
// pattern = ~pattern; // Invert the pattern
// set_ports(pattern);
// }
// }
//This happens in setup
// Timer 0A Setup for 1 ms
// https://deepbluembedded.com/arduino-timer-calculator-code-generator/
// TCCR0A = 0; // Init Timer0A
// TCCR0B = 0; // Init Timer0B
// TCCR0B |= B00000011; // Prescaler = 64
// OCR0A = 250; // Timer Compare0A Register
// TIMSK0 |= B00000010; // Enable Timer COMPA Interrupt
// This happens in loop()
// ISR(TIMER0_COMPA_vect)
// {
// OCR0A += 250; // Advance The COMPA Register
// // Handle The Timer Interrupt
// timerCount++;
// if (timerCount >= 500) {
// HALF_SECOND = true;
// timerCount = 1;
// }
// //###########################
// boolean time_to_set_ports(){
// static long future;
// if (millis() > future){
// future = millis() + FREQ_SET_PORTS;
// return true;
// } else {
// return false;
// }
// }
// void set_ports(int ports){
// digitalWrite(STCP, LOW);
// shiftOut(DSP, SCHP, LSBFIRST, ports);
// digitalWrite(STCP, HIGH);
// }
//#############################
//###########################
// boolean time_to_check_ports(){
// static long future;
// if (millis() > future){
// future = millis() + FREQ_CHK_PORTS;
// return true;
// } else {
// return false;
// }
// }
// void check_ports(){
// for(int i = 0; i < 0; i++){
// ports[i].check_port()
// }
//}
//#############################
// unsigned long future_time(unsigned long timeDelay){
// return millis() + timeDelay;
// }