Skip to content

Commit

Permalink
Using explicit int width types for data acquisition.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurb9 committed Feb 16, 2015
1 parent 6ef7ca8 commit fce6fc7
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 91 deletions.
16 changes: 8 additions & 8 deletions adc.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@

class ADCBase {
protected:
byte cur_mode;
uint8_t cur_mode;
public:
byte input; // analog input port connected to this ADC
byte bits; // ADC resolution in bits
virtual byte getModeCount(); // returns the number of available sampling rates (>0)
virtual bool setMode(byte mode); // set mode 0 - getModeCount()-1. True if successful.
virtual unsigned long getClock(); // return ADC clock in Hz
virtual unsigned long getSampleRate(); // sampling rate in Hz (actual read speed may vary)
virtual unsigned read(); // = analogRead(input)
uint8_t input; // analog input port connected to this ADC
uint8_t bits; // ADC resolution in bits
virtual uint8_t getModeCount(); // returns the number of available sampling rates (>0)
virtual bool setMode(uint8_t mode); // set mode 0 - getModeCount()-1. True if successful.
virtual uint32_t getClock(); // return ADC clock in Hz
virtual uint32_t getSampleRate(); // sampling rate in Hz (actual read speed may vary)
virtual uint16_t read(); // = analogRead(input)
};

#ifdef __MK20DX256__
Expand Down
16 changes: 8 additions & 8 deletions adc_avr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,31 @@
#ifdef __AVR__
#include "adc.h"

ADCInput::ADCInput(byte input)
ADCInput::ADCInput(uint8_t input)
:cur_mode(0),
input(input),
bits(ADC_BITS)
{
pinMode(input, INPUT);
}
byte ADCInput::prescalers[] = {7,6,5,4,3,2}; // 1:8MHz clock is out of ADC spec for 16MHz AVR
uint8_t ADCInput::prescalers[] = {7,6,5,4,3,2}; // 1:8MHz clock is out of ADC spec for 16MHz AVR

