Skip to content

Instantly share code, notes, and snippets.

@makotoshimazu
Created July 13, 2014 23:58
Show Gist options
  • Save makotoshimazu/c4372256b54cabb62bfb to your computer and use it in GitHub Desktop.
Save makotoshimazu/c4372256b54cabb62bfb to your computer and use it in GitHub Desktop.
Blink the LEDs on Beagle Bone Black
#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();
}
#pragma once
#include <string>
namespace pile
{
class LED
{
public:
LED(int id);
void setBrightness(int parcent);
private:
int id;
int brightness;
std::string path;
};
}
#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