This commit is contained in:
icaema 2024-01-02 19:10:12 -05:00
commit 7b053279f9
5 changed files with 192 additions and 0 deletions

44
APKBUILD Normal file
View File

@ -0,0 +1,44 @@
# Contributor:
# Maintainer:
pkgname=ledctl
pkgver=1.1
pkgrel=0
pkgdesc="Simple Utilities for controlling the led on a RPI0W"
url="http://127.0.0.1/ledctl"
arch="x86_64 armhf"
license="Unlicense"
depends=""
makedepends=""
checkdepends=""
install=""
subpackages=""
source="
ledctl.c
ledctl_off
ledctl_tipoff
"
builddir="$srcdir/"
build() {
gcc -O3 -Wall "ledctl.c" -o "$builddir/ledctl"
}
check() {
return
}
package() {
# Replace with proper package command(s)
install -m755 -D "$srcdir"/$pkgname \
"$pkgdir"/usr/bin/$pkgname
install -m755 -D "$srcdir"/ledctl_off \
"$pkgdir"/etc/init.d/ledctl_off
install -m755 -D "$srcdir"/ledctl_tipoff \
"$pkgdir"/etc/init.d/ledctl_tipoff
}
sha512sums="
6c0780670f77b48e74d167160b7ba3d52f40964bf1eb45f4982d0f9bf63d5402b82e29e25c31936369e344cb8bf74de952f37916478dca87bd50390f3c21f49f ledctl.c
aac806e7c18d1594450124f3554ccb3c3cba760fc8512fd6c60a0aee544d016d74fd0f944ed4bc66c06554892937d0f1eb796052c195bbcd7f1f1eda44760755 ledctl_off
01783cc00c972fed0bb2cf8df5e4040872cfcfa34af887583e6076d46372003aee1026f05d36038af2520dfaffc7af69d0d9a33b502bf88d1e34b721ac6310fc ledctl_tipoff
"

24
LICENCE Normal file
View File

@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org>

100
ledctl.c Normal file
View File

@ -0,0 +1,100 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#define LEDFILE "/sys/class/leds/ACT/brightness"
//write to the led
int wrLED(int fd, int val){
char *opts[] = {"0", "1"};
return write(fd, opts[val], 1);
}
void printVersion(){
printf("ledctl 1.0\n");
}
void printUsage(char *errormsg){
if (errormsg != NULL) printf("Error: %s\n", errormsg);
printVersion();
printf("Usage:\n");
printf(" ledctl -v print version information\n");
printf(" ledctl -h print this message\n");
printf(" ledctl on|off turn the LED on or off\n");
printf(" ledctl 1|0\n");
printf(" ledctl -r REPEAT -s VALUE TIMING [-s VALUE TIMING ...]\n");
}
int main(int argc, char *argv[]){
if (argc < 2) return 1;
char path[] = LEDFILE;
int fd = open(path, O_RDWR);
if (strcmp(argv[1], "on") == 0 || strcmp(argv[1], "1") == 0){
wrLED(fd, 1);
close(fd);
return 0;
}
if (strcmp(argv[1], "off") == 0 || strcmp(argv[1], "0") == 0){
wrLED(fd, 0);
close(fd);
return 0;
}
if (strcmp(argv[1], "-v") == 0 && argc == 2){
printVersion(NULL);
close(fd);
return 0;
}
if (strcmp(argv[1], "-h") == 0 && argc == 2){
printUsage(NULL);
close(fd);
return 0;
}
if (strcmp(argv[1], "-r") != 0 || argc < 6 || (argc % 3) != 0){
printUsage(NULL);
close(fd);
return 1;
}
if (argc % 3 !=0) {
printUsage("Invalid number of arguments");
close(fd);
return 1;
}
// keep track of previous state
char prev;
read(fd, &prev, 1);
int pattern_repetitions = atoi(argv[2]);
char **args_pattern = &argv[3];
int pattern_steps = (argc-3) / 3;
int pattern_values[pattern_steps];
float pattern_timing[pattern_steps];
//parse arguments into our schema
for (int i=0; i < pattern_steps; i++){
int args_pattern_index = i*3;
if (strcmp(args_pattern[args_pattern_index], "-s") != 0){
printUsage("Invalid format for -s flag");
close(fd);
return 1;
}
pattern_values[i] = atoi(args_pattern[args_pattern_index + 1]);
pattern_timing[i] = atof(args_pattern[args_pattern_index + 2]);
}
for (int i = 0; i < pattern_repetitions; i++) {
for (int pstep = 0; pstep < pattern_steps; pstep++) {
wrLED(fd, pattern_values[pstep]);
usleep(1000000 * pattern_timing[pstep]);
}
}
write(fd, &prev, 1);
close(fd);
}

13
ledctl_off Executable file
View File

@ -0,0 +1,13 @@
#!/sbin/openrc-run
name="ledoff"
description="Turn the danm led off"
depend() {
need localmount
before sysctl
}
start(){
ledctl off
}

11
ledctl_tipoff Executable file
View File

@ -0,0 +1,11 @@
#!/sbin/openrc-run
name="ledtipoff"
description="Blink Led when ssh is enabled"
depend() {
need sshd
}
start() {
ledctl -r 3 -s 0 0.2 -s 1 0.1 -s 0 0.1 -s 1 0.1
}