/*
* Get the number of
*/
byte ADCInput::getModeCount(){
uint8_t ADCInput::getModeCount(){
return sizeof(ADCInput::prescalers);
}

/*
* Set ADC prescaler.
* This should accept a clock or time base instead so it's not AVR specific.
*/
void ADCInput::setPrescaler(byte mode){
void ADCInput::setPrescaler(uint8_t mode){
// set prescaler
if (mode < ADCInput::getModeCount()){
cur_mode = mode;
byte prescaler = prescalers[mode];
uint8_t prescaler = prescalers[mode];
prescaler & 4 ? sbi(ADCSRA,ADPS2) : cbi(ADCSRA,ADPS2);
prescaler & 2 ? sbi(ADCSRA,ADPS1) : cbi(ADCSRA,ADPS1);
prescaler & 1 ? sbi(ADCSRA,ADPS0) : cbi(ADCSRA,ADPS0);
Expand All @@ -60,7 +60,7 @@ void ADCInput::setPrescaler(byte mode){
/*
* Configure ADC for given mode.
*/
bool ADCInput::setMode(byte mode){
bool ADCInput::setMode(uint8_t mode){
ADCInput::setPrescaler(mode);
return 1;
}
Expand All @@ -69,14 +69,14 @@ bool ADCInput::setMode(byte mode){
/*
* Return ADC clock in Hz. This is only useful to estimate sampling rate.
*/
unsigned long ADCInput::getClock(){
uint32_t ADCInput::getClock(){
return F_CPU/(1<<prescalers[cur_mode]);
}

/*
* Return ADC sampling rate, if available, otherwise 0
*/
unsigned long ADCInput::getSampleRate(){
uint32_t ADCInput::getSampleRate(){
return ADCInput::getClock() / ADC_CLOCK_TO_SAMPLING;
}

Expand Down
22 changes: 11 additions & 11 deletions adc_avr.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@

class ADCInput : public ADCBase {
protected:
byte cur_mode;
void setPrescaler(byte mode);
uint8_t cur_mode;
void setPrescaler(uint8_t mode);
public:
static byte prescalers[];
byte input; // analog input port
byte bits; // ADC resolution in bits
ADCInput(byte input);
byte getModeCount(); // returns the number of available rates
bool setMode(byte mode); // set sampling rate from the available values
unsigned long getClock(); // Hz for information purposes only
unsigned long getSampleRate(); // Hz for information purposes only
static uint8_t prescalers[];
uint8_t input; // analog input port
uint8_t bits; // ADC resolution in bits
ADCInput(uint8_t input);
uint8_t getModeCount(); // returns the number of available rates
bool setMode(uint8_t mode); // set sampling rate from the available values
uint32_t getClock(); // Hz for information purposes only
uint32_t getSampleRate(); // Hz for information purposes only

__inline__ unsigned read(){
__inline__ uint16_t read(){
return analogRead(input);
}
};
Expand Down
10 changes: 5 additions & 5 deletions adc_teensy3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#ifdef __MK20DX256__
#include "adc.h"

ADCInput::ADCInput(byte input)
ADCInput::ADCInput(uint8_t input)
:input(input),
bits(ADC_BITS)
{
Expand All @@ -21,28 +21,28 @@ ADCInput::ADCInput(byte input)
/*
* Get the number of
*/
byte ADCInput::getModeCount(){
uint8_t ADCInput::getModeCount(){
return 1;
}

/*
* Configure ADC for given mode.
*/
bool ADCInput::setMode(byte mode){
bool ADCInput::setMode(uint8_t mode){
return 1;
}

/*
* Return ADC clock in Hz. This is only useful to estimate sampling rate.
*/
unsigned long ADCInput::getClock(){
uint32_t ADCInput::getClock(){
return F_CPU/2;
}

/*
* Return ADC sampling rate, if available, otherwise 0
*/
unsigned long ADCInput::getSampleRate(){
uint32_t ADCInput::getSampleRate(){
return ADCInput::getClock() / ADC_CLOCK_TO_SAMPLING;
}

Expand Down
16 changes: 8 additions & 8 deletions adc_teensy3.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@

class ADCInput : public ADCBase {
public:
byte input; // analog input port
byte bits; // ADC resolution in bits
ADCInput(byte input);
byte getModeCount(); // returns the number of available rates
bool setMode(byte mode); // set sampling rate from the available values
unsigned long getClock(); // Hz for information purposes only
unsigned long getSampleRate(); // Hz for information purposes only
uint8_t input; // analog input port
uint8_t bits; // ADC resolution in bits
ADCInput(uint8_t input);
uint8_t getModeCount(); // returns the number of available rates
bool setMode(uint8_t mode); // set sampling rate from the available values
uint32_t getClock(); // Hz for information purposes only
uint32_t getSampleRate(); // Hz for information purposes only

__inline__ unsigned read(){
__inline__ uint16_t read(){
return analogRead(input);
}
};
Expand Down
20 changes: 10 additions & 10 deletions capture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
*/
#include "capture.h"

Capture::Capture(ADCInput adc, byte samples, unsigned rangemV)
Capture::Capture(ADCInput adc, unsigned samples, uint16_t rangemV)
:adc(adc),
samples(samples),
rangemV(rangemV)
{
}

int Capture::init(){
data = (unsigned*)calloc(samples+2, sizeof(unsigned)); // two extra reads: 0 and a non-zero
data = (uint16_t*)calloc(samples+2, sizeof(uint16_t)); // two extra reads: 0 and a non-zero
if (!data){
samples = 0;
}
Expand All @@ -33,23 +33,23 @@ int Capture::init(){
* on return, data contains the raw capture data.
*/
void Capture::capture(){
unsigned long start;
unsigned *dataCur;
unsigned v;
byte i = samples;
uint32_t start;
uint16_t *dataCur;
uint16_t v;
unsigned i = samples;

dataCur = data;
/*
* Attempt to latch on a zero transition
*/
for (byte j=255; j; j--){
for (unsigned j=255; j; j--){
v = adc.read();
if (v < ADC_JITTER){
*dataCur++ = v;
break;
}
}
for (byte j=255; j; j--){
for (unsigned j=255; j; j--){
v = adc.read();
if (v >= ADC_JITTER){
*dataCur++ = v;
Expand All @@ -69,10 +69,10 @@ void Capture::capture(){
* Convert captured data to mV and extract min/max values
*/
void Capture::tomV(){
unsigned v;
uint16_t v;
minmV = rangemV;
maxmV = 0;
for (int i=0; i<samples; i++){
for (unsigned i=0; i<samples; i++){
v = map(data[i], 0, (1L<<adc.bits)-1, 0, long(rangemV));
data[i] = v;
if (v < minmV){
Expand Down
10 changes: 5 additions & 5 deletions capture.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
class Capture {
public:
ADCInput adc;
byte samples;
unsigned minmV, maxmV, rangemV;
unsigned long elapsedus;
unsigned *data;
unsigned samples;
uint32_t elapsedus;
uint16_t minmV, maxmV, rangemV;
uint16_t *data;

Capture(ADCInput adc, byte samples, unsigned rangemV);
Capture(ADCInput adc, unsigned samples, uint16_t rangemV);
int init();
void capture();
void tomV();
Expand Down
6 changes: 3 additions & 3 deletions display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ int Display::printf(const __FlashStringHelper *format, ...){
/*
* Helper function to move cursor at text positions
*/
void Display::setTextCursor(byte row, byte col){
void Display::setTextCursor(uint8_t row, uint8_t col){
Display::setCursor(col*CHR_WIDTH, row*CHR_HEIGHT);
}

/*
* Helper function to print a number with corresponding unit (M, K, etc)
*/
void Display::printLargeUnits(unsigned long value, const char *unit){
void Display::printLargeUnits(uint32_t value, const char *unit){
if (value >= 1000000)
Display::printf(F("%lu.%01u M"), (value+50000)/1000000, ((value+50000) % 1000000)/100000);
else if (value >= 1000)
Expand All @@ -79,7 +79,7 @@ void Display::printLargeUnits(unsigned long value, const char *unit){
/*
* Helper method to print a fractional (micros) number with units (u, m)
*/
void Display::printSmallUnits(unsigned long value, const char *unit){
void Display::printSmallUnits(uint32_t value, const char *unit){
if (value < 100){
Display::printf(F("%lu u"), value);
} else if (value <= 500000){
Expand Down
6 changes: 3 additions & 3 deletions display.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class Display : public Adafruit_SSD1306 {
Display(int8_t RST);
void clearDisplay();
int printf(const char *format, ...);
void setTextCursor(byte row, byte col);
void printLargeUnits(unsigned long value, const char *unit);
void printSmallUnits(unsigned long value, const char *unit);
void setTextCursor(uint8_t row, uint8_t col);
void printLargeUnits(uint32_t value, const char *unit);
void printSmallUnits(uint32_t value, const char *unit);
#ifdef F // F() macro is available
int printf(const __FlashStringHelper *format, ...);
#endif
Expand Down
Loading

0 comments on commit fce6fc7

Please sign in to comment.