From be6f1f63ad1385b9fedff38cafde3bb5fe88ce42 Mon Sep 17 00:00:00 2001 From: Unreal-Dan <72595612+Unreal-Dan@users.noreply.github.com> Date: Mon, 1 Jan 2024 01:22:37 -0800 Subject: [PATCH] generalized core by removing embedded specifics (#199) --- VortexEngine/src/Log/Log.cpp | 37 -------- VortexEngine/src/Serial/Serial.cpp | 4 - VortexEngine/src/Wireless/IRSender.cpp | 85 ------------------- VortexEngine/src/Wireless/IRSender.h | 5 -- VortexEngine/src/Wireless/VLReceiver.cpp | 103 ----------------------- 5 files changed, 234 deletions(-) diff --git a/VortexEngine/src/Log/Log.cpp b/VortexEngine/src/Log/Log.cpp index 3f4660c2e4..19877548da 100644 --- a/VortexEngine/src/Log/Log.cpp +++ b/VortexEngine/src/Log/Log.cpp @@ -14,20 +14,9 @@ #if LOGGING_LEVEL > 0 void InfoMsg(const char *msg, ...) { -#ifdef VORTEX_EMBEDDED - if (!SerialComs::isConnected()) { - return; - } -#endif va_list list; va_start(list, msg); -#ifdef VORTEX_EMBEDDED - char buf[2048] = {0}; - vsnprintf(buf, sizeof(buf), msg, list); - Serial.println(buf); -#else Vortex::printlog(NULL, NULL, 0, msg, list); -#endif va_end(list); } #endif @@ -35,22 +24,9 @@ void InfoMsg(const char *msg, ...) #if LOGGING_LEVEL > 1 void ErrorMsg(const char *func, const char *msg, ...) { -#ifdef VORTEX_EMBEDDED - if (!SerialComs::isConnected()) { - return; - } -#endif va_list list; va_start(list, msg); -#ifdef VORTEX_EMBEDDED - char fmt[2048] = {0}; - snprintf(fmt, sizeof(fmt), "%s(): %s", func, msg); - char buf[2048] = {0}; - vsnprintf(buf, sizeof(buf), fmt, list); - Serial.println(buf); -#else Vortex::printlog(NULL, func, 0, msg, list); -#endif va_end(list); } #endif @@ -58,11 +34,6 @@ void ErrorMsg(const char *func, const char *msg, ...) #if LOGGING_LEVEL > 2 void DebugMsg(const char *file, const char *func, int line, const char *msg, ...) { -#ifdef VORTEX_EMBEDDED - if (!SerialComs::isConnected()) { - return; - } -#endif va_list list; va_start(list, msg); const char *ptr = file + strlen(file); @@ -75,15 +46,7 @@ void DebugMsg(const char *file, const char *func, int line, const char *msg, ... } ptr--; } -#ifdef VORTEX_EMBEDDED - char fmt[2048] = {0}; - snprintf(fmt, sizeof(fmt), "%s:%d %s(): %s", file, line, func, msg); - char buf[2048] = {0}; - vsnprintf(buf, sizeof(buf), fmt, list); - Serial.println(buf); -#else Vortex::printlog(file, func, line, msg, list); -#endif va_end(list); } #endif diff --git a/VortexEngine/src/Serial/Serial.cpp b/VortexEngine/src/Serial/Serial.cpp index f858954b39..2d3c4bd343 100644 --- a/VortexEngine/src/Serial/Serial.cpp +++ b/VortexEngine/src/Serial/Serial.cpp @@ -12,10 +12,6 @@ #include #endif -#ifdef VORTEX_EMBEDDED -#include -#endif - bool SerialComs::m_serialConnected = false; uint32_t SerialComs::m_lastCheck = 0; diff --git a/VortexEngine/src/Wireless/IRSender.cpp b/VortexEngine/src/Wireless/IRSender.cpp index 61fda5d769..187a429285 100644 --- a/VortexEngine/src/Wireless/IRSender.cpp +++ b/VortexEngine/src/Wireless/IRSender.cpp @@ -31,15 +31,8 @@ uint32_t IRSender::m_blockSize = 0; // write total uint32_t IRSender::m_writeCounter = 0; -#if defined(VORTEX_EMBEDDED) -// Timer used for PWM, is initialized in initpwm() -Tcc *IR_TCCx; -#endif - bool IRSender::init() { - // initialize the IR device - initPWM(); return true; } @@ -119,8 +112,6 @@ void IRSender::beginSend() m_isSending = true; DEBUG_LOGF("[%zu] Beginning send size %u (blocks: %u remainder: %u blocksize: %u)", Time::microseconds(), m_size, m_numBlocks, m_remainder, m_blockSize); - // init sender before writing, is this necessary here? I think so - initPWM(); // wakeup the other receiver with a very quick mark/space sendMark(50); sendSpace(100); @@ -154,9 +145,6 @@ void IRSender::sendMark(uint16_t time) #ifdef VORTEX_LIB // send mark timing over socket Vortex::vcallbacks()->infraredWrite(true, time); -#else - startPWM(); - Time::delayMicroseconds(time); #endif } @@ -165,79 +153,6 @@ void IRSender::sendSpace(uint16_t time) #ifdef VORTEX_LIB // send space timing over socket Vortex::vcallbacks()->infraredWrite(false, time); -#else - stopPWM(); - Time::delayMicroseconds(time); -#endif -} - -// shamelessly stolen from IRLib2, thanks -void IRSender::initPWM() -{ -#if defined(VORTEX_EMBEDDED) - // just in case - pinMode(IR_SEND_PWM_PIN, OUTPUT); - digitalWrite(IR_SEND_PWM_PIN, LOW); // When not sending PWM, we want it low - uint8_t port = g_APinDescription[IR_SEND_PWM_PIN].ulPort; // 0 - uint8_t pin = g_APinDescription[IR_SEND_PWM_PIN].ulPin; // 8 - ETCChannel IR_TCC_Channel = TCC0_CH0; - int8_t IR_PER_EorF = PORT_PMUX_PMUXE_E; - //println();Serial.print("Port:"); Serial.print(port,DEC); Serial.print(" Pin:"); Serial.println(pin,DEC); - // Enable the port multiplexer for the PWM channel on pin - PORT->Group[port].PINCFG[pin].bit.PMUXEN = 1; - - // Connect the TCC timer to the port outputs - port pins are paired odd PMUXO and even PMUXEII - // F & E peripherals specify the timers: TCC0, TCC1 and TCC2 - PORT->Group[port].PMUX[pin >> 1].reg |= IR_PER_EorF; - -// pinPeripheral (IR_SEND_PWM_PIN,PIO_TIMER_ALT); - // Feed GCLK0 to TCC0 and TCC1 - REG_GCLK_CLKCTRL = GCLK_CLKCTRL_CLKEN | // Enable GCLK0 to TCC0 and TCC1 - GCLK_CLKCTRL_GEN_GCLK0 | // Select GCLK0 - GCLK_CLKCTRL_ID_TCC0_TCC1; // Feed GCLK0 to TCC0 and TCC1 - while (GCLK->STATUS.bit.SYNCBUSY); // Wait for synchronization - - // Normal (single slope) PWM operation: timers countinuously count up to PER - // register value and then is reset to 0 - IR_TCCx = (Tcc*) GetTC(IR_TCC_Channel); - IR_TCCx->WAVE.reg |= TCC_WAVE_WAVEGEN_NPWM; // Setup single slope PWM on TCCx - while (IR_TCCx->SYNCBUSY.bit.WAVE); // Wait for synchronization - - // Each timer counts up to a maximum or TOP value set by the PER register, - // this determines the frequency of the PWM operation. - uint32_t cc = F_CPU/(38*1000) - 1; - IR_TCCx->PER.reg = cc; // Set the frequency of the PWM on IR_TCCx - while(IR_TCCx->SYNCBUSY.bit.PER); - - // The CCx register value corresponds to the pulsewidth in microseconds (us) - // Set the duty cycle of the PWM on TCC0 to 33% - IR_TCCx->CC[GetTCChannelNumber(IR_TCC_Channel)].reg = cc/3; - while (IR_TCCx->SYNCBUSY.reg & TCC_SYNCBUSY_MASK); - //while(IR_TCCx->SYNCBUSY.bit.CC3); - - // Enable IR_TCCx timer but do not turn on PWM yet. Will turn it on later. - IR_TCCx->CTRLA.reg |= TCC_CTRLA_PRESCALER_DIV1; // Divide GCLK0 by 1 - while (IR_TCCx->SYNCBUSY.bit.ENABLE); - IR_TCCx->CTRLA.reg &= ~TCC_CTRLA_ENABLE; //initially off will turn on later - while (IR_TCCx->SYNCBUSY.bit.ENABLE); -#endif -} - -void IRSender::startPWM() -{ -#if defined(VORTEX_EMBEDDED) - // start the PWM - IR_TCCx->CTRLA.reg |= TCC_CTRLA_ENABLE; - while (IR_TCCx->SYNCBUSY.bit.ENABLE); -#endif -} - -void IRSender::stopPWM() -{ -#if defined(VORTEX_EMBEDDED) - // stop the PWM - IR_TCCx->CTRLA.reg &= ~TCC_CTRLA_ENABLE; - while (IR_TCCx->SYNCBUSY.bit.ENABLE); #endif } diff --git a/VortexEngine/src/Wireless/IRSender.h b/VortexEngine/src/Wireless/IRSender.h index d8e9df30eb..8f65fa7048 100644 --- a/VortexEngine/src/Wireless/IRSender.h +++ b/VortexEngine/src/Wireless/IRSender.h @@ -34,11 +34,6 @@ class IRSender // send a mark/space by turning PWM on/off static void sendMark(uint16_t time); static void sendSpace(uint16_t time); - // Pulse-Width Modulator (IR Transmitter) - static void initPWM(); - // turn the IR transmitter on/off in realtime - static void startPWM(); - static void stopPWM(); // the serial buffer for the data static ByteStream m_serialBuf; diff --git a/VortexEngine/src/Wireless/VLReceiver.cpp b/VortexEngine/src/Wireless/VLReceiver.cpp index f4a7815213..d418986fb0 100644 --- a/VortexEngine/src/Wireless/VLReceiver.cpp +++ b/VortexEngine/src/Wireless/VLReceiver.cpp @@ -7,11 +7,6 @@ #include "../Leds/Leds.h" #include "../Log/Log.h" -#ifdef VORTEX_EMBEDDED -#include -#include -#endif - #if VL_ENABLE_RECEIVER == 1 BitStream VLReceiver::m_vlData; @@ -20,51 +15,8 @@ uint32_t VLReceiver::m_prevTime = 0; uint8_t VLReceiver::m_pinState = 0; uint32_t VLReceiver::m_previousBytes = 0; -#ifdef VORTEX_EMBEDDED -#define MIN_THRESHOLD 200 -#define BASE_OFFSET 100 -#define THRESHOLD_BEGIN (MIN_THRESHOLD + BASE_OFFSET) -// the sample count exponent, so 5 means 2^5 = 32 samples -// 0 NONE No accumulation > doesn't work -// 1 ACC2 2 results accumulated > doesn't work -// 2 ACC4 4 results accumulated > works okay -// 3 ACC8 8 results accumulated > works decent -// 4 ACC16 16 results accumulated > works very well -// 5 ACC32 32 results accumulated > works best -// 6 ACC64 64 results accumulated > doesn't work -#define SAMPLE_COUNT 5 -// the threshold needs to start high then it will be automatically pulled down -uint16_t threshold = THRESHOLD_BEGIN; -ISR(ADC0_WCOMP_vect) -{ - // this will store the last known state - static bool wasAboveThreshold = false; - // grab the current analog value but divide it by 8 (the number of samples) - uint16_t val = (ADC0.RES >> SAMPLE_COUNT); - // calculate a threshold by using the baseline minimum value that is above 0 - // with a static offset, this ensures whatever the baseline light level and/or - // hardware sensitivity is it will always pick a threshold just above the 'off' - if (val > MIN_THRESHOLD && val < (threshold + BASE_OFFSET)) { - threshold = val + BASE_OFFSET; - } - // compare the current analog value to the light threshold - bool isAboveThreshold = (val > threshold); - if (wasAboveThreshold != isAboveThreshold) { - VLReceiver::recvPCIHandler(); - wasAboveThreshold = isAboveThreshold; - } - // Clear the Window Comparator interrupt flag - ADC0.INTFLAGS = ADC_WCMP_bm; -} -#endif - bool VLReceiver::init() { -#ifdef VORTEX_EMBEDDED - // Disable digital input buffer on the pin to save power - PORTB.PIN1CTRL &= ~PORT_ISC_gm; - PORTB.PIN1CTRL |= PORT_ISC_INPUT_DISABLE_gc; -#endif return m_vlData.init(VL_RECV_BUF_SIZE); } @@ -130,63 +82,12 @@ bool VLReceiver::receiveMode(Mode *pMode) bool VLReceiver::beginReceiving() { -#ifdef VORTEX_EMBEDDED - // Set up the ADC - // sample campacitance, VDD reference, prescaler division - // Options are: - // 0x0 DIV2 CLK_PER divided by 2 > works - // 0x1 DIV4 CLK_PER divided by 4 > works - // 0x2 DIV8 CLK_PER divided by 8 > works - // 0x3 DIV16 CLK_PER divided by 16 > works - // 0x4 DIV32 CLK_PER divided by 32 > doesn't work - // 0x5 DIV64 CLK_PER divided by 64 > doesn't work - // 0x6 DIV128 CLK_PER divided by 128 > doesn't work - // 0x7 DIV256 CLK_PER divided by 256 > doesn't work -#if (F_CPU == 20000000) - ADC0.CTRLC = ADC_SAMPCAP_bm | ADC_REFSEL_VDDREF_gc | ADC_PRESC_DIV2_gc; -#else - ADC0.CTRLC = ADC_SAMPCAP_bm | ADC_REFSEL_VDDREF_gc | ADC_PRESC_DIV2_gc; -#endif - // no sampling delay and no delay variation - ADC0.CTRLD = 0; - // sample length - // 0 = doesn't work - // 1+ = works - ADC0.SAMPCTRL = 1; - // Select the analog pin input PB1 (AIN10) - ADC0.MUXPOS = ADC_MUXPOS_AIN10_gc; - // Initialize the Window Comparator Mode in above - ADC0.CTRLE = ADC_WINCM_ABOVE_gc; - // Set the threshold value very low - ADC0.WINHT = 0x1; - ADC0.WINLT = 0; - // set sampling amount - // 0x0 NONE No accumulation > doesn't work - // 0x1 ACC2 2 results accumulated > doesn't work - // 0x2 ACC4 4 results accumulated > works okay - // 0x3 ACC8 8 results accumulated > works decent - // 0x4 ACC16 16 results accumulated > works very well - // 0x5 ACC32 32 results accumulated > works best - // 0x6 ACC64 64 results accumulated > doesn't work - ADC0.CTRLB = SAMPLE_COUNT; - // Enable Window Comparator interrupt - ADC0.INTCTRL = ADC_WCMP_bm; - // Enable the ADC and start continuous conversions - ADC0.CTRLA = ADC_ENABLE_bm | ADC_FREERUN_bm; - // start the first conversion - ADC0.COMMAND = ADC_STCONV_bm; -#endif resetVLState(); return true; } bool VLReceiver::endReceiving() { -#ifdef VORTEX_EMBEDDED - // Stop conversions and disable the ADC - ADC0.CTRLA &= ~(ADC_ENABLE_bm | ADC_FREERUN_bm); - ADC0.INTCTRL = 0; -#endif resetVLState(); return true; } @@ -295,10 +196,6 @@ void VLReceiver::resetVLState() m_recvState = WAITING_HEADER_MARK; // zero out the receive buffer and reset bit receiver position m_vlData.reset(); -#ifdef VORTEX_EMBEDDED - // reset the threshold to a high value so that it can be pulled down again - threshold = THRESHOLD_BEGIN; -#endif DEBUG_LOG("VL State Reset"); }