Start working on generator class

This commit is contained in:
Глеб Луговцов 2022-10-31 14:07:36 +03:00
parent e718654bd7
commit 95136cad44
9 changed files with 149 additions and 52 deletions

3
project/Device.cpp Normal file
View File

@ -0,0 +1,3 @@
#include "Device.hpp"

94
project/Device.hpp Normal file
View File

@ -0,0 +1,94 @@
#include<fstream>
#include<iostream>
#include<string>
class Device {
public:
Device(std::string path_name): root_path("/dev/" + path_name) {
std::string idn = query("*IDN?");
idn.erase(0, idn.find(',') + 1);
idn.erase(idn.find(','));
name = idn;
}
protected:
void command(std::string comm) {
std::ofstream out;
out.open(root_path);
if(!out.is_open())
std::cerr << "**ERROR** file is not open to output." << std::endl;
else {
out << comm;
out.close();
}
}
std::string query(std::string comm) {
command(comm);
std::string buffer;
std::ifstream in;
in.open(root_path);
if(!in.is_open())
std::cerr << "**ERROR** file is not open to input." << std::endl;
else {
std::getline(in, buffer);
in.close();
}
return buffer;
}
public:
std::string name;
std::string root_path;
};
class Generator: public Device {
public:
Generator(std::string path_name): Device(path_name), channel("C1") { }
void buzz() {
command("BUZZ ON");
}
void basic_wave(
size_t channel,
std::string type,
double frequency,
double period,
double amplitude,
double offset,
double phase
);
void set_channel(size_t ch) {
if (ch == 1 || ch == 2) channel = "C" + std::to_string(ch);
else std::cerr << "Invalid channel" << std::endl;
}
void set_waveform(std::string type) {
command(channel + ":BSWV WVTP," + type);
}
private:
std::string channel;
};
class Oscilloscope: public Device {
public:
Oscilloscope(std::string path_name): Device(path_name) { }
};

13
project/Makefile Normal file
View File

@ -0,0 +1,13 @@
all: main
main: main.o
g++ main.o -o main
main.o: main.cpp
g++ -c main.cpp -o main.o
clean:
rm -f main
rm -f *.o
.PNONY: all clean

Binary file not shown.

View File

@ -1,52 +0,0 @@
#include<fstream>
#include<iostream>
#include<string>
class Device
{
public:
Device(std::string name): root_path("/dev/" + name) { }
void send_command(std::string command)
{
std::ofstream out;
out.open(root_path);
if(!out.is_open()) {
std::cerr << "**ERROR** file is not open to output." << std::endl;
} else {
out << command;
out.close();
}
}
std::string accept_command()
{
std::ifstream in;
in.open(root_path);
if(!in.is_open()) {
std::cerr << "**ERROR** file is not open to input." << std::endl;
} else {
std::getline(in, buffer);
in.close();
}
return buffer;
}
private:
std::string root_path;
std::string buffer;
};
int main()
{
Device generator("usbtmc0");
generator.send_command("*IDN?");
std::cout << generator.accept_command() << std::endl;
return 0;
}

BIN
project/main Executable file

Binary file not shown.

39
project/main.cpp Normal file
View File

@ -0,0 +1,39 @@
#include "Device.hpp"
#include <vector>
#include <unistd.h>
int main() {
// initialize generator and oscilloscope
std::string generator_path;
std::string oscilloscope_path;
for (size_t i = 0; i < 2; i++) {
auto path = "usbtmc" + std::to_string(i);
Device device(path);
if (device.name == "AKIP-3409-4") generator_path = path;
else if (device.name == "AKIP-4131/1") oscilloscope_path = path;
else {
std::cerr << "Devices not available!" << std::endl;
return EXIT_FAILURE;
}
}
Generator generator(generator_path);
Oscilloscope oscilloscope(oscilloscope_path);
// work with devices
//generator.buzz();
std::string waveform;
std::cout << "Input waveform (SINE, SQUARE, RAMP, PULSE, NOISE, ARB, DC, PRBS, IQ):" << std::endl;
std::cin >> waveform;
generator.set_waveform(waveform);
return EXIT_SUCCESS;
}

BIN
project/main.o Normal file

Binary file not shown.