# Path: plugins/io_base.py
# class IOBase:
#     def __init__(self, bus):
#         self.bus = bus
#         self.bus.subscribe(self.process_event, thread=False)
#         self.write_queue = Queue(10)
#         self.reader = threading.Thread(target=self.reader_thread)
#         self.reader.daemon = True
#         self.reader.start()
#         self.writer = threading.Thread(target=self.writer_thread)
#         self.writer.daemon = True
#         self.writer.start()
# 
#     def reader_thread(self):
#         raise NotImplementedError()
# 
#     def writer_thread(self):
#         raise NotImplementedError()
# 
#     def convert_data(self, data):
#         return data
# 
#     def process_event(self, ev):
#         try:
#             if ev.name == "leds_enabled":
#                 data = self.convert_data(ev.data)
#                 self.write_queue.put_nowait(data)
#         except queue.Full:
#             #TODO alert somehow without flooding?
#             pass


import time
import logging
import ctypes
import time
import RPi.GPIO as GPIO
import foos.config as config
import foos.process as process
from .io_base import IOBase

        self.pin = pin_number
        self.team = team
        if self.pin:
            #GPIO.setup(self.pin, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
            GPIO.setup(self.pin, GPIO.IN, pull_up_down = GPIO.PUD_UP)
            GPIO.add_event_detect(self.pin, GPIO.FALLING, callback=self.on_goal, bouncetime=10)
        else:
            logger.warn("Cannot init GoalDetector {0}, pin not specified".format(self.team))
    def __del__(self):
        if self.pin:
            GPIO.remove_event_detect(self.pin)
            
    def on_goal(self, channel):
        logger.info("Goal {}!".format(self.team))
        self.bus.notify('goal_event', {'source': 'rpi', 'team': self.team})

class IRBarrierPwmGenerator:
    def __init__(self):
        
        # https://sites.google.com/site/semilleroadt/raspberry-pi-tutorials/gpio:
        # "According to it, configure GPIO18 (WiringPi Pin 1)..."

        # Raspberry base PWM frequency: 19,200,000 Hz
        # Resulted frequency: base freq / 101 / 5 = 38.019 kHz
        # Signal duty cycle = 3/5 = ~60%
        process.call_and_log("gpio mode 1 pwm && gpio pwm-ms && gpio pwmr 5 && gpio pwmc 101 && gpio pwm 1 2", shell=True)
        
    def __del__(self):
        process.call_and_log("gpio pwm 1 0", shell=True)
  

# Path: plugins/io_base.py
# class IOBase:
#     def __init__(self, bus):
#         self.bus = bus
#         self.bus.subscribe(self.process_event, thread=False)
#         self.write_queue = Queue(10)
#         self.reader = threading.Thread(target=self.reader_thread)
#         self.reader.daemon = True
#         self.reader.start()
#         self.writer = threading.Thread(target=self.writer_thread)
#         self.writer.daemon = True
#         self.writer.start()
# 
#     def reader_thread(self):
#         raise NotImplementedError()
# 
#     def writer_thread(self):
#         raise NotImplementedError()
# 
#     def convert_data(self, data):
#         return data
# 
#     def process_event(self, ev):
#         try:
#             if ev.name == "leds_enabled":
#                 data = self.convert_data(ev.data)
#                 self.write_queue.put_nowait(data)
#         except queue.Full:
#             #TODO alert somehow without flooding?
#             pass


# Path: plugins/io_raspberry.py
import time
import logging
import ctypes
import time
import RPi.GPIO as GPIO
import foos.config as config
import foos.process as process
from .io_base import IOBase

        self.pin = pin_number
        self.team = team
        if self.pin:
            #GPIO.setup(self.pin, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
            GPIO.setup(self.pin, GPIO.IN, pull_up_down = GPIO.PUD_UP)
            GPIO.add_event_detect(self.pin, GPIO.FALLING, callback=self.on_goal, bouncetime=10)
        else:
            logger.warn("Cannot init GoalDetector {0}, pin not specified".format(self.team))
    def __del__(self):
        if self.pin:
            GPIO.remove_event_detect(self.pin)
            
    def on_goal(self, channel):
        logger.info("Goal {}!".format(self.team))
        self.bus.notify('goal_event', {'source': 'rpi', 'team': self.team})

class IRBarrierPwmGenerator:
    def __init__(self):
        
        # https://sites.google.com/site/semilleroadt/raspberry-pi-tutorials/gpio:
        # "According to it, configure GPIO18 (WiringPi Pin 1)..."

        # Raspberry base PWM frequency: 19,200,000 Hz
        # Resulted frequency: base freq / 101 / 5 = 38.019 kHz
        # Signal duty cycle = 3/5 = ~60%
        process.call_and_log("gpio mode 1 pwm && gpio pwm-ms && gpio pwmr 5 && gpio pwmc 101 && gpio pwm 1 2", shell=True)
        
    def __del__(self):
        process.call_and_log("gpio pwm 1 0", shell=True)
  

class Plugin(IOBase):
