diff options
| -rw-r--r-- | Makefile | 10 | ||||
| -rw-r--r-- | led.cpp | 23 | ||||
| -rw-r--r-- | led.h | 4 | ||||
| -rw-r--r-- | main.cpp | 34 | ||||
| -rw-r--r-- | morse.cpp | 104 | ||||
| -rw-r--r-- | morse.h | 6 |
6 files changed, 181 insertions, 0 deletions
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 @@ -0,0 +1,23 @@ +#include <fstream> +#include <leds.h> +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; +} @@ -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 <unistd.h> +#include <led.h> +#include <morse.h> +#include <iostream> // 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 <led.h> +#include <iostream> +#include <string> +#include <map> +#include <ctime> +#include <cctype> +#include <unistd.h> +#include <cstring> +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<string, string> table; +void transmit(const string& str) +{ + cout << "Transmitting message in Morse code via ACT LED..." << endl; + for(unsigned long long i=0;i<str.length();++i) + { + char ch=str[i]; + string tmp; + tmp=toupper(ch); + string morse=table[tmp]; + if(morse!="") + cout << ch << ": " << flush; + for(unsigned int j=0;j<morse.length();++j) + { + char morseCh=morse[j]; + if(morseCh=='.') + dot(); + else if(morseCh=='-') + dash(); + usleep(200000); + } + if(morse!="") + cout << " " << flush; + usleep(250000); // between chars + if(isspace(ch)) + { + cout << " " << flush; + usleep(500000); // extra for spaces + } + } +} + +void init_morse() +{ + table["A"]=".-"; + table["B"]="-..."; + table["C"]="-.-."; + table["D"]="-.."; + table["E"]="."; + table["F"]="..-."; + table["G"]="--."; + table["H"]="...."; + table["I"]=".."; + table["J"]=".---"; + table["K"]="-.-"; + table["L"]=".-.."; + table["M"]="--"; + table["N"]="-."; + table["O"]="---"; + table["P"]=".--."; + table["Q"]="--.-"; + table["R"]=".-."; + table["S"]="..."; + table["T"]="-"; + table["U"]="..-"; + table["V"]="...-"; + table["W"]=".--"; + table["X"]="-..-"; + table["Y"]="-.--"; + table["Z"]="--.."; + table["0"]="-----"; + table["1"]=".----"; + table["2"]="..---"; + table["3"]="...--"; + table["4"]="....-"; + table["5"]="....."; + table["6"]="-...."; + table["7"]="--..."; + table["8"]="---.."; + table["9"]="----."; + table["."]=".-.-.-"; + table[","]="--..--"; + table["("]="-.--."; + table[")"]="-.--.-"; + table["/"]="-..-."; + table["="]="-...-"; + table["+"]=".-.-."; + table["-"]="-....-"; + table["\""]=".-..-."; + table["@"]=".--.-."; +} @@ -0,0 +1,6 @@ +#include <string> +using namespace std; +void transmit(const string&); +void morse_init(); // call this first! +void dot(); +void dash(); |