Clean structure

This commit is contained in:
Глеб Луговцов 2022-11-01 16:18:53 +03:00
parent 20d17edaf8
commit 1be63c1793
8 changed files with 138 additions and 109 deletions

View File

@ -1,7 +1,44 @@
#include "Device.hpp" #include "Device.hpp"
void Generator::update() {
std::string response = query(m_channel + ":BSWV?");
Device::Device(std::string path_name) : root_path("/dev/" + path_name)
{
std::string response = query("*IDN?");
response.erase(0, response.find(',') + 1);
response.erase(response.find(','));
name = response;
}
void Device::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 Device::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;
} }

View File

@ -1,114 +1,25 @@
#pragma once
#include<fstream> #include<fstream>
#include<iostream> #include<iostream>
#include<string> #include<string>
class Device { class Device
{
public: public:
Device(std::string path_name): root_path("/dev/" + path_name) { Device(std::string path_name);
std::string idn = query("*IDN?");
idn.erase(0, idn.find(',') + 1);
idn.erase(idn.find(','));
name = idn; std::string get_name()
{
return name;
} }
protected: protected:
void command(std::string comm) { void command(std::string comm);
std::ofstream out; std::string query(std::string comm);
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 name;
std::string root_path; std::string root_path;
}; };
class Generator: public Device {
public:
Generator(std::string path_name): Device(path_name), m_channel("C1") { }
void set_channel(size_t channel) {
if (channel == 1 || channel == 2) m_channel = "C" + std::to_string(channel);
else std::cerr << "Invalid channel" << std::endl;
}
void buzz() {
command("BUZZ ON");
}
void set_waveform(std::string waveform) {
m_waveform = waveform;
command(m_channel + ":BSWV WVTP," + waveform);
}
void set_frequency(double frequency) {
m_frequency = frequency;
command(m_channel + ":BSWV FRQ," + std::to_string(frequency));
}
void set_period(double period) {
command(m_channel + ":BSWV PERI," + std::to_string(period));
}
void set_amplitude(double amplitude) {
command(m_channel + ":BSWV AMP," + std::to_string(amplitude));
}
void set_offset(double offset) {
command(m_channel + ":BSWV OFST," + std::to_string(offset));
}
void set_phase(double phase) {
command(m_channel + ":BSWV PHSE," + std::to_string(phase));
}
void update();
private:
std::string m_channel;
std::string m_waveform;
double m_frequency;
double m_period;
double m_amplitude;
double m_offset;
double m_phase;
};
class Oscilloscope: public Device {
public:
Oscilloscope(std::string path_name): Device(path_name) { }
};

2
project/Generator.cpp Normal file
View File

@ -0,0 +1,2 @@
#include "Generator.hpp"

54
project/Generator.hpp Normal file
View File

@ -0,0 +1,54 @@
#pragma once
#include "Device.hpp"
class Generator : public Device
{
public:
Generator(std::string path_name) : Device(path_name), m_channel("C1") {}
void set_channel(size_t channel)
{
if (channel == 1 || channel == 2)
m_channel = "C" + std::to_string(channel);
else
std::cerr << "Invalid channel" << std::endl;
}
void buzz()
{
command("BUZZ ON");
}
void set_waveform(std::string waveform)
{
command(m_channel + ":BSWV WVTP," + waveform);
}
void set_frequency(double frequency)
{
command(m_channel + ":BSWV FRQ," + std::to_string(frequency));
}
void set_period(double period)
{
command(m_channel + ":BSWV PERI," + std::to_string(period));
}
void set_amplitude(double amplitude)
{
command(m_channel + ":BSWV AMP," + std::to_string(amplitude));
}
void set_offset(double offset)
{
command(m_channel + ":BSWV OFST," + std::to_string(offset));
}
void set_phase(double phase)
{
command(m_channel + ":BSWV PHSE," + std::to_string(phase));
}
private:
std::string m_channel;
};

View File

@ -1,10 +1,23 @@
all: main CC=g++
CFLAGS=-c
EXECUTABLE=start
main: main.o all: $(EXECUTABLE)
g++ main.o -o main
$(EXECUTABLE): main.o Device.o Generator.o Oscilloscope.o
$(CC) main.o Device.o Generator.o Oscilloscope.o -o main
main.o: main.cpp main.o: main.cpp
g++ -c main.cpp -o main.o $(CC) $(CFLAGS) main.cpp
Device.o: Device.cpp
$(CC) $(CFLAGS) Device.cpp
Generator.o: Generator.cpp
$(CC) $(CFLAGS) Generator.cpp
Oscilloscope.o: Oscilloscope.cpp
$(CC) $(CFLAGS) Oscilloscope.cpp
clean: clean:
rm -f main rm -f main

1
project/Oscilloscope.cpp Normal file
View File

@ -0,0 +1 @@
#include "Oscilloscope.hpp"

8
project/Oscilloscope.hpp Normal file
View File

@ -0,0 +1,8 @@
#pragma once
#include "Device.hpp"
class Oscilloscope : public Device
{
public:
Oscilloscope(std::string path_name) : Device(path_name) {}
};

View File

@ -1,4 +1,5 @@
#include "Device.hpp" #include "Generator.hpp"
#include "Oscilloscope.hpp"
#include <vector> #include <vector>
#include <unistd.h> #include <unistd.h>
@ -13,8 +14,10 @@ int main() {
auto path = "usbtmc" + std::to_string(i); auto path = "usbtmc" + std::to_string(i);
Device device(path); Device device(path);
if (device.name == "AKIP-3409-4") generator_path = path; if (device.get_name() == "AKIP-3409-4")
else if (device.name == "AKIP-4131/1") oscilloscope_path = path; generator_path = path;
else if (device.get_name() == "AKIP-4131/1")
oscilloscope_path = path;
else { else {
std::cerr << "Devices not available!" << std::endl; std::cerr << "Devices not available!" << std::endl;
return EXIT_FAILURE; return EXIT_FAILURE;