diff --git a/arduino/devcomuino/devcomuino.ino b/arduino/devcomuino/devcomuino.ino index 0b88b611279d6cabe42af6c995d477bf368362c3..74fb1973ec8ce30eb1ee76ce55d784bb0fb966f7 100644 --- a/arduino/devcomuino/devcomuino.ino +++ b/arduino/devcomuino/devcomuino.ino @@ -1,4 +1,5 @@ #include "Wire.h" +#include <EEPROM.h> #include "devcomuino.h" const unsigned int MAXARGS=5; @@ -39,6 +40,35 @@ void runcommand() } cmdADC(atoi(argv[1])); } + + else if(strncmp("EEPROM", argv[0], 6)==0) + { // EEPROM commands + + if(strncmp("WRITE", argv[1], 5)==0) + { + if(argc<4) + { + Serial.println("ERR wrong number of arguments to EEPROM WRITE"); + return; + } + + int address = atoi(argv[2]); + int value = atoi(argv[3]); + cmdEEPROMwrite(address, value); + } + + if(strncmp("READ", argv[1], 4)==0) + { + if(argc<3) + { + Serial.println("ERR wrong number of arguments to EEPROM READ"); + return; + } + int address = atoi(argv[2]); + cmdEEPROMread(address); + } + + } else if(strncmp("I2C", argv[0], 3)==0) { // I2C commands if(argc<4) @@ -84,6 +114,8 @@ void cmdHELP() 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("\tEEPROM WRITE addr value - Write a value to a addr in EEPROM"); + Serial.println("\tEEPROM READ addr - Read number of bytes from addr from EEPROM"); } // @@ -102,6 +134,21 @@ void cmdADC(int channel) } } +// +// EEPROM Write +void cmdEEPROMwrite(int address, int value) +{ + EEPROM.write(address,value); +} + +// +// EEPROM Read +void cmdEEPROMread(int address) +{ + int val = EEPROM.read(address); + Serial.println(val); +} + // // I2C write void cmdI2Cwrite(int address, char *cmd) diff --git a/src/tools/CMakeLists.txt b/src/tools/CMakeLists.txt index 4ae1192aaf7baf625f5fa50658e93e129fd016b2..56af09bd57398b9f29bd6f842e27f071c269a020 100644 --- a/src/tools/CMakeLists.txt +++ b/src/tools/CMakeLists.txt @@ -39,6 +39,12 @@ add_executable(ps_monitor) target_sources(ps_monitor PRIVATE ps_monitor.cpp) target_link_libraries(ps_monitor PRIVATE Utils PS EquipConf) +# +# arduino_eeprom +add_executable(arduino_eeprom) +target_sources(arduino_eeprom PRIVATE arduino_eeprom.cpp) +target_link_libraries(arduino_eeprom PRIVATE Utils DevCom) + # # gpibscan add_executable(gpibscan) diff --git a/src/tools/arduino_eeprom.cpp b/src/tools/arduino_eeprom.cpp new file mode 100644 index 0000000000000000000000000000000000000000..36d4fc052a35a0f4f6adc1e0faafc9957abf6bb7 --- /dev/null +++ b/src/tools/arduino_eeprom.cpp @@ -0,0 +1,146 @@ +#include "TextSerialCom.h" +#include "NTCSensor.h" +#include "Logger.h" +#include <iostream> +#include <getopt.h> +#include <chrono> +#include <thread> +#include <sstream> +#include <iomanip> +#include <cstdint> +#include <string> +#include <memory> + +// Value to set eeprom to +uint8_t val=0; +// The address of the device port connected to the Arduino +std::string device=""; + +void usage(char *argv[]) +{ + std::cerr << "" << std::endl; + std::cerr << "Usage: " << argv[0] << " [options]" << std::endl; + std::cerr << "List of possible COMMAND:" << std::endl; + std::cerr << " write -- A V Write eeprom address A to value V" << std::endl; + std::cerr << " read -- A Read eeprom value for address A" << std::endl; + std::cerr << "List of options:" << std::endl; + std::cerr << " -a, --port ADDR Set the address of the port connected to the Arduino" << std::endl; + std::cerr << " -d, --debug Enable more verbose printout, use multiple for increased debug level" << std::endl; + std::cerr << " -h, --help List the commands and options available" << std::endl; + std::cerr << "" << std::endl; +} + + +int main(int argc, char* argv[]) +{ + //Parse command-line + if (argc < 1) + { + usage(argv); + return 1; + } + + int c; + while (true) { + int option_index = 0; + static struct option long_options[] = { + {"port", required_argument, 0, 'a' }, + {"debug", no_argument , 0, 'd' }, + {"help", no_argument , 0, 'h' }, + {0, 0, 0, 0 } + }; + + c = getopt_long(argc, argv, "a:dh", long_options, &option_index); + if (c == -1) break; + switch (c) { + case 'a': + device = optarg; + break; + case 'd': + logIt::incrDebug(); + break; + case 'h': + usage(argv); + return 1; + default: + std::cerr << "Invalid option '" << c << "' supplied. Aborting." << std::endl; + std::cerr << std::endl; + usage(argv); + return 1; + } + } + + if(device=="") { + std::cerr << "No device port specified for the Arduino. Aborting." <<std::endl; + usage(argv); + return 1; + } + + logger(logDEBUG) << "Device port: " << device; + + std::string command; + std::vector<std::string> params; + if (optind < argc) + { + command = argv[optind++]; + std::transform(command.begin(), command.end(), command.begin(), ::tolower); + while (optind < argc) + { + std::string p(argv[optind++]); + std::transform(p.begin(), p.end(), p.begin(), ::tolower); + params.push_back(p); + } + } + else + { + std::cerr << "Required command argument missing." << std::endl; + std::cerr << std::endl; + usage(argv); + return 1; + } + + std::shared_ptr<TextSerialCom> com = std::make_shared<TextSerialCom>(device,B9600); + com->setTermination("\r\n"); + com->setTimeout(20); + com->init(); + + logger(logDEBUG) << "Sending commend to arduino."; + // interpret command + + if (command == "write") + { + if (params.size() != 2) + { + logger(logERROR) << "Invalid number of parameters to set-eeprom command."; + logger(logERROR) << ""; + usage(argv); + return 1; + } + + std::string cmd="EEPROM WRITE "; + cmd.append(params[0]); + cmd.append(" "); + cmd.append(params[1]); + com->send(cmd); + + } + if (command == "read") + { + if (params.size() != 1) + { + logger(logERROR) << "Invalid number of parameters to get-eeprom command."; + logger(logERROR) << ""; + usage(argv); + return 1; + } + + std::string cmd="EEPROM READ "; + cmd.append(params[0]); + com->send(cmd); + std::string response = com->receive(); + + std::cout << response << std::endl; + } + + return 0; +}