Compare commits

..

6 Commits

Author SHA1 Message Date
1558beaaf2 fix grap position 2024-11-28 19:41:52 +01:00
1b35c285df Fix text 2024-11-28 19:35:03 +01:00
f39713d97e add manual refresh 2024-11-28 19:16:45 +01:00
816ae120a2 Fix positionsing to fit 3 digits 2024-11-28 18:37:48 +01:00
fb8788420d Add graph 2024-11-28 18:36:49 +01:00
7941d32984 Add displays 2024-11-27 19:22:06 +01:00
2 changed files with 98 additions and 38 deletions

View File

@@ -1,5 +1,8 @@
extern Adafruit_SSD1306 display; // Reference to the OLED display object
extern unsigned long lastRefresh;
extern const unsigned long refreshInterval;
bool is_display_off = false; // State variable to track display on/off status
bool is_sound_off = false; // State variable to track display on/off status
int buttonDebounceDelay = 50; // Debounce delay in milliseconds
int buttonLongPressDelay = 2000; // Long press threshold in milliseconds
@@ -29,7 +32,10 @@ void cubeButtonHandler() {
if (!leftBeeped) {
beep(1000); // Play beep for a short press
leftBeeped = true; // Mark as beeped to avoid repeat beeps
// Handle left short press action here
if (is_sound_off) {
is_sound_off = false; // Update display state
beep(1600); // Additional beep to confirm display is on
}
}
}
} else {
@@ -43,7 +49,7 @@ void cubeButtonHandler() {
if (!middleBeeped) {
beep(1000); // Play beep for a short press
middleBeeped = true; // Mark as beeped to avoid repeat beeps
// Handle middle short press action here
lastRefresh = millis() + refreshInterval + 1;
}
}
} else {
@@ -71,10 +77,10 @@ void cubeButtonHandler() {
// Long press detection for left button
if (leftPressed && (currentMillis - leftPressStart > buttonLongPressDelay)) {
if (!leftBeeped) {
beep(1000); // Play beep for a long press
leftBeeped = true; // Mark as beeped to avoid repeat beeps
// Handle left long press action here
if (!is_sound_off) { // Turn off the display if it's on
beep(1400); // Beep to indicate display turn-off
is_sound_off = true; // Update display state
beep(1000); // Additional beep to confirm display is off
}
}

View File

@@ -15,10 +15,20 @@ const unsigned long refreshInterval = 60000; // 60 seconds
String lastEventId = "";
String problems[10];
String problemDescriptions = "Test";
String problemDescriptions;
int problemCount = 0;
int severityCounts[5] = {0};
int severityCounts[6] = {0};
bool newProblemsDetected = false;
int totalProblems = 0; // Variable to store the total number of active problems
// History buffer for the graph
const int maxHistorySize = 60;
int problemHistory[maxHistorySize] = {0}; // Array to store history
int historyIndex = 0; // Current index for the latest value
int scrollPos = SCREEN_WIDTH; // Global variable to keep track of the scroll position
unsigned long lastScrollTime = 0; // To control the scroll speed
const int scrollDelay = 120; // Delay in milliseconds between scroll updates
void initSystems() {
pinMode(PIN_BTN_L, INPUT);
@@ -31,9 +41,10 @@ void initSystems() {
Wire.setClock(400000L); // Set I2C to Fast Mode (400 kHz)
display.ssd1306_command(SSD1306_SETCONTRAST);
display.ssd1306_command(200); // Value between 0 and 255
// Initialize the SSD1306 display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Adjust I2C address if needed
for(;;); // Don't proceed, loop forever
for (;;); // Don't proceed, loop forever
}
// Rotate the display 180 degrees
@@ -41,14 +52,12 @@ void initSystems() {
// Clear the display buffer
display.clearDisplay();
display.setTextSize(1);
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.display();
}
int totalProblems = 0; // Variable to store the total number of active problems
void fetchActiveProblems() {
WiFiClient client;
HTTPClient http;
@@ -90,6 +99,10 @@ void fetchActiveProblems() {
totalProblems = result.size(); // Set total problems count
// Store the current totalProblems in the history buffer
problemHistory[historyIndex] = totalProblems;
historyIndex = (historyIndex + 1) % maxHistorySize; // Circular buffer
int newProblemCount = 0;
for (JsonObject problem : result) {
String eventId = problem["eventid"].as<String>();
@@ -101,10 +114,6 @@ void fetchActiveProblems() {
newProblemsDetected = true;
}
if (newProblemCount < 10) {
problems[newProblemCount] = description;
}
if (severity >= 0 && severity < 5) {
severityCounts[severity]++;
}
@@ -121,47 +130,92 @@ void fetchActiveProblems() {
} else {
beep(1000);
}
} else {
display.clearDisplay();
display.setCursor(0,30);
display.print("Error querying zabbix API");
} else {
problemDescriptions="Zabbix API error";
}
http.end();
}
int scrollPos = SCREEN_WIDTH; // Global variable to keep track of the scroll position
unsigned long lastScrollTime = 0; // To control the scroll speed
const int scrollDelay = 150; // Delay in milliseconds between scroll updates
void playAlertMelody() {
int toneDuration = 60; // Each tone lasts 150ms
int pauseDuration = 30; // Pause between tones
// Play first tone
tone(PIN_BUZZER, 1500); // 1000 Hz
delay(toneDuration);
noTone(PIN_BUZZER);
delay(pauseDuration);
// Play second tone
tone(PIN_BUZZER, 1200); // 1200 Hz
delay(toneDuration);
noTone(PIN_BUZZER);
delay(pauseDuration);
// Play third tone
tone(PIN_BUZZER, 1500); // 1500 Hz
delay(toneDuration);
noTone(PIN_BUZZER);
}
void displayProblems() {
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0,0);
display.print("Total Problems: ");
display.print(totalProblems); // Display total problems
display.setCursor(0, 0);
display.printf("I:%d W:%d A:%d H:%d D:%d",
severityCounts[1],
severityCounts[2],
severityCounts[3],
severityCounts[4],
severityCounts[5]);
if (is_sound_off) {
display.setCursor(122, 0);
display.print("X");
}
display.drawLine(0, 45, 127, 45, 1); // Line under the graph
display.drawLine(0, 9, 127, 9, 1); // Line under the graph
// Draw the history graph
int graphHeight = 30; // Height of the graph
int graphTop = 14; // Top position of the graph
int graphBottom = graphTop + graphHeight;
int maxProblems = *std::max_element(problemHistory, problemHistory + maxHistorySize); // Find max for scaling
if (maxProblems == 0) maxProblems = 1; // Avoid division by zero
for (int i = 0; i < maxHistorySize - 1; i++) {
int x1 = (i * SCREEN_WIDTH) / maxHistorySize;
int x2 = ((i + 1) * SCREEN_WIDTH) / maxHistorySize;
int y1 = graphBottom - (problemHistory[(historyIndex + i) % maxHistorySize] * graphHeight) / maxProblems;
int y2 = graphBottom - (problemHistory[(historyIndex + i + 1) % maxHistorySize] * graphHeight) / maxProblems;
display.drawLine(x1, y1, x2, y2, 1); // Draw line between points
}
// Scroll text below the graph
int stateWidth = strlen(problemDescriptions.c_str()) * 12;
int stateWidth = strlen(problemDescriptions.c_str()) * 12; // Approximate width of the status text in pixels
// Scroll the text to the left
if (!is_display_off) {
if (millis() - lastScrollTime > scrollDelay) {
// Update scroll position
scrollPos -= 11; // Move left by 1 pixel each time
scrollPos -= 11;
lastScrollTime = millis();
// If the text has completely scrolled off, reset scroll position to start from the right
if (scrollPos < -stateWidth) {
scrollPos = SCREEN_WIDTH;
}
}
}
// Draw the file name with current scroll position
display.setTextSize(2);
display.setCursor(scrollPos, 48);
display.print(problemDescriptions);
display.drawLine(0,46,127,46,1);
display.setTextSize(1);
display.fillRect(112,35,16,11,0);
display.drawRect(112,35,16,11,1);
display.setCursor(114,37);
display.print(totalProblems);
display.display();
}
@@ -182,8 +236,8 @@ void loop() {
displayProblems();
if (newProblemsDetected) {
beep(1000);
if (newProblemsDetected && !is_sound_off) {
playAlertMelody();
newProblemsDetected = false;
}
}
}