Commit 9dde5881 authored by fmg005's avatar fmg005

Initial commit

parents
#include "CLA_Logger.h"
CLA::LOGGER::LOGGER(const string& filename): m_filename(move(filename)) {
m_logstream.open(m_filename);
}
string CLA::LOGGER::current_time(){
time_t now = time(0);
struct tm* timeinfo;
char buffer[50];
timeinfo = localtime(&now);
strftime(buffer, 50, "%Y-%M-%d %X-%Z", timeinfo);
return buffer;
}
string CLA::LOGGER::set_level(LEVEL level) {
switch(level) {
case DEBUG:
level_name = "DEBUG";
break;
case INFO:
level_name = "INFO";
break;
case WARNING:
level_name = "WARNING";
break;
case ERROR:
level_name = "ERROR";
break;
case FATAL:
level_name = "FATAL";
break;
}
return level_name;
}
CLA::LOGGER::~LOGGER() {
m_logstream.close();
}
cmake_minimum_required(VERSION 3.9.4)
project(prosim_driver)
find_package(serial REQUIRED)
if(serial_FOUND)
message("Serial Found")
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++11")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread")
set( my_srcs
HardwareSimulator.cpp
CLA_Logger.cpp
main.cpp
)
add_executable(prosim_test ${my_srcs})
target_include_directories(prosim_test PRIVATE
${serial_INCLUDE_DIRS}
${CMAKE_CURRENT_SOURCE_DIR}/include)
target_link_libraries(prosim_test optimized ${serial_LIBRARIES})
#include "HardwareSimulator.h"
HardwareSimulator::HardwareSimulator(serial::Serial*
my_serial, CLA::LOGGER* logger):m_serial(my_serial), m_logger(logger) {
}
bool HardwareSimulator::isOpen() {
return m_serial->isOpen();
}
/* HR range: 10-360 */
void HardwareSimulator::SetHeartRate(int rate) {
if (rate >= 10 && rate <= 360) {
SendCommand("NSRA=0"+std::to_string(rate));
std::cout <<"Heart rate set to " << rate << " bpm\n";
}
else {
std::cout << rate <<" not within acceptable range 10-360\n";
}
}
/* RR range: 10-150 */
void HardwareSimulator::SetRespRate(int rate) {
if (rate >= 10 && rate <= 150) {
// size_t n = m_serial->write("RESPRATE=0"+std::to_string(rate)+"\r\n");
std::cout <<"Respiratory rate set to "<< rate <<" brpm\n";
}
else {
std::cout << rate <<" not within acceptable range 10-150\n";
}
}
/* O2 range: 0-100 */
void HardwareSimulator::SetOxygenSat(int rate) {
if ( GetMode() != "REMOTE") {
cout << " Command Not sent\n";
m_logger->error("Oxygen Saturation can't be changed in this mode");
}
else {
int i_rate = rate - 1;
std::string s_rate = std::to_string(i_rate);
if (i_rate >= 0 && i_rate <= 100) {
if (rate < 10) {
s_rate = "0" + s_rate; //pad an extra leading zero
}
SendCommand("SAT=0"+s_rate);
std::cout <<"Oxygen saturation set to "<< rate <<"\n";
m_logger->info("Oxygen saturation set to " + to_string(rate));
}
else {
std::cout << rate <<" Not within acceptable range 0-100\n";
m_logger->error("Oxygen saturation value: "+to_string(rate)+
",not within acceptable range 0-100");
}
}
}
// ToBeDefined
void HardwareSimulator::SetBloodPressure(int rate) {
}
void HardwareSimulator::SetRemoteMode() {
SendCommand("REMOTE");
std::cout <<"Device switched to REMOTE mode\n";
m_logger->info("Device switched to REMOTE mode");
}
void HardwareSimulator::SetLocalMode() {
SendCommand("LOCAL");
std::cout <<"Device switched to LOCAL mode\n";
m_logger->info("Device switched to LOCAL mode");
}
bool HardwareSimulator::isRemoteMode() {
SendCommand("QMODE");
string data = GetResponse();
if (data.find("RMAIN") != std::string::npos) {
return true;
}
else {
return false;
}
}
size_t HardwareSimulator::SendCommand(string command) {
size_t bytes_sent = m_serial->write(command+"\r\n");
return bytes_sent;
}
string HardwareSimulator::GetResponse() {
size_t bytes_in_buffer = m_serial->available();
std::string response = m_serial->read(bytes_in_buffer);
return response;
}
string HardwareSimulator::GetMode() {
if(isRemoteMode()){
return "REMOTE";
}
else {
return "LOCAL";
}
}
void HardwareSimulator::Clear() {
m_serial = nullptr;
m_logger = nullptr;
}
HardwareSimulator::~HardwareSimulator() {
SetLocalMode();// Switch back device to LOCAL mode when done
Clear();
}
#ifndef CLALOGGER_H
#define CLALOGGER_H
#include <string>
#include <time.h> /* time_t, time, struct tm, localtime, strftime*/
#include <fstream>
#include <utility>
#include <sstream>
using namespace std;
namespace CLA {
class LOGGER {
private:
string level_name;
const string m_filename;
ofstream m_logstream;
enum LEVEL {DEBUG, INFO, WARNING, ERROR, FATAL};
string current_time();
string set_level(LEVEL);
public:
LOGGER(const string&);
~LOGGER();
template <typename T>
void debug(const T& t) {
ostringstream ss;
ss << t;
m_logstream<<current_time()<<","<<set_level(LOGGER::DEBUG)<<","+ss.str()+"\n";
ss.str("");
ss.clear();
}
template <typename T>
void info(const T& t) {
ostringstream ss;
ss << t;
m_logstream<<current_time()<<","<<set_level(LOGGER::INFO)<<","+ss.str()+"\n";
ss.str("");
ss.clear();
}
template <typename T>
void warning(const T& t) {
ostringstream ss;
ss << t;
m_logstream<<current_time()<<","<<set_level(LOGGER::WARNING)<<","+ss.str()+"\n";
ss.str("");
ss.clear();
}
template <typename T>
void error(const T& t) {
ostringstream ss;
ss << t;
m_logstream<<current_time()<<","<<set_level(LOGGER::ERROR)<<","+ss.str()+"\n";
ss.str("");
ss.clear();
}
template <typename T>
void fatal(const T& t) {
ostringstream ss;
ss << t;
m_logstream<<current_time()<<","<<set_level(LOGGER::FATAL)<<","+ss.str()+"\n";
ss.str("");
ss.clear();
}
};
}
#endif
#ifndef HARDWARESIMULATOR_H
#define HARDWARESIMULATOR_H
#include <string>
#include <iostream>
#include <cstdio>
#include <unistd.h>
#include "serial/serial.h"
#include "CLA_Logger.h"
using namespace std;
class HardwareSimulator {
private:
serial::Serial* m_serial;
bool isRemoteMode();
void Clear();
size_t SendCommand(string);
string GetResponse();
CLA::LOGGER* m_logger;
public:
HardwareSimulator(serial::Serial*, CLA::LOGGER*);
~HardwareSimulator();
bool isOpen();
void SetHeartRate(int);
void SetRespRate(int);
void SetOxygenSat(int);
void SetBloodPressure(int);
void SetRemoteMode();
void SetLocalMode();
string GetMode();
};
#endif
#include <string>
#include <iostream>
#include <cstdio>
#include <unistd.h>
#include "serial/serial.h"
#include "HardwareSimulator.h"
#include "CLA_Logger.h"
using namespace std;
int main() {
/*serial instance arguments*/
std::string port = "/dev/ttySB0"; // arbitrary port
uint32_t baudrate = 115200;
serial::Timeout timeout = serial::Timeout();
serial::bytesize_t bytesize = serial::eightbits;
serial::parity_t parity = serial::parity_none;
serial::stopbits_t stopbits = serial::stopbits_one;
serial::flowcontrol_t flowcontrol = serial::flowcontrol_hardware;
/*end serial instance arguments*/
CLA::LOGGER logger("mylog.txt");
serial::Serial myserial(port, baudrate, timeout, bytesize, parity, stopbits,
flowcontrol);
HardwareSimulator simulator(&myserial, &logger);
/*
Do stuff with simulator
*/
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment