0

我正在使用 Odroid(类似 Raspi 的 ARM 板)来运行基于 SPI 的小型无线电芯片,该芯片将数据发送到 Websocket 等。这是使用 Websocket++ 应用的:https ://github.com/zaphoyd/websocketpp 。我对其中一个简单的例子进行了混蛋,它向所有客户端发送服务器消息。该程序有一个处理 websocket 的 count_server 类,还有一个由 WiringPi 处理的 ISR,它调用嵌套函数来处理不同的操作。

我遇到的问题是,为了发送这条消息,据我所知,发送函数必须在 count_server 类中,以访问客户端地址等。这个类方法不能从我的 ISR 内部访问,它处理从无线电接收的所有数据,所以当我尝试从 ISR 内部发送 websocket 消息时,我收到错误:

error: 'webSocketServer' was not declared in this scope webSocketServer.sendLiveData();

webSocketServer 是类 count_server 的一个实例,在 main() 中实例化。为什么 ISR 不能“看到” webSocketServer 类。

一种解决方法是在 count() 函数内部进行轮询,但这会阻塞 CPU,我宁愿让它准备好执行其他任务。]

这是我能产生的最简单的例子。attachInterrupt 需要 Websocket++ 和 WiringPi。

#include <cstdlib>
#include <unistd.h>
#include <iostream>
#include <sstream>
#include <string>
#include <RF24/RF24.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//Websocket++ includes
#include <mutex>
#include <set>
#include <thread>

#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
//*/


//websocket++ declarations

typedef websocketpp::server<websocketpp::config::asio> server;

using websocketpp::connection_hdl;
using websocketpp::lib::placeholders::_1;

class count_server {
public:
    count_server() : m_count(0) {
        m_server.init_asio();

        m_server.set_open_handler(bind(&count_server::on_open,this,_1));
        m_server.set_close_handler(bind(&count_server::on_close,this,_1));
    }

    void on_open(connection_hdl hdl) {
        std::lock_guard<std::mutex> lock(m_mutex);
        m_connections.insert(hdl);
    }

    void on_close(connection_hdl hdl) {
        std::lock_guard<std::mutex> lock(m_mutex);
        m_connections.erase(hdl);
    }

        void sendLiveData(){
        std::stringstream ss;
        ss << "foobar";
        for (auto it : m_connections) {
                 m_server.send(it,ss.str(),websocketpp::frame::opcode::text);
            }

    }

    void count() {//simple loop thread, most likely not needed, but in working example so lef
t for time being
        while (1) {
            sleep(1000);
                }
        }
    void run(uint16_t port) {
        m_server.listen(port);
        m_server.start_accept();
        m_server.run();
    }
private:
    typedef std::set<connection_hdl,std::owner_less<connection_hdl>> con_list;

    int m_count;
    server m_server;
    con_list m_connections;
    std::mutex m_mutex;
};

/****************** Raspberry Pi ***********************/


int interruptPin = 6; // GPIO pin for interrupts - interrupts have been edited to be handled
//by wiringPi, so #6 is used, not #103, check RF24/utility/SPIDEV/interrupt.c for info
int i=0;
/**************************************************************/

void addLiveData(){ //Live data buffer handler + calls sending function when buffer hits max
webSocketServer.sendLiveData();
}

void intHandler(){//when radio chip IRQ goes Low, something happened, this handles it
                addLiveData();//recvd = 2;//flag that the data is live data
}

int main(){

    attachInterrupt(interruptPin, INT_EDGE_FALLING, &intHandler); //Attach interrupt to bcm p
in 23
    count_server webSocketServer;
    std::thread t(std::bind(&count_server::count,&webSocketServer));
    webSocketServer.sendLiveData();
    webSocketServer.run(8080);
}
'
4

0 回答 0