#!/usr/bin/perl
#################################################
# Package for controlling the radio controls at #
# the Raspberry Pi GPIO port                    #
# Requires Inline::C & WiringPi                 #
#################################################

package WLAN::Radio::Controls;

# Loading C inline code
use Inline (
    C => "DATA",
    (( LIBS => '-lwiringPi', )),
    ( () )
);

Inline->init();

# Constructor 
sub new {
   my $invocant = shift;
   
   my $self = {};
   my $class=ref($invocant) || $invocant;
   
   $self->{'source'} = 0;
   $self->{'btn_left'} = 0;
   $self->{'btn_right'} = 0; 
   $self->{'btn_ok'} = 0;
   $self->{'btn_cancel'} = 0;  

   $self->{'gpio'} = {
	"btn_ok" => 7,
	"btn_cancel" => 0,
	"btn_left" => 1,
	"btn_right" => 2
   };

   #$self->{'max_lines'} = 2;
   setupRadioControls(); 
   bless $self, $class;
   return $self;
}

#
# Checks the button state and returns the button, that was clicked
# Also updates the source according to the source selector
#
sub checkState {
   my $self = shift;
   my $event = "";
   my $source = getSource();
   if ($source != $self->{'source'}) {
      $event = "source"
   }
   foreach my $button (keys %{$self->{'gpio'}}) {
	my $state = getButtonState($self->{'gpio'}->{$button});
        if ($state >= 0) {
	   if (($state != $self->{$button}) && ($state == 1) && ($event eq "")) {
              $event = $button;
           } 
           $self->{$button} = $state;
        }
   }
   $self->{'source'} = $source;
   return($event);
}

sub setSource {
   my $self = shift;
   my $source = shift;
   showSource ($source)
}


1;

__DATA__
 
__C__

#include <wiringPi.h>
#include <stdio.h>
#define SOURCE_SWITCH_1 8
#define SOURCE_SWITCH_2 9
#define BTN_OK 7
#define BTN_CANCEL 0
#define BTN_LEFT 1
#define BTN_RIGHT 2
#define SOURCE_RADIO 0
#define SOURCE_PODCAST 1
#define SOURCE_MP3 2
#define LED_1 3
#define LED_2 4
#define LED_3 5



int getButtonState (int button) {
  if ((button == BTN_OK) || (button == BTN_CANCEL) || (button == BTN_LEFT) || (button == BTN_RIGHT)) {
      int value = digitalRead(button);
      if (button == BTN_CANCEL) {return(value);}
      return ((value == 1) ? 0 : 1);
  }
  /* Not allowed */
  return (-1);
}


int setupRadioControls () {
   wiringPiSetup () ;
   pinMode (SOURCE_SWITCH_1, INPUT);
   pinMode (SOURCE_SWITCH_2, INPUT); 
   pinMode (BTN_OK, INPUT);
   pinMode (BTN_CANCEL, INPUT);
   pinMode (BTN_LEFT, INPUT);
   pinMode (BTN_RIGHT, INPUT);
   pinMode (LED_1, OUTPUT);
   pinMode (LED_2, OUTPUT);
   pinMode (LED_3, OUTPUT);
   digitalWrite(LED_1, LOW);
   digitalWrite(LED_2, LOW);
   digitalWrite(LED_3, LOW);
}

int getSource () {
  int sw1 = digitalRead(SOURCE_SWITCH_1);
  int sw2 = digitalRead(SOURCE_SWITCH_2);
  if ((sw1 == 1) && (sw2 == 0)) {
     digitalWrite(LED_1, HIGH);
     digitalWrite(LED_2, LOW);
     digitalWrite(LED_3, LOW);
     return SOURCE_RADIO;
  }
  if ((sw1 == 0) && (sw2 == 1)) {
     digitalWrite(LED_1, LOW);
     digitalWrite(LED_2, HIGH);
     digitalWrite(LED_3, LOW); 
    return SOURCE_PODCAST;
  }
   digitalWrite(LED_1, LOW);
     digitalWrite(LED_2, LOW);
     digitalWrite(LED_3, HIGH);
  return SOURCE_MP3;
}
  
void showSource (int source) {
   if (source == SOURCE_RADIO) {
     digitalWrite(LED_1, HIGH);
     digitalWrite(LED_2, LOW);
     digitalWrite(LED_3, LOW);
     return;
   }
   if (source == SOURCE_PODCAST) {
     digitalWrite(LED_1, LOW);
     digitalWrite(LED_2, HIGH);
     digitalWrite(LED_3, LOW);
     return;
   }
   if (source == SOURCE_MP3) {
     digitalWrite(LED_1, LOW);
     digitalWrite(LED_2, LOW);
     digitalWrite(LED_3, HIGH);
   }
}
