Skip to content

Commit

Permalink
Incorporate pieces of DigitalAttenuator firmware in SpectrumAnalyzer.
Browse files Browse the repository at this point in the history
Allows setting and reading a device ID. Gives some more robustness to input
command formatting.
  • Loading branch information
blakejohnson committed Mar 26, 2015
1 parent b4e5fc5 commit 0909c6d
Showing 1 changed file with 88 additions and 23 deletions.
111 changes: 88 additions & 23 deletions common/+deviceDrivers/@SpectrumAnalyzer/SpecAnalog/SpecAnalog.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,32 @@ template<class T> inline Print &operator <<(Print &obj, T arg) {
return obj;
}

#include <EEPROM.h>

const int NO_MESSAGE = -1;

// processInput states
const byte COMMAND = 0;
const byte NUMBER = 1;
const byte CHANNEL = 2;
const byte OPTIMIZE = 3;
const byte READPOWER = 4;
const byte DONE = 5;
const byte DONE = 10;

// command modes
const byte READPOWER = 1; // read ADC value
const byte GETID = 2; // get device ID
const byte SETID = 3; // set device ID
const byte UNKNOWN = -1;

const int sensorPin = A5; // pin for analog input from log amp
const int numAverages = 50;
const int verbose = 0;

const boolean VERBOSE = false;

/**
* EEPROM memory map (total size = 1024 bytes)
* bytes 0-1: serial number
*/

const int ID_OFFSET = 0;

/* for simulating operation */
const int simulate = 0;
Expand All @@ -34,16 +49,15 @@ void setup() {
void loop() {
char input[255];
int output;

readString(input);
//Serial.println(input);
output = processInput(input);
if ( output == NO_MESSAGE ) {
Serial.println("ERROR Unknown command");
}

while (Serial.available() <= 0) {
delay(100);

if (Serial.available() > 0) {
readString(input);
if (VERBOSE) Serial.println(input);
output = processInput(input);
if ( output == NO_MESSAGE ) {
Serial.println("ERROR Unknown command");
}
Serial.flush();
}
}

Expand All @@ -53,14 +67,27 @@ void loop() {

void readString(char* s) {
int readCount = 0;
while (Serial.available() > 0 && readCount < 255) {
s[readCount++] = Serial.read();
unsigned long startTime = millis();
char val;
// terminate at 255 chars, a semicolon, or 100 ms timeout
while (readCount < 255 && (millis() - startTime) < 100) {
if (Serial.available() == 0) {
continue;
}
val = Serial.read();
if (val != '\n' && val != '\r' && val != ';') { // also ignore linefeeds and carriage returns
s[readCount++] = val;
}
else {
break;
}
}
s[readCount] = 0;
}

int processInput(char* input) {
byte mode = COMMAND;
byte mode = COMMAND; // state
byte command = UNKNOWN; // the interpretted command mode
int value = NO_MESSAGE;
char* token = strtok(input, " ");
while (token) {
Expand All @@ -71,24 +98,47 @@ int processInput(char* input) {
switch (mode) {
case COMMAND:
if (!strcasecmp(token, "READ")) {
value = READPOWER;
mode = READPOWER;
command = READPOWER;
mode = DONE;
}
if (!strcasecmp(token, "ID?")) {
command = GETID;
mode = DONE;
}
if (!strcasecmp(token, "ID")) {
command = SETID;
mode = NUMBER;
}
break;
case NUMBER:
value = constrain(atoi(token), 0, 1023);
value = atoi(token);
mode = DONE;
break;
}

token = strtok(NULL, " ");
}

if (mode == READPOWER) {
if (mode != DONE) {
return NO_MESSAGE;
}

if (command == READPOWER) {
Serial << readPower();
Serial.println();
return DONE;
} else if (command == GETID) {
Serial << getID();
Serial.println();
return DONE;
} else if (command == SETID) {
Serial << "Setting board ID to " << value;
Serial.println();
setID(value);
return DONE;
} else {
return NO_MESSAGE;
}
return DONE;
}

double analogReadAverage(int pin, int numavg) {
Expand All @@ -108,3 +158,18 @@ double readPower() {
return analogReadAverage(sensorPin, numAverages);
}

// get first two bytes from EEPROM memory
unsigned int getID(void) {
unsigned int id = word( EEPROM.read(ID_OFFSET+1), EEPROM.read(ID_OFFSET) );
return id;
}

// write ID into first two bytes of EEPROM memory
void setID(unsigned int id) {
if (VERBOSE) {
Serial << "Low Byte 0: " << (id & 0xFF) << "\n";
Serial << "High Byte 1: " << (id >> 7) << "\n";
}
EEPROM.write(ID_OFFSET, id & 0xFF);
EEPROM.write(ID_OFFSET+1, (id >> 7) );
}

0 comments on commit 0909c6d

Please sign in to comment.