From 1be63c17936bd11e10886e535d24198fdcffe178 Mon Sep 17 00:00:00 2001 From: Lugovtsov Gleb Date: Tue, 1 Nov 2022 16:18:53 +0300 Subject: [PATCH] Clean structure --- project/Device.cpp | 43 ++++++++++++++-- project/Device.hpp | 107 ++++----------------------------------- project/Generator.cpp | 2 + project/Generator.hpp | 54 ++++++++++++++++++++ project/Makefile | 23 +++++++-- project/Oscilloscope.cpp | 1 + project/Oscilloscope.hpp | 8 +++ project/main.cpp | 9 ++-- 8 files changed, 138 insertions(+), 109 deletions(-) create mode 100644 project/Generator.cpp create mode 100644 project/Generator.hpp create mode 100644 project/Oscilloscope.cpp create mode 100644 project/Oscilloscope.hpp diff --git a/project/Device.cpp b/project/Device.cpp index af5fd0f..d52d933 100644 --- a/project/Device.cpp +++ b/project/Device.cpp @@ -1,7 +1,44 @@ #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; } diff --git a/project/Device.hpp b/project/Device.hpp index ba20660..de86b21 100644 --- a/project/Device.hpp +++ b/project/Device.hpp @@ -1,114 +1,25 @@ +#pragma once #include #include #include -class Device { +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(',')); + Device(std::string path_name); - name = idn; + std::string get_name() + { + return name; } protected: - void command(std::string comm) { - std::ofstream out; - out.open(root_path); + void command(std::string comm); + std::string query(std::string comm); - 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), 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) { } -}; - - - - - - - diff --git a/project/Generator.cpp b/project/Generator.cpp new file mode 100644 index 0000000..18b6cf8 --- /dev/null +++ b/project/Generator.cpp @@ -0,0 +1,2 @@ +#include "Generator.hpp" + diff --git a/project/Generator.hpp b/project/Generator.hpp new file mode 100644 index 0000000..471d6b0 --- /dev/null +++ b/project/Generator.hpp @@ -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; +}; \ No newline at end of file diff --git a/project/Makefile b/project/Makefile index 5b34e72..50db55b 100644 --- a/project/Makefile +++ b/project/Makefile @@ -1,10 +1,23 @@ -all: main +CC=g++ +CFLAGS=-c +EXECUTABLE=start -main: main.o - g++ main.o -o main +all: $(EXECUTABLE) -main.o: main.cpp - g++ -c main.cpp -o main.o +$(EXECUTABLE): main.o Device.o Generator.o Oscilloscope.o + $(CC) main.o Device.o Generator.o Oscilloscope.o -o main + +main.o: main.cpp + $(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: rm -f main diff --git a/project/Oscilloscope.cpp b/project/Oscilloscope.cpp new file mode 100644 index 0000000..9ee135e --- /dev/null +++ b/project/Oscilloscope.cpp @@ -0,0 +1 @@ +#include "Oscilloscope.hpp" \ No newline at end of file diff --git a/project/Oscilloscope.hpp b/project/Oscilloscope.hpp new file mode 100644 index 0000000..0ee2404 --- /dev/null +++ b/project/Oscilloscope.hpp @@ -0,0 +1,8 @@ +#pragma once +#include "Device.hpp" + +class Oscilloscope : public Device +{ +public: + Oscilloscope(std::string path_name) : Device(path_name) {} +}; \ No newline at end of file diff --git a/project/main.cpp b/project/main.cpp index 4fc2754..9a11227 100644 --- a/project/main.cpp +++ b/project/main.cpp @@ -1,4 +1,5 @@ -#include "Device.hpp" +#include "Generator.hpp" +#include "Oscilloscope.hpp" #include #include @@ -13,8 +14,10 @@ int main() { 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; + if (device.get_name() == "AKIP-3409-4") + generator_path = path; + else if (device.get_name() == "AKIP-4131/1") + oscilloscope_path = path; else { std::cerr << "Devices not available!" << std::endl; return EXIT_FAILURE;