pump.ino 2.32 KB
#include "pump.h"

void insertSequence(int pump, int src_valve, int dst_valve, double duration, struct action *action) {
  unsigned int ut = time(NULL);
  struct sequence *ptr = (struct sequence*) malloc(sizeof(struct sequence));
  sequence_tail->next = ptr;
  if ( sequence_head == NULL ) {
    sequence_head = ptr;
    ptr->end = ut + duration;
    ptr->start = ut;
  }

  ptr->pump = pump;
  ptr->src_valve = src_valve;
  ptr->dst_valve = dst_valve;
  ptr->start = sequence_tail->start;
  ptr->end = sequence_tail->end + duration;
  ptr->action = action;
  ptr->next = NULL;

  sequence_tail=ptr;
}

void next_sequence_step() {
  if ( sequence_head != NULL ) { // there's something to do
    unsigned int ut = time(NULL);
    if ( ut >= sequence_head->end ) { // do next step
      struct sequence *tmp = (struct sequence*) malloc(sizeof(struct sequence));
      tmp = sequence_head;
      sequence_head = tmp->next;
      if ( sequence_head != NULL ) {
        set_relay(sequence_head);
      }
      free(tmp);
    }
  }
}

int pump_start(struct action *ptr) {
  if ( sequence_head == NULL ) {
    String remainingStr(ptr->duration);
    mqttClient.publish(PREAMBLE T_PUMPSTATE, remainingStr);
    insertSequence( dive, src_valve_pump, dst_valve_rotate, ptr->duration, ptr );
    insertSequence( off , off           , off             , 0            , ptr );
    set_relay(sequence_head); // kickoff sequence
    return 1;
  } else {
    return 0;
  }
}

int pump_start(struct button *ptr){
  return 1;
}

void pump_running(struct action *ptr) {
  unsigned int ut = time(NULL);
  String remainingStr(ptr->end - ut);
  mqttClient.publish(PREAMBLE T_PUMPSTATE, remainingStr );
}

void pump_end(struct action *ptr) {
  // abort

  /* pcf8574.digitalWrite(leeren11, 1); */
  /* pcf8574.digitalWrite(leeren1, 1); */
  /* pcf8574.digitalWrite(leeren2, 1); */
  /* pcf8574.digitalWrite(fuellen2, 1); */

  String remainingStr(0);
  mqttClient.publish(PREAMBLE T_PUMPSTATE, remainingStr );
}

void pump_end(struct button *ptr) {
}

void pump_end(int source) {
}


void set_relay(struct sequence *step) {
  int pin = 1;

  while ( pin < 7 ) {
    if (    step->pump      == pin
         || step->src_valve == pin
         || step->dst_valve == pin
         ) {
      /* pcf8574.digitalWrite(pin,0); */
    } else {
      /* pcf8574.digitalWrite(pin,1); */
    }
    pin++;
  }
}