From 0287c672816fa76bdc9221e1927fe5e763e8c622 Mon Sep 17 00:00:00 2001 From: jakeg00dwin Date: Tue, 24 Sep 2024 09:44:25 -0700 Subject: [PATCH] Changed the `load.h` and `load.c` files for better documentation and differnt threshold values. --- src/load/load.c | 31 ++++++++++++++++++++++--------- src/load/load.h | 32 +++++++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 14 deletions(-) diff --git a/src/load/load.c b/src/load/load.c index 7d4d01a..be3f1fa 100644 --- a/src/load/load.c +++ b/src/load/load.c @@ -22,21 +22,23 @@ #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}; + static bool valid_load(uint16_t val) { - if(val > 527 && val < 1000) { + if(val > LOWTHRESH && val < HIGHTHRESH) { return true; } return false; } - -void Load_HandleLoadPortA(uint8_t adc_pin, uint8_t out_pin) +static uint16_t sample_adc(uint8_t adc_pin) { ADC_Init(adc_pin); ADC_Enable(); @@ -44,6 +46,13 @@ void Load_HandleLoadPortA(uint8_t adc_pin, uint8_t out_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(valid_load(val) && !porta_disabled[adc_pin]){ RegEdit_SetBit((void *) &PORTA.DIR, out_pin); RegEdit_SetBit((void *) &PORTA.OUT, out_pin); @@ -56,12 +65,8 @@ void Load_HandleLoadPortA(uint8_t adc_pin, uint8_t out_pin) void Load_HandleLoadPortB(uint8_t adc_pin, uint8_t out_pin) { - ADC_Init(adc_pin); - ADC_Enable(); - ADC_SetPin(adc_pin); - uint16_t val = ADC_ReadValue(adc_pin); - - ADC_Disable(); + uint16_t val = sample_adc(adc_pin); + if(valid_load(val) && !portb_disabled[adc_pin]){ RegEdit_SetBit((void *) &PORTB.DIR, out_pin); RegEdit_SetBit((void *) &PORTB.OUT, out_pin); @@ -71,3 +76,11 @@ void Load_HandleLoadPortB(uint8_t adc_pin, uint8_t out_pin) portb_disabled[adc_pin] = true; } } + +void Load_SoftResetDisabledLoads() +{ + for(int i = 0; i < 8; i++){ + porta_disabled[i] = false; + portb_disabled[i] = false; + } +} diff --git a/src/load/load.h b/src/load/load.h index 1b3112f..698bf43 100644 --- a/src/load/load.h +++ b/src/load/load.h @@ -10,23 +10,45 @@ #ifndef LOAD_H #define LOAD_H +#include + /** * @brief Low Threshold - * + * Anything below or equal to the value will cause the pin to lock into the disabled + * state. */ -#define LOWTHRESH 527 +#define LOWTHRESH 517 /** * @brief High Threshold - * + * Anything equal or above the value will cause the pin to lock into the disabled + * state. */ -#define HIGHTHRESH 1000 +#define HIGHTHRESH 830 + +/** + * @brief Checks if the adc pin is inbetween LOWTHRESH and HIGHTHRESH and then + * sets or disables the output pin on PORTA. + * @param adc_pin the pin number that the load value is read from. + * @param out_pin The pin that is set high if the adc_pin input is valid. + */ void Load_HandleLoadPortA(uint8_t adc_pin, uint8_t out_pin); + + +/** + * @brief Checks if the adc pin is inbetween LOWTHRESH and HIGHTHRESH and then + * sets or disables the output pin on PORTB. + * @param adc_pin the pin number that the load value is read from. + * @param out_pin The pin that is set high if the adc_pin input is valid. + */ void Load_HandleLoadPortB(uint8_t adc_pin, uint8_t out_pin); -void Load_HandlePinLoads(void); +/** + * @brief Resets the disabled array state. + */ +void Load_SoftResetDisabledLoads(void); #endif /* LOAD_H */