2

我正在尝试增加和减少读数列表的索引,但是,每次我运行update()时,m_notifIndex都会被设置回零。很抱歉,如果以前有人问过这类问题,但是在寻找了一个小时的答案后,我放弃了,继续在这里问我的第一个问题。再次抱歉格式错误,这是我第一次发帖。

显示.cpp

#include "display.h"

#include <time.h>
#include <unistd.h>
#include <iostream>

Display::Display(unsigned int width, unsigned int height, unsigned int num_sensors, std::string* sensors, const std::string& dir, Message* messages) {

    m_notifIndex = 0;
}

void Display::update() {
    if (m_showNotif) {
        //when there are no more messages, show the notifications
        while (!m_notifications->isEmpty()) {
            //first draw the notification if there is a warning in the current index
            if (m_notifications->sensors[m_notifIndex] != NULL) {
                m_oled->clear(PAGE);
                drawSensor();
                m_oled->display();
                sleep(1);

                //keep updating the message and notif stack when there are no inputs
                while (m_a->pinRead() == HIGH && m_right->pinRead() == HIGH && m_left->pinRead() == HIGH && m_b->pinRead() == HIGH) {
                    m_messages->updateMsgStack();
                    updateNotif();
                    if (!m_messages->isEmpty() || m_notifications->warnings[m_notifIndex])
                        return; //break away from the loop if a new message comes in
                }

                //listen in for inputs
                if (m_right->pinRead() == LOW) {
                    //wait until the button is released
                    while (m_right->pinRead() == LOW) {}
                    if (m_notifIndex == m_num_sensors-1)
                        m_notifIndex = 0; //wrap around when at the end
                    else m_notifIndex++;
                }
                else if (m_left->pinRead() == LOW) {
                    while (m_left->pinRead() == LOW) {}
                    if (m_notifIndex == 0)
                        m_notifIndex = m_num_sensors-1; //wrap around when at the beginning
                    else m_notifIndex--;
                }
                else if (m_a->pinRead() == LOW) {
                    while (m_a->pinRead() == LOW) {}
                    m_showNotif = false;
                    return;
                }

                checkForPanic();
            }
            else { //when the current index has no warning
                if (m_notifIndex == m_num_sensors-1)
                    m_notifIndex = 0; //wrap around when at the end
                else m_notifIndex++;
            }
        }
    }
}

void Display::updateNotif() {
    std::string line;
    for (unsigned int i = 0; i < sizeof(m_sensors)/sizeof(m_sensors[0]); i++) {
        const char* filePath = (m_notifDir + m_sensors[i] + ".txt").c_str();
        if (m_messages->exists(filePath)) {
            usleep(10000);
            try {
                m_messages->m_incMsg.open(filePath);
                while(std::getline(m_messages->m_incMsg, line)) {
                    m_notifications->addWarning (line, i);
                }
                m_messages->m_incMsg.close();
            }
            catch (std::ios_base::failure& e) {
                std::cerr << e.what() << '\n' << std::endl;
            }
        }
        else m_notifications->removeWarning (i); //delete the warning when the file doesnt exist
    }
}

void Display::checkForPanic() {
    if (m_b->pinRead() == LOW) {
        time_t hold;
        time(&hold);
        while (m_b->pinRead() == LOW) {
            if (time(NULL) > hold + 3) { //if the timer reaches 3 seconds
                //m_messages->sendReply("Miner_Emergency");
                return;
            }
        }
    }
}

显示.h

#ifndef OLED_DISPLAY_H_
#define OLED_DISPLAY_H_

#include <sstream>
#include <string>

#include "image/image.h"
#include "oled/Edison_OLED.h"
#include "message.h"
#include "DataStructs/Notif.h"
#include "gpio/gpio.h"

#define SLEEPTIMEOUT 20

class Display {
public:
    Display(unsigned int width, unsigned int height, unsigned int num_sensors, std::string* sensors, const std::string& dir, Message* messages);

    void run();

    ~Display();

private:
    edOLED* m_oled;
    const unsigned int m_height, m_width;
    Image* m_miner;
    Image* m_checkin;
    Image* m_reminder;
    Image* m_blast;
    Image* m_go_to;
    Image* m_goto_zone;
    bool m_isSleeping, m_showNotif;
    time_t m_timer;
    Message* m_messages;

    std::string* m_sensors;
    std::string m_notifDir;
    NotifHandler* m_notifications;
    unsigned int m_notifIndex;
    const unsigned int m_num_sensors;

    gpio* m_up;
    gpio* m_down;
    gpio* m_left;
    gpio* m_right;
    gpio* m_a;
    gpio* m_b;

    void drawImage(Image* image);
    void update();
    void drawMessage(MsgNode* message);
    void drawTime();
    void drawSensor();
    void updateNotif();
    void checkForPanic();
};

#endif //OLED_DISPLAY_H_
4

0 回答 0