ADC integrated into console

This commit is contained in:
rusefillc 2022-02-19 10:19:33 -05:00
parent b09bfc7d2c
commit 760822ca32
6 changed files with 97 additions and 3 deletions

View File

@ -131,6 +131,7 @@ CSRC = $(ALLCSRC) \
CPPSRC = $(ALLCPPSRC) \
main.cpp \
source/usbconsole.cpp \
efilib.cpp \
adc.cpp \
digital_inputs.cpp \

View File

@ -9,7 +9,6 @@
*/
#define ADC_GRP_NUM_CHANNELS 3
#define ADC_GRP_BUF_DEPTH 8
static adcsample_t samples[ADC_GRP_NUM_CHANNELS * ADC_GRP_BUF_DEPTH];
@ -44,6 +43,20 @@ static const ADCConversionGroup adcgrpcfg = {
/*.sqr3 = */0
};
static adcsample_t getAvgAdcValue(int index, adcsample_t *samples, int bufDepth, int numChannels) {
uint32_t result = 0;
for (int i = 0; i < bufDepth; i++) {
result += samples[index];
index += numChannels;
}
// this truncation is guaranteed to not be lossy - the average can't be larger than adcsample_t
return static_cast<adcsample_t>(result / bufDepth);
}
adcsample_t getAdcValue(int channel) {
return getAvgAdcValue(channel, samples, ADC_GRP_BUF_DEPTH, ADC_GRP_NUM_CHANNELS);
}
void initAnalogInputs() {
/*

View File

@ -2,4 +2,9 @@
#include "global.h"
void initAnalogInputs();
typedef uint16_t adcsample_t;
#define ADC_GRP_NUM_CHANNELS 3
void initAnalogInputs();
adcsample_t getAdcValue(int channel);

View File

@ -0,0 +1,63 @@
#include <string.h>
#include <stdint.h>
#include "efilib.h"
static char *ltoa_internal(char *p, uint32_t num, unsigned radix) {
constexpr int bufferLength = 10;
char buffer[bufferLength];
size_t idx = bufferLength - 1;
// First, we write from right-to-left so that we don't have to compute
// log(num)/log(radix)
do
{
auto digit = num % radix;
// Digits 0-9 -> '0'-'9'
// Digits 10-15 -> 'a'-'f'
char c = digit < 10
? digit + '0'
: digit + 'a' - 10;
// Write this digit in to the buffer
buffer[idx] = c;
idx--;
} while ((num /= radix) != 0);
idx++;
// Now, we copy characters in to place in the final buffer
while (idx < bufferLength)
{
*p++ = buffer[idx++];
}
return p;
}
/**
* @return pointer at the end zero symbol after the digits
*/
static char* itoa_signed(char *p, int num, unsigned radix) {
if (num < 0) {
*p++ = '-';
char *end = ltoa_internal(p, -num, radix);
*end = 0;
return end;
}
char *end = ltoa_internal(p, num, radix);
*end = 0;
return end;
}
/**
* Integer to string
*
* @return pointer at the end zero symbol after the digits
*/
char* itoa10(char *p, int num) {
return itoa_signed(p, num, 10);
}

View File

@ -8,3 +8,5 @@ constexpr size_t size(const T(&)[N]) {
return N;
}
}
char* itoa10(char *p, int num);

View File

@ -20,6 +20,7 @@
#include "chprintf.h"
#include "digital_inputs.h"
#include "adc.h"
#include "efilib.h"
BaseSequentialStream *chp = (BaseSequentialStream *)&EFI_CONSOLE_USB_DEVICE;
@ -51,7 +52,16 @@ static THD_FUNCTION(Thread1, arg) {
static THD_WORKING_AREA(consoleThread, 256);
static void ConsoleThread(void*) {
while (true) {
chprintf(chp, "Hello\r\n");
char buf[6];
for (int i = 0;i<ADC_GRP_NUM_CHANNELS;i++) {
int value = getAdcValue(i);
itoa10(buf, value);
chprintf(chp, buf);
chprintf(chp, " ");
}
chprintf(chp, "Hello\r\n");
chThdSleepMilliseconds(200);
}
}