Skip to content
Snippets Groups Projects
Commit ee3f4d79 authored by Elodie Deborah Resseguie's avatar Elodie Deborah Resseguie Committed by Elisabetta Pianori
Browse files

read and write to eeprom

parent 96cecfb9
Branches
Tags
6 merge requests!308Bring main and devel back in sync,!300playing with fixing divergence between devel and main,!287Updates for Julabo Chiller,!285Julabo Chiller Added with Fixed Pipeline Compatibility,!269Merge devel into main: largest change is code formatting checker and enforcement in CI,!176read and write to eeprom
#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)
......
......@@ -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)
......
#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;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment