Created
July 13, 2014 23:58
-
-
Save makotoshimazu/c4372256b54cabb62bfb to your computer and use it in GitHub Desktop.
Blink the LEDs on Beagle Bone Black
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 <fstream> | |
#include <sstream> | |
#include <stdexcept> | |
#include "led.h" | |
using namespace pile; | |
using namespace std; | |
LED::LED(int id) | |
{ | |
if (id < 0 || id > 4) | |
throw invalid_argument("id must be from 0 to 3"); | |
this->id = id; | |
stringstream ss; | |
ss << "/sys/class/leds/beaglebone:green:usr" << id << "/brightness"; | |
this->path = ss.str(); | |
} | |
void LED::setBrightness(int parcent) | |
{ | |
if (parcent < 0 || parcent > 100) | |
throw invalid_argument("parcent must be from 0 to 100"); | |
this->brightness = parcent; | |
int duty = 255*parcent/100; | |
ofstream os(this->path); | |
os << duty; | |
os.close(); | |
} |
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
#pragma once | |
#include <string> | |
namespace pile | |
{ | |
class LED | |
{ | |
public: | |
LED(int id); | |
void setBrightness(int parcent); | |
private: | |
int id; | |
int brightness; | |
std::string path; | |
}; | |
} |
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 <iostream> | |
#include <unistd.h> | |
#include <memory> | |
#include "led.h" | |
using namespace pile; | |
int main() | |
{ | |
std::shared_ptr<LED> leds[4]; | |
for (int i = 0; i < 4; i++) | |
leds[i] = std::shared_ptr<LED>(new LED(i)); | |
while (true) { | |
for (int i = 0; i < 4; i++) { | |
std::cout << i << std::endl; | |
leds[i]->setBrightness(100); | |
sleep(1); | |
} | |
for (int i = 0; i < 4; i++) { | |
std::cout << i << std::endl; | |
leds[i]->setBrightness(0); | |
sleep(1); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment