Reused ACT (led0) light as activity light.
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
#include "u2f.hpp"
|
||||
#include <iostream>
|
||||
#include "IO.hpp"
|
||||
#include "LED.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -11,11 +12,18 @@ Controller::Controller(const uint32_t startChannel)
|
||||
|
||||
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();
|
||||
|
||||
if (!msg)
|
||||
return;
|
||||
|
||||
lastMessage = chrono::system_clock::now();
|
||||
|
||||
auto opChannel = msg->cid;
|
||||
|
||||
if (msg->cmd == U2FHID_INIT)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
#include <map>
|
||||
#include <chrono>
|
||||
#include "Channel.hpp"
|
||||
|
||||
class Controller
|
||||
@@ -7,6 +8,7 @@ class Controller
|
||||
protected:
|
||||
std::map<uint32_t, Channel> channels;
|
||||
uint32_t currChannel;
|
||||
std::chrono::system_clock::time_point lastMessage;
|
||||
|
||||
public:
|
||||
Controller(const uint32_t startChannel = 1);
|
||||
|
||||
49
LED.cpp
Normal file
49
LED.cpp
Normal 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
6
LED.hpp
Normal file
@@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
bool getLEDState();
|
||||
void disableACTTrigger(bool nowDisabled);
|
||||
void enableACTLED(bool nowOn);
|
||||
void toggleACTLED();
|
||||
23
monitor.cpp
23
monitor.cpp
@@ -1,6 +1,7 @@
|
||||
#include <iostream>
|
||||
#include "Storage.hpp"
|
||||
#include "Controller.hpp"
|
||||
#include "LED.hpp"
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
|
||||
@@ -12,6 +13,16 @@ volatile bool contProc = true;
|
||||
|
||||
int main()
|
||||
{
|
||||
try
|
||||
{
|
||||
disableACTTrigger(true);
|
||||
enableACTLED(false);
|
||||
}
|
||||
catch (runtime_error &e)
|
||||
{
|
||||
cerr << e.what() << endl;
|
||||
}
|
||||
|
||||
signal(SIGINT, signalCallback);
|
||||
Storage::init();
|
||||
|
||||
@@ -25,11 +36,21 @@ int main()
|
||||
|
||||
Storage::save();
|
||||
|
||||
try
|
||||
{
|
||||
disableACTTrigger(false);
|
||||
enableACTLED(true);
|
||||
}
|
||||
catch (runtime_error &e)
|
||||
{
|
||||
cerr << e.what() << endl;
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
void signalCallback([[maybe_unused]] int signum)
|
||||
{
|
||||
contProc = false;
|
||||
clog << "Caught SIGINT signal" << endl;
|
||||
clog << "\nCaught SIGINT signal" << endl;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user