86 lines
1.7 KiB
C
86 lines
1.7 KiB
C
/*
|
|
* Author: Jake G
|
|
* Date: 2024
|
|
* filename: load.c
|
|
* description: module_purpose
|
|
*/
|
|
|
|
#ifndef __AVR_ATtiny404__
|
|
#define __AVR_ATtiny404__
|
|
#endif
|
|
|
|
|
|
#include <avr/io.h>
|
|
#include "load.h"
|
|
|
|
|
|
#ifndef UNIT_TESTING
|
|
#include "ADC.h"
|
|
#include "RegEdit.h"
|
|
#else
|
|
#include "MockADC/MockADC.h"
|
|
#include "MockRegEdit.h"
|
|
#endif
|
|
|
|
|
|
|
|
//These two arrays allow us to track the A and B ports to know when
|
|
//one has been disabled before.
|
|
static bool porta_disabled[8] = {0};
|
|
static bool portb_disabled[8] = {0};
|
|
|
|
|
|
bool is_valid_load(uint16_t val)
|
|
{
|
|
if(val > LOWTHRESH && val < HIGHTHRESH) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool is_below_target(uint16_t val)
|
|
{
|
|
if(val < HYSTERESIS){
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
uint16_t sample_adc(uint8_t adc_pin)
|
|
{
|
|
ADC_Init(adc_pin);
|
|
ADC_Enable();
|
|
ADC_SetPin(adc_pin);
|
|
uint16_t val = ADC_ReadValue(adc_pin);
|
|
|
|
ADC_Disable();
|
|
return val;
|
|
}
|
|
|
|
void Load_HandleLoadPortA(uint8_t adc_pin, uint8_t out_pin)
|
|
{
|
|
uint16_t val = sample_adc(adc_pin);
|
|
|
|
if(is_valid_load(val) && !porta_disabled[adc_pin]){
|
|
RegEdit_SetBit((void *) &PORTA.DIR, out_pin);
|
|
RegEdit_SetBit((void *) &PORTA.OUT, out_pin);
|
|
}
|
|
else{
|
|
RegEdit_ClearBit((void *) &PORTA.OUT, out_pin);
|
|
porta_disabled[adc_pin] = true;
|
|
}
|
|
}
|
|
|
|
void Load_HandleLoadPortB(uint8_t adc_pin, uint8_t out_pin)
|
|
{
|
|
uint16_t val = sample_adc(adc_pin);
|
|
|
|
if(is_valid_load(val) && !portb_disabled[adc_pin]){
|
|
RegEdit_SetBit((void *) &PORTB.DIR, out_pin);
|
|
RegEdit_SetBit((void *) &PORTB.OUT, out_pin);
|
|
}
|
|
else{
|
|
RegEdit_ClearBit((void *) &PORTB.OUT, out_pin);
|
|
portb_disabled[adc_pin] = true;
|
|
}
|
|
}
|