Skip to content
Snippets Groups Projects
Commit bfa677bb authored by Elisabetta Pianori's avatar Elisabetta Pianori
Browse files

Merge branch 'juerg-arduino' into 'devel'

Extend Arduino devcomuino to support digital IO pins and Arduino Nano

See merge request !258
parents 9fedd424 93892a29
Branches
No related tags found
4 merge requests!308Bring main and devel back in sync,!300playing with fixing divergence between devel and main,!269Merge devel into main: largest change is code formatting checker and enforcement in CI,!258Extend Arduino devcomuino to support digital IO pins and Arduino Nano
Pipeline #5935387 passed
// Arduino Uno
#if defined(ARDUINO_AVR_UNO)
static const uint8_t analog_pins[] = {A0, A1, A2, A3, A4, A5}; // Arduino Uno
static const uint8_t analog_pins[] = {A0, A1, A2, A3, A4, A5};
static const uint8_t n_digital_pins = 14;
// Arduino mega
#elif defined(ARDUINO_AVR_MEGA2560)
static const uint8_t analog_pins[] = {
A0, A1, A2, A3, A4, A5, A6, A7,
A8, A9, A10, A11, A12, A13, A14, A15}; // Arduino mega
static const uint8_t analog_pins[] = {A0, A1, A2, A3, A4, A5, A6, A7,
A8, A9, A10, A11, A12, A13, A14, A15};
static const uint8_t n_digital_pins = 54;
// Arduino Nano
#elif defined(ARDUINO_AVR_NANO)
static const uint8_t analog_pins[] = {A0, A1, A2, A3, A4, A5, A6, A7};
static const uint8_t n_digital_pins = 22;
#endif
......@@ -5,7 +5,7 @@
const unsigned int MAXARGS = 5;
const unsigned int MAXBYTES = 16;
const unsigned int MAXCMDLENGTH = 256;
const unsigned int MAXCMDLENGTH = 64;
const unsigned int MAXARGLENGTH = 16;
// Command parsing
......@@ -31,7 +31,7 @@ void runcommand() {
// need to check ADC functionality
else if (strncmp("ADC", argv[0], 3) == 0) { // Read ADC
if (argc != 2) {
Serial.println("ERR wrong number of arguments to ADC");
Serial.println("ERR wrong number of args to ADC");
return;
}
cmdADC(atoi(argv[1]));
......@@ -41,7 +41,7 @@ void runcommand() {
if (strncmp("WRITE", argv[1], 5) == 0) {
if (argc < 4) {
Serial.println("ERR wrong number of arguments to EEPROM WRITE");
Serial.println("ERR wrong number of args to EEPROM WRITE");
return;
}
......@@ -52,7 +52,7 @@ void runcommand() {
if (strncmp("READ", argv[1], 4) == 0) {
if (argc < 3) {
Serial.println("ERR wrong number of arguments to EEPROM READ");
Serial.println("ERR wrong number of args to EEPROM READ");
return;
}
int address = atoi(argv[2]);
......@@ -61,7 +61,7 @@ void runcommand() {
} else if (strncmp("I2C", argv[0], 3) == 0) { // I2C commands
if (argc < 4) {
Serial.println("ERR wrong number of arguments to I2C");
Serial.println("ERR wrong number of args to I2C");
return;
}
......@@ -74,9 +74,30 @@ void runcommand() {
} else {
Serial.println("ERR unknown I2C command");
}
}
else {
} else if (strncmp("DGT", argv[0], 3) == 0) { // DGT commands
if (argc == 3) {
int chan = atoi(argv[2]);
if (strncmp("OUT", argv[1], 3) == 0) {
cmdDGToutput(chan);
} else if (strncmp("IN", argv[1], 2) == 0) {
cmdDGTinput(chan);
} else if (strncmp("PULLUP", argv[1], 6) == 0) {
cmdDGTinputpullup(chan);
} else if (strncmp("READ", argv[1], 4) == 0) {
cmdDGTread(chan);
} else {
Serial.println("ERR unknown DGT command");
}
} else if (argc == 4 && strncmp("WRITE", argv[1], 5) == 0) {
int chan = atoi(argv[2]);
int value = atoi(argv[3]);
cmdDGTwrite(chan, value);
} else {
Serial.println("ERR wrong number of args to DGT");
return;
}
} else {
Serial.println("ERR unknown command");
}
}
......@@ -88,24 +109,27 @@ void runcommand() {
//
// Print
void cmdHELP() {
Serial.println("Hello World from DevComduino!");
Serial.println(F("Hello World from DevComuino!"));
Serial.println("");
Serial.println("Available commands:");
Serial.println("\tHELP - Print this help");
Serial.println("\tADC ch - Return ADC reading on channel ch");
Serial.println(
"\tI2C WRITE addr byte-string - Write a byte-string using I2C to addr, "
"MSB first");
Serial.println("\tI2C READ addr nbytes - Read number of bytes from addr");
Serial.println(F("Commands:"));
Serial.println(F("\tHELP - This help"));
Serial.println(F("\tADC ch - Read ADC channel ch"));
Serial.println(
"\tEEPROM WRITE addr value - Write a value to a addr in EEPROM");
F("\tI2C WRITE addr byte-string - Write byte-string to I2C addr, "
"MSB first"));
Serial.println(F("\tI2C READ addr nbytes - Read nbytes from I2C addr"));
Serial.println(F("\tEEPROM WRITE addr value - Write to addr in EEPROM"));
Serial.println(F("\tEEPROM READ addr - Read addr from EEPROM"));
Serial.println(F("\tDGT OUT ch - Set digital pin ch as output"));
Serial.println(F("\tDGT IN ch - Set digital pin ch as input"));
Serial.println(F("\tDGT PULLUP ch - Set digital pin ch as input w/pullup"));
Serial.println(F("\tDGT READ ch - Read channel ch"));
Serial.println(
"\tEEPROM READ addr - Read number of bytes from addr from EEPROM");
F("\tDGT WRITE ch 0|1 - Set channel ch to LOW (0) or HIGH (0)"));
}
//
// Read an analogue pin
void cmdADC(int channel) {
float V;
......@@ -166,6 +190,61 @@ void cmdI2Cread(int address, unsigned int nBytes) {
Serial.println();
}
//
// DGT input
void cmdDGTinput(int channel) {
if (channel < n_digital_pins) {
pinMode(channel, INPUT);
Serial.println("OK");
} else {
Serial.println("ERR invalid channel");
}
}
//
// DGT input with pullup resistors
void cmdDGTinputpullup(int channel) {
if (channel < n_digital_pins) {
pinMode(channel, INPUT_PULLUP);
Serial.println("OK");
} else {
Serial.println("ERR invalid channel");
}
}
//
// DGT output
void cmdDGToutput(int channel) {
if (channel < n_digital_pins) {
pinMode(channel, OUTPUT);
Serial.println("OK");
} else {
Serial.println("ERR invalid channel");
}
}
//
// DGT write
void cmdDGTwrite(int channel, int value) {
if (channel < n_digital_pins) {
if (value == 0) {
digitalWrite(channel, LOW);
} else {
digitalWrite(channel, HIGH);
}
Serial.println("OK");
} else {
Serial.println("ERR invalid channel");
}
}
//
// DGT read
void cmdDGTread(int channel) {
uint8_t data = digitalRead(channel);
Serial.println(data);
}
//
// The big main loop
//
......@@ -173,7 +252,7 @@ void cmdI2Cread(int address, unsigned int nBytes) {
//
// Setup serial
void setup() {
Serial.begin(9600);
Serial.begin(115200);
Wire.begin();
}
......@@ -194,7 +273,7 @@ void loop() {
if (cmdptr >= MAXCMDLENGTH - 1) {
// overflow command. Clean-up buffer to avoid stalled program
cmdptr = 0;
Serial.print("ERR Command too long");
Serial.print("ERR command too long");
}
return;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment