/* Copyright 2025 by COG9 Enterprises LLC
 * All rights reserved.
 * Title: push.ino
 * Author: Jed Marti KI7NNP
 * Description: Pushbutton support code.
 * Created: Sun Oct 12 06:42:43 2025
 * Revision History:
 */


#include "powerard.h"
#include "push.h"

volatile uint32_t lastNext;
volatile uint32_t lastPrev;

extern   uint8_t  recording;

void nextScreenInterrupt(void)
/*     Select the next screen, 1 of some. Do a button debounce. If the other button is down at
 *   this time, then stop recording.
 * Parameters: none
 * External references:
 *   lastNext: When was the last time we had a change?
 * Returned values: none
 * Errors detected: none
 */
{
  uint32_t lm;
  lm = millis();
  if ((lm - lastNext) > 20)
  {
    nextScreen();
    lastNext = lm;
    if (digitalRead(PREVB) == LOW) recording = 0;
  }
  else lastNext = lm;
}


 void prevScreenInterrupt(void)
/*     Select the next screen, 1 of some. Do a button debounce. If the other button is down at
 *   this time, then stop recording.
 * Parameters: none
 * External references:
 *   lastNext: When was the last time we had a change?
 * Returned values: none
 * Errors detected: none
 */
{
  uint32_t lm;
  lm = millis();
  if ((lm - lastNext) > 20)
  {
    prevScreen();
    lastPrev = lm;
    if (digitalRead(NEXTB) == LOW) recording = 0;
  }
  else lastPrev = lm;
}


uint8_t initPushButtons(void)
/*     Initialize the push buttons. They should default HIGH if not pressed, LOW if pressed.
 *   Do not attach interrupts until after startup push.
 * Parameters: none
 * External References:
 *   NEXTB: right push button (constant).
 *   PREVB: Left push button (constant).
 * Returned values: 0 - none pushed, 1 NEXTB pushed, 2 PREVB pushed, 3 both pressed.
 *    0: no recording
 *    right: Record only milliamps on 3 channels.
 *    left: Record milliamps and volts all channels.
 *    left + right: record everything.
 * Errors detected: none
 */
{
  uint8_t rv;
  
  // Set so they default to high.
  pinMode(NEXTB, INPUT_PULLUP);
  pinMode(PREVB, INPUT_PULLUP);

  // Return: 0 no switches on, 1 NEXTB on, 2 PREVB on, 3 both pressed.
  rv = 0;
  if (digitalRead(NEXTB) == LOW) rv = 1;
  if (digitalRead(PREVB) == LOW) rv |= 2;
  return rv;
  //  Serial.println("initPushButtons");
  //  Serial.print("NEXT "); Serial.println(digitalRead(NEXTB));
  //  Serial.print("PREV "); Serial.println(digitalRead(PREVB));
}



void waitForAllReleased(void)
/*     Wait for all released for at least one millisecond - a debounce. Only do this
 *   before interrupts are enabled - as part of power on.
 */
{
  for (;;)
  {
    while (1) if (digitalRead(NEXTB) == HIGH && digitalRead(PREVB) == HIGH) break;
    delay(1);
    if (digitalRead(NEXTB) == HIGH && digitalRead(PREVB) == HIGH) return;
  }
}
  



void initPushInterrupts(void)
/*     Attach interrupts for the rising edge so things happen when the button is released.
 *   We only have this happen after startup.
 * Parameters: none
 * External References:
 *   NEXTB: right push button (constant).
 *   PREVB: Left push button (constant).
 * Returned values: none
 * Errors detected: none
 */
{

  // Now set so they only interrupt when rising - that is, after the switch released.
  attachInterrupt(digitalPinToInterrupt(NEXTB), nextScreenInterrupt, RISING);
  attachInterrupt(digitalPinToInterrupt(PREVB), prevScreenInterrupt, RISING);
}  


// EOF
