added support for LDR and models

This commit is contained in:
2025-02-23 21:31:08 -05:00
parent 988765e2c2
commit f067bc26f9
13 changed files with 147 additions and 264 deletions

127
main.c
View File

@@ -33,13 +33,13 @@
THIS SOFTWARE.
*/
#include "mcc_generated_files/system/system.h"
#include <stdlib.h>
#include <math.h>
#include "commons.h"
#include "proto.h"
static uint16_t led1, led2, sensor;
static errors_t peer_err = SUCCESS, retcode = SUCCESS;
static uint8_t in_rx, cmd, peer_id = 0;
static uint16_t led1 = 0, led2 = 0, moisture = 0, light = 0;
static errors_t retcode = SUCCESS;
static uint8_t in_rx, cmd;
static bool rx_over = false, update_sensor = false;
volatile bool tx_timed_out = false;
@@ -47,6 +47,8 @@ volatile bool tx_timed_out = false;
// Private Declarations
//==============================================================================
static void init();
static void read_adc(adc_channel_t channel, uint16_t* p_val);
static void convert_adc_lux();
static void show_error();
static void tmr_tx_cb();
@@ -60,59 +62,6 @@ int main(void)
{
init();
// uint16_t i = 0;
// int change = 1;
//
// while(1)
// {
//// PWM3_LoadDutyValue(i);
//// PWM4_LoadDutyValue(i);
//// __delay_ms(1);
////
//// if (i == 1024)
//// change = -1;
//// else if (i == 0)
//// change = 1;
////
//// i += change;
//
// // Update sensor if it's time
// if (update_sensor)
// {
// update_sensor = false;
// ADC_StartConversion();
// while(!ADC_IsConversionDone());
// sensor = ADC_GetConversionResult();
// }
//
// retcode = uart_rx_byte(&in_rx);
// if (retcode == SUCCESS)
// {
// rx_over = byte_in(in_rx, &cmd, &peer_id, &led1, &led2, &sensor, &peer_err);
// if (rx_over)
// {
// switch (cmd)
// {
// case GET_CMD_ID:
// // Wait for Master to stop writing
// __delay_ms(DELAY_TO_REPLY);
// retcode = send_data(MY_ID, sensor);
// break;
// case SET_CMD_ID:
// PWM3_LoadDutyValue(led1);
// PWM4_LoadDutyValue(led2);
// __delay_ms(DELAY_TO_REPLY);
// retcode = send_error(MY_ID, SUCCESS);
// break;
// default:
// DEBUG_Toggle();
// retcode = UNSUPPORTED_CMD;
// break;
// }
// }
// }
// }
//
while(1)
{
if (RET_FAILURE(retcode))
@@ -122,46 +71,46 @@ int main(void)
if (update_sensor)
{
update_sensor = false;
ADC_StartConversion();
while(!ADC_IsConversionDone());
sensor = ADC_GetConversionResult();
#if (HAS_MOISTURE == 1)
read_adc(channel_ANC0, &moisture);
#endif
#if (HAS_LDR == 1)
read_adc(channel_ANA0, &light);
#endif
}
retcode = uart_rx_byte(&in_rx);
if (retcode == SUCCESS)
{
rx_over = byte_in(in_rx, &cmd, &peer_id, &led1, &led2, &sensor, &peer_err);
rx_over = byte_in(in_rx, &cmd, &led1, &led2);
if (rx_over)
{
switch (cmd)
{
#ifndef IS_MASTER
case GET_CMD_ID:
// Wait for Master to stop writing
case GET_MODEL_CMD_ID:
// Wait for Master to stop writing
__delay_ms(DELAY_TO_REPLY);
retcode = send_data(MY_ID, sensor);
retcode = send_model(MY_ID, MODEL);
break;
case SET_CMD_ID:
PWM3_LoadDutyValue(led1);
PWM4_LoadDutyValue(led2);
__delay_ms(DELAY_TO_REPLY);
retcode = send_error(MY_ID, SUCCESS);
break;
#else
case DATA_CMD_ID:
Serial.print("Sensor: ");
Serial.println(sensor);
break;
case ERROR_CMD_ID:
Serial.print("Peer error: ");
Serial.println(peer_err);
break;
case GET_CMD_ID:
// Wait for Master to stop writing
__delay_ms(DELAY_TO_REPLY);
retcode = send_data(MY_ID, moisture, light);
break;
case SET_CMD_ID:
#if (HAS_LED == 1)
PWM3_LoadDutyValue(led1);
PWM4_LoadDutyValue(led2);
__delay_ms(DELAY_TO_REPLY);
retcode = send_error(MY_ID, SUCCESS);
#endif
default:
retcode = UNSUPPORTED_CMD;
break;
break;
default:
retcode = UNSUPPORTED_CMD;
break;
}
}
}
@@ -181,7 +130,6 @@ static void init()
INTERRUPT_PeripheralInterruptEnable();
ADC_Initialize();
ADC_SelectChannel(channel_ANC0);
// TMR callback
TMR_TX_OverflowCallbackRegister(tmr_tx_cb);
@@ -197,6 +145,15 @@ static void init()
DEBUG_SetLow();
}
/*=****************************************************************************/
static void read_adc(adc_channel_t channel, uint16_t* p_val)
{
ADC_SelectChannel(channel);
ADC_StartConversion();
while(!ADC_IsConversionDone());
*p_val = ADC_GetConversionResult();
}
/*=****************************************************************************/
static void show_error()
{