|
Boost Users : |
Subject: Re: [Boost-users] [Asio] Need help making a custom service
From: svante karlsson (saka_at_[hidden])
Date: 2015-03-16 09:32:40
I think it should be similar to networking code - and there is no need to
implement that there.
I'm still curious as to why we would get an interrupt because state changes
on IO pins (but that has nothing to do with boost). But as I said I have
been to busy the last week to try it out on a raspberry.
Also note that I have not thought the destruction faze through. So you
might crash there..
2015-03-16 14:18 GMT+01:00 Kyle Edwards <kedwards_at_[hidden]>:
> Haven't tested it, but at first glance it looks correct.
>
> So you don't think there's a need to implement service and all that,
> like I did in my code?
>
> Kyle
>
> On Mon, 2015-03-16 at 12:27 +0100, svante karlsson wrote:
> > Sorry for not replying earlier but I have not yet gotten any time to
> > actually try it out on a raspberry.
> >
> >
> > Hopefully tonight.... anyway - if the original code worked then the
> > following "could" there is probably mistakes since I compiled this on
> > windows and the ::open call in not working there...
> >
> >
> >
> >
> > #include <fcntl.h>
> > #include <iostream>
> > #include <boost/bind.hpp>
> > #include <boost/thread.hpp>
> > #include <boost/asio.hpp>
> > #include <boost/function.hpp>
> >
> > class gpio_interrupt
> > {
> > public:
> > gpio_interrupt(boost::asio::io_service &ios,
> > unsigned int number) :
> > _socket(ios)
> > {
> > open(number);
> > }
> >
> > ~gpio_interrupt()
> > {
> > close();
> > }
> >
> > void open(unsigned int number)
> > {
> > boost::system::error_code ec;
> > open(number, ec);
> > boost::asio::detail::throw_error(ec, "open");
> > }
> >
> > void open(unsigned int number,
> > boost::system::error_code& ec)
> > {
> > std::ostringstream filename;
> > filename << "/sys/class/gpio/gpio" << number
> > << "/value";
> >
> > int fd = ::open(filename.str().c_str(),
> > O_RDONLY | O_NONBLOCK);
> > if (fd < 0)
> > {
> > ec = boost::system::error_code(errno,
> > boost::asio::error::get_system_category());
> > return;
> > }
> > _socket.assign(boost::asio::ip::udp::v4(), fd,
> > ec);
> > }
> >
> > void close()
> > {
> > boost::system::error_code ec;
> > cancel(ec);
> > boost::asio::detail::throw_error(ec, "close");
> > }
> >
> > void close(boost::system::error_code& ec)
> > {
> > cancel(ec);
> > }
> >
> > void cancel()
> > {
> > boost::system::error_code ec;
> > _socket.cancel(ec);
> > boost::asio::detail::throw_error(ec, "close");
> > }
> >
> > void cancel(boost::system::error_code& ec)
> > {
> > _socket.cancel(ec);
> > }
> >
> > void async_wait(boost::function<void(const
> > boost::system::error_code& ec)> cb)
> > {
> >
> > _socket.async_receive(boost::asio::null_buffers(),
> > [cb](const boost::system::error_code& ec, std::size_t
> > bytes_transferred)
> > {
> > cb(ec);
> > });
> > }
> >
> > private:
> > boost::asio::ip::udp::socket _socket;
> > };
> >
> >
> > int main(int argc, char **argv)
> > {
> > boost::asio::io_service ios;
> > boost::asio::io_service::work work(ios);
> > boost::thread
> > thread(boost::bind(&boost::asio::io_service::run,
> > &ios));
> >
> > gpio_interrupt interrupt(ios, 21);
> > interrupt.async_wait([](const
> > boost::system::error_code& ec)
> > {
> > if (ec)
> > {
> > std::cout << ec.message() << std::endl;
> > return;
> > }
> >
> > std::cout << "Pin changed state" << std::endl;
> > });
> >
> > while (true)
> > {
> >
> >
> boost::this_thread::sleep(boost::posix_time::milliseconds(1000)); // just
> dummy - do something else here...
> > }
> >
> > ios.stop();
> > thread.join();
> > return 0;
> > }
> >
> >
> > _______________________________________________
> > Boost-users mailing list
> > Boost-users_at_[hidden]
> > http://lists.boost.org/mailman/listinfo.cgi/boost-users
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net