Newer
Older
mpd-beat / main.c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <wiringPi.h>
#include <mpd/client.h>

pthread_cond_t isr_cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t isr_mtx = PTHREAD_MUTEX_INITIALIZER;
unsigned int pin_pressed= 0;

#define FASTFORWARD_PIN 5
#define PLAYPAUSE_PIN 6
#define REWIND_PIN 13
#define VOLUMEDOWN_PIN 26
#define ONOFF_PIN 12
#define VOLUMEUP_PIN 16

void serviceInterrupt(int pin)
{
  pthread_mutex_lock(&isr_mtx);
  pin_pressed = pin;
  pthread_cond_signal(&isr_cond);
  pthread_mutex_unlock(&isr_mtx);
}

void fastForwardInterrupt() { serviceInterrupt(FASTFORWARD_PIN); }
void playPauseInterrupt() { serviceInterrupt(PLAYPAUSE_PIN); }
void rewindInterrupt() { serviceInterrupt(REWIND_PIN); }
void volumeDownInterrupt() { serviceInterrupt(VOLUMEDOWN_PIN); }
void onOffInterrupt() { serviceInterrupt(ONOFF_PIN); }
void volumeUpInterrupt() { serviceInterrupt(VOLUMEUP_PIN); }

int main()
{
  struct mpd_connection *conn;
  conn = mpd_connection_new(NULL, 0, 0);
  if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) {
    mpd_connection_free(conn);
    return EXIT_FAILURE;
  }
  
  wiringPiSetupGpio();
  pullUpDnControl(FASTFORWARD_PIN, PUD_UP);
  pullUpDnControl(PLAYPAUSE_PIN, PUD_UP);
  pullUpDnControl(REWIND_PIN, PUD_UP);
  pullUpDnControl(VOLUMEDOWN_PIN, PUD_UP);
  pullUpDnControl(ONOFF_PIN, PUD_UP);
  pullUpDnControl(VOLUMEUP_PIN, PUD_UP);
  wiringPiISR(FASTFORWARD_PIN, INT_EDGE_FALLING, &fastForwardInterrupt);
  wiringPiISR(PLAYPAUSE_PIN, INT_EDGE_FALLING, &playPauseInterrupt);
  wiringPiISR(REWIND_PIN, INT_EDGE_FALLING, &rewindInterrupt);
  wiringPiISR(VOLUMEDOWN_PIN, INT_EDGE_FALLING, &volumeDownInterrupt);
  wiringPiISR(ONOFF_PIN, INT_EDGE_FALLING, &onOffInterrupt);
  wiringPiISR(VOLUMEUP_PIN, INT_EDGE_FALLING, &volumeUpInterrupt);

  for(;;) {
    pthread_mutex_lock(&isr_mtx);
    while (pin_pressed == 0) {
      pthread_cond_wait(&isr_cond, &isr_mtx);
    }
    printf("Pressed %d\n", pin_pressed);
    pin_pressed = 0;
    pthread_mutex_unlock(&isr_mtx);
  }

  return 0;
}