Skip to content

Commit

Permalink
fixes after merging master
Browse files Browse the repository at this point in the history
  • Loading branch information
Unreal-Dan committed Jan 1, 2024
1 parent 9fc75b2 commit b2ca979
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 103 deletions.
1 change: 1 addition & 0 deletions VortexEngine/src/VortexEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "Wireless/IRReceiver.h"
#include "Wireless/IRSender.h"
#include "Wireless/VLReceiver.h"
#include "Wireless/VLSender.h"
#include "Wireless/IRConfig.h"
#include "Wireless/VLConfig.h"
Expand Down
103 changes: 0 additions & 103 deletions VortexEngine/src/Wireless/VLReceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,62 +10,14 @@
#include "../Leds/Leds.h"
#include "../Log/Log.h"

#ifdef VORTEX_EMBEDDED
#include <avr/interrupt.h>
#include <avr/io.h>
#endif

BitStream VLReceiver::m_vlData;
VLReceiver::RecvState VLReceiver::m_recvState = WAITING_HEADER_MARK;
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);
}

Expand Down Expand Up @@ -131,63 +83,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;
}
Expand Down Expand Up @@ -296,10 +197,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");
}

Expand Down

0 comments on commit b2ca979

Please sign in to comment.