From 3b33d88f5621a8d40ccf4c0177c98f63a5c19c38 Mon Sep 17 00:00:00 2001 From: Franklin Wei Date: Thu, 1 Jan 1970 01:45:37 +0100 Subject: Initial commit --- Makefile | 10 ++++++ led.cpp | 23 ++++++++++++++ led.h | 4 +++ main.cpp | 34 ++++++++++++++++++++ morse.cpp | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ morse.h | 6 ++++ 6 files changed, 181 insertions(+) create mode 100644 Makefile create mode 100644 led.cpp create mode 100644 led.h create mode 100644 main.cpp create mode 100644 morse.cpp create mode 100644 morse.h diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..cd6ce6d --- /dev/null +++ b/Makefile @@ -0,0 +1,10 @@ +SOURCES = led.o morse.o main.o +HEADERS = led.h morse.h +CXXFLAGS = -I. +pish: $(SOURCES) $(HEADERS) Makefile + g++ $(SOURCES) -o pish +all: pish +clean: + rm -f *~ + rm -f *.o + rm -f pish diff --git a/led.cpp b/led.cpp new file mode 100644 index 0000000..16d48a1 --- /dev/null +++ b/led.cpp @@ -0,0 +1,23 @@ +#include +#include +using namespace std; +bool isLightOn() +{ + ifstream ifs("/sys/class/leds/led0/brightness"); + int brightness; + ifs >> brightness; + if(brightness==0) + return false; + else + return true; +} +void setLight(bool on) +{ + ofstream ofs("/sys/class/leds/led0/brightness"); + int val; + if(on) + val=255; + else + val=0; + ofs << val << flush; +} diff --git a/led.h b/led.h new file mode 100644 index 0000000..49b8f6c --- /dev/null +++ b/led.h @@ -0,0 +1,4 @@ +bool isLightOn(); +void setLight(bool); +#define lightOn() setLight(true) +#define lightOff() setLight(false) diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..d3ec2ae --- /dev/null +++ b/main.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include // let the user use this with a screen, too +using namespace std; +void parse_args(int argc, char* argv[]) +{ + // nothing for now +} +int main(int argc, char* argv[]) +{ + if(geteuid()!=0) // must be root to control LEDS! + { + cerr << "Need to be root to control LEDs!"<< endl; + cerr << "Current user ID: " << geteuid() << endl; + return 1; + } + parse_args(argc, argv); + cout << "PiSH initializied." << endl; + dash(); + usleep(250000); + dash(); + usleep(250000); + string cmd; + while(cmd!="exit") + { + dot(); + cout << "PiSH> "; + getline(cin, cmd); + dash(); + usleep(250000); + } +} + diff --git a/morse.cpp b/morse.cpp new file mode 100644 index 0000000..95c5114 --- /dev/null +++ b/morse.cpp @@ -0,0 +1,104 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +void dot() +{ + cout << "." << flush; + setLight(true); + usleep(250000); + setLight(false); +} +void dash() +{ + cout << "-" << flush; + setLight(true); + usleep(750000); + setLight(false); +} +static map table; +void transmit(const string& str) +{ + cout << "Transmitting message in Morse code via ACT LED..." << endl; + for(unsigned long long i=0;i +using namespace std; +void transmit(const string&); +void morse_init(); // call this first! +void dot(); +void dash(); -- cgit v1.1