Last active
August 29, 2015 13:56
-
-
Save nna774/9003891 to your computer and use it in GitHub Desktop.
gplv3 by nonamea774
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "select.hpp" | |
#include <functional> | |
class GPIO{ | |
public: | |
GPIO(std::function<void(Button)> _callback); | |
void waitButtonPushed(){ | |
callback(select.getPushedButton()); | |
} | |
private: | |
std::function<void(Button)> callback; | |
Select select; | |
}; | |
GPIO::GPIO(std::function<void(Button)> _callback){ | |
callback = _callback; | |
// GPIO の見えているファイルと, ボタンの関連付け | |
select.addButton(Button::Zero, "/sys/class/gpio/gpio4/value"); | |
select.addButton(Button::One, "/sys/class/gpio/gpio7/value"); | |
select.addButton(Button::Two, "/sys/class/gpio/gpio8/value"); | |
select.addButton(Button::Three, "/sys/class/gpio/gpio9/value"); | |
select.addButton(Button::Four, "/sys/class/gpio/gpio10/value"); | |
select.addButton(Button::Five, "/sys/class/gpio/gpio11/value"); | |
select.addButton(Button::Six, "/sys/class/gpio/gpio17/value"); | |
select.addButton(Button::Seven, "/sys/class/gpio/gpio18/value"); | |
select.addButton(Button::Eight, "/sys/class/gpio/gpio22/value"); | |
select.addButton(Button::Nine, "/sys/class/gpio/gpio23/value"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "select.hpp" | |
void Select::addButton(Button buttonName, std::string fileName){ | |
auto fd = open(fileName.c_str(), O_RDONLY); | |
buttonMap.push_back({buttonName, fd}); | |
char buff; | |
read(fd, &buff, 1); | |
lseek(fd, 0, SEEK_SET); | |
} | |
Button Select::getPushedButton(){ | |
fd_set fds; | |
FD_ZERO(&fds); | |
for(auto e: buttonMap){ | |
FD_SET(e.second, &fds); | |
} | |
select(buttonMap.size(), &fds, nullptr, nullptr, nullptr); | |
for(auto e: buttonMap){ | |
auto fd = e.second; | |
if(FD_ISSET(fd, &fds)){ | |
char buff; | |
read(fd, &buff, 1); | |
lseek(fd, 0, SEEK_SET); | |
return e.first; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/time.h> | |
#include <sys/types.h> | |
#include <unistd.h> | |
#include <string> | |
#include <algorithm> | |
#include <fcntl.h> | |
enum class Button{ | |
Zero, | |
One, | |
Two, | |
Three, | |
Four, | |
Five, | |
Six, | |
Seven, | |
Eight, | |
Nine | |
}; | |
class Select { | |
public: | |
using FileDescriptor = int; | |
Select() {} | |
void addButton(Button buttonName, std::string fileName); | |
Button getPushedButton(); | |
private: | |
std::vector<std::pair<Button, FileDescriptor>> buttonMap; | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment