#include "Arduino.h"
#include "ArduinoUniqueID.h"
#include "Vrekrer_scpi_parser.h"
/// These are found and replaced by the compiler.
#define CompileDate __DATE__
#define CompileTime __TIME__
/// These are for returning copyright information, and storing this information in the code.
const String Copyright = "Philip McGaw"; ///< Copyright company / person's name.
String Product = "SCPI Demo"; ///< Project name. (Updated when I make a release).
int Hardware_Rev = -1; ///< Placeholder - Takes the value of the VERSION_PINS.
String Serial_Number = "-1"; ///< Placeholder - This is the serial number of the microprocessor.
const int Software_Version = 1; ///< Firmware version. (Updated when I make a release).
String CompileYear = CompileDate;
SCPI_Parser my_instrument; ///< Brings up the SCPI Parser as my_instrument
/**
Sets up the Tree for the SCPI commands
Four Default ones are:
*IDN? Identify Outputs an identifying string. The response will show the following information:
<company name>, <model number>, <serial number>, <firmware revision>
*OPT? Options Returns a comma-separated list of all the instrument options currently installed on the equipment.
*RST Reset Resets most functions to factory-defined conditions.
*TST? Self-Test Initiates the internal self-test and returns one of the following results:
0 - This shows that all tests passed.
1 - This shows that one or more tests failed.
*/
void scpi_tree() {
my_instrument.RegisterCommand(F("*IDN?"), &Identify);
my_instrument.RegisterCommand(F("*OPT?"), &Options);
my_instrument.RegisterCommand(F("*RST"), &Reset);
my_instrument.RegisterCommand(F("*TST?"), &SelfTest);
}
void setup() {
identification(); ///< Generates the response for the *IDN? command.
scpi_tree(); ///< registers the SCPI commands
Serial.begin(9600);
Serial.println(" "); ///< Causes WOKWI to open the serial console.
}
void loop() {
my_instrument.ProcessInput(Serial, "\n"); ///< Checks the serial buffer for SCPI actions.
}
void Identify(SCPI_C commands, SCPI_P parameters, Stream& interface) {
//*IDN? Suggested return string should be in the following format:
// "<vendor>,<model>,<serial number>,<firmware>" however, we supply more information back…
interface.println((String) "© " + Copyright + " " + CompileYear + " - " + Product);
interface.println((String) "Software Version: \t\t" + Software_Version + " (" + CompileDate + " " + CompileTime + ")");
interface.println((String) "Hardware Version: \t\t" + Hardware_Rev); ///< on WOKWI we will get a random number if the hardware revision jumpers are not set during first start of the application.
interface.println((String) "Serial Number: \t\t\t" + Serial_Number);
}
void Options(SCPI_C commands, SCPI_P parameters, Stream& interface) {
//*OPT? - This is hardware implementation dependent.
interface.println("OPT - not implimented");
}
void Reset(SCPI_C commands, SCPI_P parameters, Stream& interface) {
//*RST - This is hardware implementation dependent - Normaly uses one Digital output to toggle the reset pin.
interface.println("RST - not implimented");
}
void SelfTest(SCPI_C commands, SCPI_P parameters, Stream& interface) {
//*TST? - This is hardware implementation dependent.
interface.println("0, TST - not implimented");
}
/**
* Works out the answerd to the IDN SCPI command.
*
* This code is run once during setup.
*/
void identification() {
/**
Resistor Values:
Version Ratio VDD (dec) Top Resistor Value Bottom Resistor Value
0 1/16 (0.0625) 4k7 DNF
1 3/16 (0.1875) 910R 3k9
2 5/16 (0.3125) 1k5 3k3
3 7/16 (0.4375) 2k4 2k4 & 430R
4 9/16 (0.5625) 2k4 & 430R 2K4
5 11/16 (0.6875) 3k3 1k5
6 13/16 (0.8125) 3k9 910R
7 15/16 (0.9357) DNF 4k7
*/
int valA = analogRead(PIN_A0); // Analog pin A1
int valB = analogRead(PIN_A1); // Analog pin A0
valA = map(valA, 0, 1023, 7, 0); // Maps the value from analouge pin to 0-7.
valB = map(valB, 0, 1023, 7, 0);
if (valB != 0)
valB = valB + 7; // adds 7 to the MSB if it is not zero
Hardware_Rev = valA; + valB; // gives us a hardware version between 0 and 21.
Serial_Number = ""; ///< empties the string removing the -1 value we filled it with,
for (size_t i = 0; i < UniqueIDsize; i++) {
Serial_Number += String(UniqueID[i], HEX);
}
Serial_Number.toUpperCase();
CompileYear = CompileYear.substring(7, 11);
}