Multi-SNICS_Interlock/statusDisplay/display.cpp

222 lines
6.5 KiB
C++
Raw Normal View History

#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <string>
2023-12-15 15:38:22 -05:00
#include <thread>
#include <chrono>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
SDL_Window* window = nullptr;
SDL_Renderer* renderer = nullptr;
TTF_Font* font = nullptr;
SDL_Color blackColor = { 0, 0, 0, 255};
SDL_Color redColor = { 255, 0, 0, 255};
SDL_Color greenColor = { 0, 200, 0, 255};
SDL_Color blueColor = { 0, 0, 255, 255};
SDL_Texture* textTexture = nullptr;
void textDisplay(std::string text, int posX, int posY, SDL_Color color){
SDL_Surface* textSurface = TTF_RenderText_Solid(font, text.c_str(), color);
textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
SDL_Rect textRect = { posX, posY, textSurface->w, textSurface->h };
SDL_RenderCopy(renderer, textTexture, NULL, &textRect);
SDL_FreeSurface(textSurface);
SDL_DestroyTexture(textTexture);
}
int statusReading = 0;
double tritiumReading = 0;
double vaccumReading = 0;
double subPumpReading = 0;
bool preAccel = true;
bool valve1 = true;
bool valve2 = true;
bool hv = true;
bool boiler = true ;
bool ionizer = true ;
void readStatus(){
std::ifstream file("data.txt"); // Replace "your_file.txt" with your actual file path
if (!file.is_open()) {
std::cout << "Unable to open file" << std::endl;
return;
}
std::string line;
while (std::getline(file, line)) {
std::istringstream iss(line);
std::string key;
double value;
if (std::getline(iss, key, '=')) {
// Extract the key
std::string valueStr;
if (std::getline(iss, valueStr)) {
// Convert the value string to double
try {
value = std::stod(valueStr);
//std::cout << "Key: " << key << ", Value: " << value << std::endl;
// Use 'value' as needed
if( key == "State value" ) statusReading = value;
if( key == "Tritium value" ) tritiumReading = value;
if( key == "Vaccum value" ) vaccumReading = value;
if( key == "SubPump value" ) subPumpReading = value;
} catch (const std::invalid_argument& e) {
std::cout << "Invalid value format for key: " << key << std::endl;
}
}
}
}
file.close();
if( statusReading == 0 || statusReading == 1 ){ // startup or normal
preAccel = true;
valve1 = true;
valve2 = true;
hv = true;
boiler = true;
ionizer = true;
}
if( statusReading == 2 ){ // toruble1
preAccel = false;
hv = false;
}
if( statusReading == 3 ){ // toruble2
preAccel = false;
valve1 = false;
valve2 = false;
hv = false;
ionizer = false;
}
if( statusReading == 4 ){ // toruble3
preAccel = false;
valve1 = false;
valve2 = false;
hv = false;
boiler = false;
ionizer = false;
}
}
int main() {
SDL_Init(SDL_INIT_VIDEO);
// Create a window
2023-12-15 15:38:22 -05:00
window = SDL_CreateWindow("Tritium Source Status",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
2023-12-14 17:53:47 -05:00
1577, 695, 0);
// Create a renderer
2023-12-15 15:38:22 -05:00
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
// Font initialization for rendering text
TTF_Init();
2023-12-15 15:38:22 -05:00
font = TTF_OpenFont("/usr/share/fonts/truetype/freefont/FreeSerifBold.ttf", 24);
2023-12-15 15:38:22 -05:00
// Load an image
SDL_Surface* imageSurface = IMG_Load("BG.png");
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, imageSurface);
SDL_FreeSurface(imageSurface);
SDL_Rect imageRect = { 0, 0, 1577, 695 }; // Image position and size
SDL_Event event;
bool running = true;
2023-12-15 15:38:22 -05:00
int count = 0;
while (running) {
2023-12-14 17:53:47 -05:00
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
running = false;
}
}
2023-12-15 15:38:22 -05:00
if( count == 0 ) readStatus();
count ++;
if( count >= 5 ) count = 0;
2023-12-14 17:53:47 -05:00
// Clear the renderer
SDL_RenderClear(renderer);
2023-12-14 17:53:47 -05:00
// Render the image
SDL_RenderCopy(renderer, texture, NULL, &imageRect);
2023-12-15 15:38:22 -05:00
{// Get current time
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
std::time_t now_c = std::chrono::system_clock::to_time_t(now);
2023-12-15 15:38:22 -05:00
// Convert time to a string
std::string timeString = std::ctime(&now_c);
timeString.erase(timeString.length() - 1); //remove the '\n'
2023-12-15 15:38:22 -05:00
textDisplay(timeString, 10, 10, blackColor);
}
2023-12-15 15:38:22 -05:00
textDisplay( valve1 ? "Open" : "Close", 570, 455, valve1 ? greenColor : redColor); // GateValve-1
textDisplay( valve2 ? "Open" : "Close", 1270, 455, valve2 ? greenColor : redColor); // GateValve-2
textDisplay( "Pre-Accel. supply : ", 750, 455, blackColor); // pre-accel. supply text
textDisplay( preAccel ? "On" : "Off", 950, 455, preAccel ? greenColor : redColor); // pre-accel. supply text
2023-12-14 17:53:47 -05:00
2023-12-15 15:38:22 -05:00
textDisplay( "HV Supply : ", 30, 200, blackColor);
textDisplay( hv ? "On" : "Off", 230, 200, hv ? greenColor : redColor);
textDisplay( "Boiler Supply : ", 30, 240, blackColor);
textDisplay( boiler ? "On" : "Off", 230, 240, boiler ? greenColor : redColor);
textDisplay( "Ionizer Supply : ", 30, 280, blackColor);
textDisplay( ionizer ? "On" : "Off", 230, 280, ionizer ? greenColor : redColor);
switch (statusReading){
case 0: textDisplay( "Start-Up", 10, 40, blueColor); break;
case 1: textDisplay( "Normal", 10, 40, greenColor); break;
2024-01-30 12:56:30 -05:00
case 2: textDisplay( "Cage Door (Tripped-1)", 10, 40, redColor); break;
case 3: textDisplay( "Vacuum (Tripped-2)", 10, 40, redColor); break;
case 4: textDisplay( "Source (Tripped-3)", 10, 40, redColor); break;
2023-12-15 15:38:22 -05:00
}
textDisplay( "Tritium Sensor : " + std::to_string(tritiumReading) + " mCr", 720, 200, blueColor);
2024-01-30 12:56:30 -05:00
textDisplay( "Vaccum : " + std::to_string(vaccumReading) + "x1e-6 Torr", 630, 20, blueColor);
textDisplay( "SubPump : " + std::to_string(subPumpReading*1000) + " mA", 300, 40, blueColor);
2023-12-15 15:38:22 -05:00
textDisplay("update every 5 sec.", 1200, 670, blackColor);
2023-12-14 17:53:47 -05:00
// Update the screen
SDL_RenderPresent(renderer);
2023-12-15 15:38:22 -05:00
std::this_thread::sleep_for(std::chrono::seconds(1));
}
// Clean up
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_Quit();
SDL_Quit();
return 0;
2024-01-30 12:56:30 -05:00
}