Skip to content
Snippets Groups Projects
hw_impl_unix_mio7_led.c 1.08 KiB
#include "hw_impl_unix.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <pthread.h>

void * output_cached_led();

int on;
static char *led_fifo_name;
pthread_t worker;

int unix_mio7_led_reset(struct LEDDriver *self) {
  led_fifo_name = VIRT_QUAD_FIFOS_DIR "/mio7-led";

  mkdir(VIRT_QUAD_FIFOS_DIR, 0777);
  int i;
  for (i = 0; i < 4; i += 1) {
    unlink(led_fifo_name);
    mkfifo(led_fifo_name, 0666);
  }

  // Start up worker thread whose job is to update the caches
  pthread_create(&worker, 0, output_cached_led, NULL);

  return 0;
}

int unix_mio7_led_turn_on(struct LEDDriver *self) {
  if (!on) {
    puts("LED ON");
    on = 1;
  }
  return 0;
}

int unix_mio7_led_turn_off(struct LEDDriver *self) {
  if (on) {
    puts("LED OFF");
    on = 0;
  }
  return 0;
}

void * output_cached_led() {
  char buff[16];
  while (1) {
    int fifo = open(led_fifo_name, O_WRONLY);
    sprintf(buff, "%d\n", on);
    write(fifo, buff, strlen(buff));
    close(fifo);
    usleep(500); // don't spam the reader
  }
}