status display tested

This commit is contained in:
Ryan Tang 2023-12-15 15:38:22 -05:00
parent 07be9e3a29
commit 2ccaff73e5
5 changed files with 175 additions and 24 deletions

View File

@ -14,8 +14,8 @@ copy the listen2P1AM.service to /etc/systemd/system/
run
sudo systemctl enable listen2P1AM.service
sudo systemctl start listen2P1AM.service
`sudo systemctl enable listen2P1AM.service`
`sudo systemctl start listen2P1AM.service`
## interlock database at fsunuc.physics.fsu.edu

BIN
statusDisplay/BG.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 663 KiB

View File

@ -1,7 +1,9 @@
The Status Display on Raspberry Pi use the SDL C++ library
# Intoduction
sudo apt-get install libsdl2-dev libsdl2-image-dev libsdl2-ttf-dev
The Status Display on Raspberry Pi use the SDL (Simple DirectMedia Layer) C++ library
To Compile
`sudo apt-get install libsdl2-dev libsdl2-image-dev libsdl2-ttf-dev`
g++ -o display display.cpp -lSDL2 -lSDL2_image -lSDL2_ttf
# To Compile
`make`

View File

@ -3,34 +3,149 @@
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <string>
#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
SDL_Window* window = SDL_CreateWindow("Image with Text Overlay",
window = SDL_CreateWindow("Tritium Source Status",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
1577, 695, 0);
// Create a renderer
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
// Load an image
SDL_Surface* imageSurface = IMG_Load("image.png");
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, imageSurface);
SDL_FreeSurface(imageSurface);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
// Font initialization for rendering text
TTF_Init();
TTF_Font* font = TTF_OpenFont("/usr/share/fonts/truetype/freefont/FreeMono.ttf", 24);
SDL_Color textColor = { 255, 255, 255 }; // White color
font = TTF_OpenFont("/usr/share/fonts/truetype/freefont/FreeSerifBold.ttf", 24);
SDL_Rect imageRect = { 0, 0, 1577, 695 }; // Image position and size
// 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;
int count = 0;
while (running) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
@ -38,27 +153,61 @@ int main() {
}
}
if( count == 0 ) readStatus();
count ++;
if( count >= 5 ) count = 0;
// Clear the renderer
SDL_RenderClear(renderer);
// Render the image
SDL_RenderCopy(renderer, texture, NULL, &imageRect);
// Render text
std::string textToRender = "Dynamic Number: 123"; // Replace with your dynamic number
SDL_Surface* textSurface = TTF_RenderText_Solid(font, textToRender.c_str(), textColor);
{// 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);
// Convert time to a string
std::string timeString = std::ctime(&now_c);
timeString.erase(timeString.length() - 1); //remove the '\n'
SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
textDisplay(timeString, 10, 10, blackColor);
}
SDL_Rect textRect = { 50, 50, textSurface->w, textSurface->h }; // Position of text
SDL_RenderCopy(renderer, textTexture, NULL, &textRect);
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
SDL_FreeSurface(textSurface);
SDL_DestroyTexture(textTexture);
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;
case 2: textDisplay( "Trouble-1", 10, 40, redColor); break;
case 3: textDisplay( "Trouble-2", 10, 40, redColor); break;
case 4: textDisplay( "Trouble-3", 10, 40, redColor); break;
}
textDisplay( "Tritium Sensor : " + std::to_string(tritiumReading) + " mCr", 720, 200, blueColor);
textDisplay( "Vaccum : " + std::to_string(vaccumReading) + " Torr", 630, 20, blueColor);
textDisplay( "SubPump : " + std::to_string(subPumpReading) + " Amp", 1020, 70, blueColor);
textDisplay("update every 5 sec.", 1200, 670, blackColor);
// Update the screen
SDL_RenderPresent(renderer);
std::this_thread::sleep_for(std::chrono::seconds(1));
}
// Clean up

Binary file not shown.

Before

Width:  |  Height:  |  Size: 194 KiB