Reused ACT (led0) light as activity light.

This commit is contained in:
2018-08-12 11:33:41 +01:00
parent 2b01dbc605
commit cf69741c24
5 changed files with 89 additions and 3 deletions

View File

@@ -2,6 +2,7 @@
#include "u2f.hpp" #include "u2f.hpp"
#include <iostream> #include <iostream>
#include "IO.hpp" #include "IO.hpp"
#include "LED.hpp"
using namespace std; using namespace std;
@@ -11,10 +12,17 @@ Controller::Controller(const uint32_t startChannel)
void Controller::handleTransaction() void Controller::handleTransaction()
{ {
if (channels.size() != 0 && chrono::duration_cast<chrono::seconds>(chrono::system_clock::now() - lastMessage) < 5s)
toggleACTLED();
else
enableACTLED(false);
auto msg = U2FMessage::readNonBlock(); auto msg = U2FMessage::readNonBlock();
if (!msg) if (!msg)
return; return;
lastMessage = chrono::system_clock::now();
auto opChannel = msg->cid; auto opChannel = msg->cid;

View File

@@ -1,12 +1,14 @@
#pragma once #pragma once
#include <map> #include <map>
#include <chrono>
#include "Channel.hpp" #include "Channel.hpp"
class Controller class Controller
{ {
protected: protected:
std::map<uint32_t, Channel> channels; std::map<uint32_t, Channel> channels;
uint32_t currChannel; uint32_t currChannel;
std::chrono::system_clock::time_point lastMessage;
public: public:
Controller(const uint32_t startChannel = 1); Controller(const uint32_t startChannel = 1);

49
LED.cpp Normal file
View File

@@ -0,0 +1,49 @@
#include "LED.hpp"
#include <fstream>
#include <stdexcept>
#include <string>
using namespace std;
bool& ledState()
{
static bool state = true;
return state;
}
bool getLEDState()
{
return ledState();
}
void disableACTTrigger(bool nowDisabled)
{
ofstream trigFile{ "/sys/class/leds/led0/trigger", ofstream::out | ofstream::trunc };
if (!trigFile)
throw runtime_error{ "Failed to retrieve led0 trigger file" };
if (!static_cast<bool>(trigFile << (nowDisabled ? "none" : "mmc0")))
throw runtime_error{ "Failed to write led0 trigger to file" };
}
void enableACTLED(bool nowOn)
{
if (nowOn == getLEDState())
return;
ofstream enabledFile{ "/sys/class/leds/led0/brightness", ofstream::out | ofstream::trunc };
if (!enabledFile)
throw runtime_error{ "Failed to retrieve led0 brightness" };
if (!static_cast<bool>(enabledFile << to_string(nowOn ? 0 : 1)))
throw runtime_error{ "Failed to write led0 brightness to file" };
ledState() = nowOn;
}
void toggleACTLED()
{
enableACTLED(!getLEDState());
}

6
LED.hpp Normal file
View File

@@ -0,0 +1,6 @@
#pragma once
bool getLEDState();
void disableACTTrigger(bool nowDisabled);
void enableACTLED(bool nowOn);
void toggleACTLED();

View File

@@ -1,6 +1,7 @@
#include <iostream> #include <iostream>
#include "Storage.hpp" #include "Storage.hpp"
#include "Controller.hpp" #include "Controller.hpp"
#include "LED.hpp"
#include <signal.h> #include <signal.h>
#include <unistd.h> #include <unistd.h>
@@ -12,6 +13,16 @@ volatile bool contProc = true;
int main() int main()
{ {
try
{
disableACTTrigger(true);
enableACTLED(false);
}
catch (runtime_error &e)
{
cerr << e.what() << endl;
}
signal(SIGINT, signalCallback); signal(SIGINT, signalCallback);
Storage::init(); Storage::init();
@@ -25,11 +36,21 @@ int main()
Storage::save(); Storage::save();
try
{
disableACTTrigger(false);
enableACTLED(true);
}
catch (runtime_error &e)
{
cerr << e.what() << endl;
}
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
void signalCallback([[maybe_unused]] int signum) void signalCallback([[maybe_unused]] int signum)
{ {
contProc = false; contProc = false;
clog << "Caught SIGINT signal" << endl; clog << "\nCaught SIGINT signal" << endl;
} }