Example #1
0
    def __init__(self,
                 ina,
                 inb,
                 enable,
                 ctrl,
                 enable_pwm=True,
                 gpio=GPIO.get_platform_gpio(),
                 pwm=PWM.get_platform_pwm()):
        # Save GPIO state and pin numbers
        self._en = enable
        self._ina = ina
        self._inb = inb
        self._ctrl = ctrl

        # Save libraries
        self._enable_pwm = enable_pwm
        self._pwm = pwm
        self._gpio = gpio

        # Setup all pins as outputs
        for pin in (ina, inb, enable):
            self._gpio.setup(pin, GPIO.OUT)

        # Setup the PWM speed control
        if (enable_pwm):
            self._pwm.start(ctrl, 0)
        else:
            gpio.setup(ctrl, GPIO.OUT)
            self._gpio.output(ctrl, False)

        # Enable Motor
        self._gpio.output(self._en, True)

        # Initialise the motor (stationary)
        self.setBrake(0)
Example #2
0
 def __init__(self,
              m1ina=VNH_SHIELD_M1INA,
              m1inb=VNH_SHIELD_M1INB,
              m1en=VNH_SHIELD_M1EN,
              m1pwm=VNH_SHIELD_M1PWM,
              m2ina=VNH_SHIELD_M2INA,
              m2inb=VNH_SHIELD_M2INB,
              m2en=VNH_SHIELD_M2EN,
              m2pwm=VNH_SHIELD_M2PWM,
              pwm_enabled=True,
              gpio=GPIO.get_platform_gpio(),
              pwm=PWM.get_platform_pwm()):
     # Initialise Motors
     # Left Motor
     self._M1 = Pololu_VNH5019(m1ina,
                               m1inb,
                               m1en,
                               m1pwm,
                               enable_pwm=pwm_enabled,
                               gpio=gpio,
                               pwm=pwm)
     # Right Motor
     self._M2 = Pololu_VNH5019(m2ina,
                               m2inb,
                               m2en,
                               m2pwm,
                               enable_pwm=pwm_enabled,
                               gpio=gpio,
                               pwm=pwm)
     self.motors = [self._M1, self._M2]
     # Initialise Motors
     self.setBrakes(0, 0)
     self._gpio = gpio
Example #3
0
    def __init__(self, rs, en, d4, d5, d6, d7, cols, lines, red, green, blue,
                 gpio=GPIO.get_platform_gpio(),
                 invert_polarity=True,
                 enable_pwm=False,
                 pwm=PWM.get_platform_pwm(),
                 initial_color=(1.0, 1.0, 1.0),
                 long_delay_us=DEFAULT_LONG_DELAY_US):
        """Initialize the LCD with RGB backlight.  RS, EN, and D4...D7 parameters
        should be the pins connected to the LCD RS, clock enable, and data line
        4 through 7 connections. The LCD will be used in its 4-bit mode so these
        6 lines are the only ones required to use the LCD.  You must also pass in
        the number of columns and lines on the LCD.

        The red, green, and blue parameters define the pins which are connected
        to the appropriate backlight LEDs.  The invert_polarity parameter is a
        boolean that controls if the LEDs are on with a LOW or HIGH signal.  By
        default invert_polarity is True, i.e. the backlight LEDs are on with a
        low signal.  If you want to enable PWM on the backlight LEDs (for finer
        control of colors) and the hardware supports PWM on the provided pins,
        set enable_pwm to True.  Finally you can set an explicit initial backlight
        color with the initial_color parameter.  The default initial color is
        white (all LEDs lit).

        You can optionally pass in an explicit GPIO class,
        for example if you want to use an MCP230xx GPIO extender.  If you don't
        pass in an GPIO instance, the default GPIO for the running platform will
        be used.
        """
        super(Adafruit_RGBCharLCD, self).__init__(rs, en, d4, d5, d6, d7,
                                                  cols,
                                                  lines,
                                                  enable_pwm=enable_pwm,
                                                  backlight=None,
                                                  invert_polarity=invert_polarity,
                                                  gpio=gpio,
                                                  pwm=pwm,
                                                  long_delay_us=long_delay_us)
        self._red = red
        self._green = green
        self._blue = blue
        # Setup backlight pins.
        if enable_pwm:
            # Determine initial backlight duty cycles.
            rdc, gdc, bdc = self._rgb_to_duty_cycle(initial_color)
            pwm.start(red, rdc)
            pwm.start(green, gdc)
            pwm.start(blue, bdc)
        else:
            gpio.setup(red, GPIO.OUT)
            gpio.setup(green, GPIO.OUT)
            gpio.setup(blue, GPIO.OUT)
            self._gpio.output_pins(self._rgb_to_pins(initial_color))
Example #4
0
    def __init__(self, rs, en, d4, d5, d6, d7, cols, lines, red, green, blue,
                 gpio=GPIO.get_platform_gpio(), 
                 invert_polarity=True,
                 enable_pwm=False,
                 pwm=PWM.get_platform_pwm(),
                 initial_color=(1.0, 1.0, 1.0)):
        """Initialize the LCD with RGB backlight.  RS, EN, and D4...D7 parameters 
        should be the pins connected to the LCD RS, clock enable, and data line 
        4 through 7 connections. The LCD will be used in its 4-bit mode so these 
        6 lines are the only ones required to use the LCD.  You must also pass in
        the number of columns and lines on the LCD.

        The red, green, and blue parameters define the pins which are connected
        to the appropriate backlight LEDs.  The invert_polarity parameter is a
        boolean that controls if the LEDs are on with a LOW or HIGH signal.  By
        default invert_polarity is True, i.e. the backlight LEDs are on with a
        low signal.  If you want to enable PWM on the backlight LEDs (for finer
        control of colors) and the hardware supports PWM on the provided pins,
        set enable_pwm to True.  Finally you can set an explicit initial backlight
        color with the initial_color parameter.  The default initial color is
        white (all LEDs lit).

        You can optionally pass in an explicit GPIO class,
        for example if you want to use an MCP230xx GPIO extender.  If you don't
        pass in an GPIO instance, the default GPIO for the running platform will
        be used.
        """
        super(Adafruit_RGBCharLCD, self).__init__(rs, en, d4, d5, d6, d7,
                                                  cols,
                                                  lines, 
                                                  enable_pwm=enable_pwm,
                                                  backlight=None,
                                                  invert_polarity=invert_polarity,
                                                  gpio=gpio, 
                                                  pwm=pwm)
        self._red = red
        self._green = green
        self._blue = blue
        # Setup backlight pins.
        if enable_pwm:
            # Determine initial backlight duty cycles.
            rdc, gdc, bdc = self._rgb_to_duty_cycle(initial_color)
            pwm.start(red, rdc)
            pwm.start(green, gdc)
            pwm.start(blue, bdc)
        else:
            gpio.setup(red, GPIO.OUT)
            gpio.setup(green, GPIO.OUT)
            gpio.setup(blue, GPIO.OUT)
            self._gpio.output_pins(self._rgb_to_pins(initial_color))
Example #5
0
def test_fan():
    print("[+] Starting Fan Test")
    fan1_frequency = 25000
    fan1_pin = config['pin_outs']['fan_1']
    fan2_frequency = 25000
    fan2_pin = config['pin_outs']['fan_2']

    pwm = PWMLib.get_platform_pwm()

    pwm.start(fan1_pin, globals.fan_speed, fan1_frequency)
    if (fan2_pin):
        pwm.start(fan2_pin, globals.fan_speed, fan2_frequency)

    while (globals.fan_speed != 100):
        print("[+] Fan Speed at {}%".format(globals.fan_speed))
        time.sleep(5)
        globals.fan_speed += 10
        pwm.set_duty_cycle(fan1_pin, globals.fan_speed)
        if (fan2_pin):
            pwm.set_duty_cycle(fan2_pin, globals.fan_speed)

    pwm.stop(fan1_pin)
    if (fan2_pin):
        pwm.stop(fan2_pin)
 def test_raspberrypi(self):
     pwm = PWM.get_platform_pwm()
     self.assertIsInstance(pwm, PWM.RPi_PWM_Adapter)
 def test_set_frequency(self):
     bbio_pwm = Mock()
     pwm = PWM.BBIO_PWM_Adapter(bbio_pwm)
     pwm.start('P9_16', 50)
     pwm.set_frequency('P9_16', 1000)
     bbio_pwm.set_frequency.assert_called_with('P9_16', 1000)
Example #8
0
def run_temperature_controller():

    globals.log('info', 'Temperature Controller Started')

    #Initialize Pid
    pid = PID(Kp=aggressive_kp,
              Ki=aggressive_ki,
              Kd=aggressive_kd,
              setpoint=globals.target_barrel_temp,
              output_limits=(0, 100),
              auto_mode=True,
              proportional_on_measurement=True
              )  # See documentation on proportial on measurement flag

    #Initialize temperature offset
    temperature_offset = 0

    if (simulated_mode == False):
        #Initialize Fan
        pwm = PWM.get_platform_pwm()
        pwm.start(fan1_pin, globals.fan_speed, fan1_frequency)

        #if a second fan is defined, initialize that as well
        if (fan2_pin):
            pwm.start(fan2_pin, globals.fan_speed, fan2_frequency)

        #if lid switch pin is defined, set it up as input
        if (lid_switch_pin):
            gpio_platform.setup(lid_switch_pin, GPIO.IN)

        #initialize thermocouples
        TC1 = MAX6675.MAX6675(CLK_pin, TC1_CS_pin, DO_pin)
        TC2 = MAX6675.MAX6675(CLK_pin, TC2_CS_pin, DO_pin)
        TC3 = MAX6675.MAX6675(CLK_pin, TC3_CS_pin, DO_pin)
        TC4 = MAX6675.MAX6675(CLK_pin, TC4_CS_pin, DO_pin)
        TC5 = MAX6675.MAX6675(CLK_pin, TC5_CS_pin, DO_pin)

        #Initialize Temperatures
        #read sensors
        TC1_temp = TC1.readTempC()
        time.sleep(0.1)
        TC2_temp = TC2.readTempC()
        time.sleep(0.1)
        TC3_temp = TC3.readTempC()
        time.sleep(0.1)
        TC4_temp = TC4.readTempC()
        time.sleep(0.1)
        TC5_temp = TC5.readTempC()

    else:
        #if in simulated mode, set to 20 degrees baseline
        TC1_temp = 20
        TC2_temp = 20
        TC3_temp = 20
        TC4_temp = 20
        TC5_temp = 20

        #and create empty pwm object
        pwm = None

    #set variables used in the loop
    TC5_temp_last = TC5_temp
    temp_weighted_avg_last = ((TC1_temp + TC2_temp + TC3_temp + TC4_temp) / 4)

    #keep looping until instructed otherwise
    while (globals.stop_threads == False):

        #update setpoint if it differs
        if (pid.setpoint != globals.target_barrel_temp):
            globals.log(
                'debug',
                'Temperature Controller - PID Setpoint changed to: {}'.format(
                    globals.target_barrel_temp))
            pid.setpoint = globals.target_barrel_temp

        #read sensors and if in simulated mode, calculate based on fan speed
        if (simulated_mode == False):
            TC1_temp = TC1.readTempC()
            time.sleep(0.1)
            TC2_temp = TC2.readTempC()
            time.sleep(0.1)
            TC3_temp = TC3.readTempC()
            time.sleep(0.1)
            TC4_temp = TC4.readTempC()
            time.sleep(0.1)
            TC5_temp = TC5.readTempC()
        else:
            simulated_temp = simulate_temperature(TC1_temp)
            TC1_temp = TC2_temp = TC3_temp = TC4_temp = simulated_temp
            TC5_temp += 0.01

        #Calculations
        temp_weighted_avg = (
            (TC1_temp + TC2_temp + TC3_temp + TC4_temp) / 4
        ) + globals.temperature_offset  # + 45 From SmokeyTheBarrel: temperature compensation between outside and center of the barrel
        temp_weighted_avg_last = (2 * temp_weighted_avg_last +
                                  temp_weighted_avg) / 3
        globals.current_barrel_temp = temp_weighted_avg_last
        temperature_gap = globals.target_barrel_temp - temp_weighted_avg_last
        globals.current_temp_gap = temperature_gap

        TC5_temp_last = (2 * TC5_temp_last + TC5_temp) / 3
        globals.current_meat_temp = TC5_temp_last

        #use conservative pid profile for small temperature differences
        if (temperature_gap >= -10 and temperature_gap <= 10):
            set_pid_profile(pid, 'conservative')

        #otherwise use the aggresive profile
        else:
            set_pid_profile(pid, 'aggressive')

        #if lid switch pin is defined, check its state. If the lid is open, kill the fan. If the pin is not defined calculate and set the speed as required
        if (lid_switch_pin):
            lid_state = gpio_platform.input(lid_switch_pin)

            if (lid_state == GPIO.HIGH):
                set_fan_speed(pwm, 0)
                globals.lid_open = True
            else:
                #calculate new fan speed and set the fan speed
                #only do the pid calculation when the lid is closed to prevent confusing the pid
                pid_output = pid(temp_weighted_avg_last)
                set_fan_speed(pwm, pid_output)
                globals.lid_open = False

        else:
            #calculate new fan speed and set the fan speed
            pid_output = pid(temp_weighted_avg_last)
            set_fan_speed(pwm, pid_output)

        #if the calibrate option has been selected
        if (globals.calibrate_temperature == True):

            globals.log(
                'info',
                'Temperature Calibration Started, stopping PID controller')

            #stop the pid
            pid.auto_mode = False

            #run the calibrate function
            globals.temperature_offset = calibrate_temperature_offset(
                TC1, TC2, TC3, TC4, TC5, pwm)

            #reenable the pid
            pid.auto_mode = True
            globals.log(
                'info',
                'Temperature Calibration Finished, PID controller started')

        #sleep for a second
        time.sleep(1)

        ###### End of loop

    if (simulated_mode == False):
        pwm.stop(fan1_pin)
        if (fan2_pin):
            pwm.stop(fan2_pin)

    globals.log('info', 'Temperature Controller Stopped')
Example #9
0
#!/usr/bin/python

import Tkinter as tk
from Tkinter import *
import Adafruit_GPIO as GPIO
import Adafruit_GPIO.PWM as PWM

gpio = GPIO.get_platform_gpio()
pwm = PWM.get_platform_pwm()

MAINPIN = 19
RELIEFPIN = 16
VENTPIN = 26
IGNITIONPIN = 20
#GPIO.setmode(GPIO.BCM)


class MainApp:
    def __init__(self, master):
        #Check Buttons
        self.propulsion = BooleanVar()
        self.mission_control = BooleanVar()
        self.safety = BooleanVar()

        #Fill States
        self.fill_state = BooleanVar()
        self.relief_state = BooleanVar()
        self.manual_state = BooleanVar()

        self.propulsion.set(0)
        self.mission_control.set(0)
 def test_raspberrypi(self):
     pwm = PWM.get_platform_pwm()
     self.assertIsInstance(pwm, PWM.RPi_PWM_Adapter)
 def test_set_duty_cycle_valid(self):
     rpi_gpio = Mock()
     pwm = PWM.RPi_PWM_Adapter(rpi_gpio)
     pwm.start(1, 50)
     pwm.set_duty_cycle(1, 75)
 def test_setup(self):
     rpi_gpio = Mock()
     pwm = PWM.RPi_PWM_Adapter(rpi_gpio)
     pwm.start(1, 50)
     rpi_gpio.PWM.assert_called_with(1, 2000)
 def test_beagleboneblack(self):
     pwm = PWM.get_platform_pwm()
     self.assertIsInstance(pwm, PWM.BBIO_PWM_Adapter)
Example #14
0
app = create_app()

create_app()
try:
    socketio = SocketIO(app, async_mode=async_mode)
except:
    # Needed for sphinx documentation
    socketio = SocketIO(app)

ws_thread = None
hb_thread = None
payload = None

try:
    pwm = pwmLib.get_platform_pwm(pwmtype="softpwm")
    gpio = gpioLib.get_platform_gpio()
except NameError:
    print "Adafruit_GPIO lib is unavailable"

DEFAULT_SOFTPWM_FREQ = 100

binary_sensors = []

class BinarySensor:
    """
    The binary sensor object contains information for each binary sensor.

    :param name:
        The human readable name of the sensor
    :param pin:
Example #15
0
def run_temperature_controller():
    
    globals.log('info', 'Temperature Controller Started')

    #Initialize Pid
    pid = PID(Kp=aggressive_kp, Ki=aggressive_ki, Kd=aggressive_kd, setpoint=globals.target_barrel_temp, output_limits=(0, 100), auto_mode=True, proportional_on_measurement=False)

    if (simulated_mode == False):
        #Initialize Fan
        pwm = PWMLib.get_platform_pwm()
        pwm.start(fan1_pin, globals.fan_speed, fan1_frequency)

        #if a second fan is defined, initialize that as well
        if(fan2_pin) :
            pwm.start(fan2_pin, globals.fan_speed, fan2_frequency)

        #initialize thermocouples
        TC1 = MAX6675.MAX6675(CLK_pin, TC1_CS_pin, DO_pin)
        TC2 = MAX6675.MAX6675(CLK_pin, TC2_CS_pin, DO_pin)
        TC3 = MAX6675.MAX6675(CLK_pin, TC3_CS_pin, DO_pin)
        TC4 = MAX6675.MAX6675(CLK_pin, TC4_CS_pin, DO_pin)
        TC5 = MAX6675.MAX6675(CLK_pin, TC5_CS_pin, DO_pin)

        #Initialize Temperatures
        #read sensors
        TC1_temp = TC1.readTempC()
        time.sleep(0.1)
        TC2_temp = TC2.readTempC()
        time.sleep(0.1)
        TC3_temp = TC3.readTempC()
        time.sleep(0.1)
        TC4_temp = TC4.readTempC()
        time.sleep(0.1)
        TC5_temp = TC5.readTempC()

    else:
        #if in simulated mode, set to 20 degrees baseline
        TC1_temp = 20
        TC2_temp = 20
        TC3_temp = 20
        TC4_temp = 20
        TC5_temp = 20

        #and create empty pwm object
        pwm = None

    #set variables used in the loop
    TC5_temp_last = TC5_temp
    temp_weighted_avg_last = ((TC1_temp + TC2_temp + TC3_temp + TC4_temp) / 4)

    #keep looping until instructed otherwise
    while(globals.stop_threads == False):
        #update setpoint if it differs
        if (pid.setpoint != globals.target_barrel_temp):
            globals.log('debug', 'Temperature Controller - PID Setpoint changed to: {}'.format(globals.target_barrel_temp))
            pid.setpoint = globals.target_barrel_temp

        #read sensors and if in simulated mode, calculate based on fan speed
        if (simulated_mode == False):
            TC1_temp = TC1.readTempC()
            time.sleep(0.1)
            TC2_temp = TC2.readTempC()
            time.sleep(0.1)
            TC3_temp = TC3.readTempC()
            time.sleep(0.1)
            TC4_temp = TC4.readTempC()
            time.sleep(0.1)
            TC5_temp = TC5.readTempC()
        else:
            simulated_temp = simulate_temperature(TC1_temp)
            TC1_temp = TC2_temp = TC3_temp = TC4_temp = simulated_temp
            TC5_temp += 0.01

        #Calculations
        temp_weighted_avg = ((TC1_temp + TC2_temp + TC3_temp + TC4_temp) / 4) # + 45 From SmokeyTheBarrel: temperature compensation between outside and center of the barrel
        temp_weighted_avg_last=(2 * temp_weighted_avg_last + temp_weighted_avg) / 3
        globals.current_barrel_temp = temp_weighted_avg_last
        temperature_gap = globals.target_barrel_temp - temp_weighted_avg_last
        globals.current_temp_gap = temperature_gap

        TC5_temp_last=(2 * TC5_temp_last + TC5_temp)/3;
        globals.current_meat_temp = TC5_temp_last

        #use conservative pid profile for small temperature differences
        if (temperature_gap >= -10  and temperature_gap <= 10):
            set_pid_profile(pid, 'conservative')

        #otherwise use the aggresive profile
        else:
            set_pid_profile(pid, 'aggressive')

        #calculate new fan speed and set the fan speed
        pid_output = pid(temp_weighted_avg_last)
        set_fan_speed(pwm, pid_output)

        #sleep for a second
        time.sleep(1)

    if (simulated_mode == False):
        pwm.stop(fan1_pin)
        if (fan2_pin):
            pwm.stop(fan2_pin)

    globals.log('info', 'Temperature Controller Stopped')
Example #16
0
 def __init__(self, pin):
     self._pin = pin
     self._motor = PWM.BBIO_PWM_Adapter(PWM.get_platform_pwm())
     self._motor.set_frequency(self._pin, self._freq)
Example #17
0
import Adafruit_GPIO.PWM as pwmLib
from time import time, sleep

#pwm = pwmLib.get_platform_pwm()
pwm = pwmLib.CHIP_PWM_Adapter()
pwm.start(5, 3, 1)
pwm.start(6, 15, 1)
sleep(3)
pwm.set_duty_cycle(5, 12)
pwm.set_frequency(5, 0.2)
sleep(10)
pwm.stop(5);
pwm.stop(6);
 def test_set_duty_cycle_invalid(self):
     rpi_gpio = Mock()
     pwm = PWM.RPi_PWM_Adapter(rpi_gpio)
     pwm.start(1, 50)
     self.assertRaises(ValueError, pwm.set_duty_cycle, 1, 150)
     self.assertRaises(ValueError, pwm.set_duty_cycle, 1, -10)
Example #19
0
    def __init__(self, rs, en, d4, d5, d6, d7, cols, lines, backlight=None,
                    invert_polarity=True,
                    enable_pwm=False,
                    gpio=GPIO.get_platform_gpio(),
                    pwm=PWM.get_platform_pwm(),
                    initial_backlight=1.0):
        """Initialize the LCD.  RS, EN, and D4...D7 parameters should be the pins
        connected to the LCD RS, clock enable, and data line 4 through 7 connections.
        The LCD will be used in its 4-bit mode so these 6 lines are the only ones
        required to use the LCD.  You must also pass in the number of columns and
        lines on the LCD.  

        If you would like to control the backlight, pass in the pin connected to
        the backlight with the backlight parameter.  The invert_polarity boolean
        controls if the backlight is one with a LOW signal or HIGH signal.  The 
        default invert_polarity value is True, i.e. the backlight is on with a
        LOW signal.  

        You can enable PWM of the backlight pin to have finer control on the 
        brightness.  To enable PWM make sure your hardware supports PWM on the 
        provided backlight pin and set enable_pwm to True (the default is False).
        The appropriate PWM library will be used depending on the platform, but
        you can provide an explicit one with the pwm parameter.

        The initial state of the backlight is ON, but you can set it to an 
        explicit initial state with the initial_backlight parameter (0 is off,
        1 is on/full bright).

        You can optionally pass in an explicit GPIO class,
        for example if you want to use an MCP230xx GPIO extender.  If you don't
        pass in an GPIO instance, the default GPIO for the running platform will
        be used.
        """
        # Save column and line state.
        self._cols = cols
        self._lines = lines
        # Save GPIO state and pin numbers.
        self._gpio = gpio
        self._rs = rs
        self._en = en
        self._d4 = d4
        self._d5 = d5
        self._d6 = d6
        self._d7 = d7
        # Save backlight state.
        self._backlight = backlight
        self._pwm_enabled = enable_pwm
        self._pwm = pwm
        self._blpol = not invert_polarity
        # Setup all pins as outputs.
        for pin in (rs, en, d4, d5, d6, d7):
            gpio.setup(pin, GPIO.OUT)
        # Setup backlight.
        if backlight is not None:
            if enable_pwm:
                pwm.start(backlight, self._pwm_duty_cycle(initial_backlight))
            else:
                gpio.setup(backlight, GPIO.OUT)
                gpio.output(backlight, self._blpol if initial_backlight else not self._blpol)
        # Initialize the display.
        self.write8(0x33)
        self.write8(0x32)
        # Initialize display control, function, and mode registers.
        self.displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF
        self.displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_2LINE | LCD_5x8DOTS
        self.displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT
        # Write registers.
        self.write8(LCD_DISPLAYCONTROL | self.displaycontrol)
        self.write8(LCD_FUNCTIONSET | self.displayfunction)
        self.write8(LCD_ENTRYMODESET | self.displaymode)  # set the entry mode
        self.clear()
 def test_set_frequency(self):
     rpi_gpio = Mock()
     pwm = PWM.RPi_PWM_Adapter(rpi_gpio)
     pwm.start(1, 50)
     pwm.set_frequency(1, 1000)
 def test_beagleboneblack(self):
     pwm = PWM.get_platform_pwm()
     self.assertIsInstance(pwm, PWM.BBIO_PWM_Adapter)
 def test_setup(self):
     bbio_pwm = Mock()
     pwm = PWM.BBIO_PWM_Adapter(bbio_pwm)
     pwm.start('P9_16', 50)
     bbio_pwm.start.assert_called_with('P9_16', 50, 2000)
Example #23
0
# lcd_columns = 20
# lcd_rows    = 4

# Initialize GPIO
gpio = GPIO.get_platform_gpio()
# Initialize One Wire Thermal Sensor
sensor = W1ThermSensor()

# Initialize the LCD using the pins
lcd = LCD.Adafruit_RGBCharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7,
                              lcd_columns,
                              lcd_rows, lcd_red, lcd_green, lcd_blue,
                              gpio=GPIO.get_platform_gpio(),
                              invert_polarity=True,
                              enable_pwm=True,
                              pwm=PWM.get_platform_pwm(pwmtype="softpwm"),
                              initial_color=(1.0, 1.0, 1.0))


# Blue startup screen
lcd.set_color(0.0, 0.0, 1.0)
lcd.clear()

# Create Farenheight Degree Symbol
lcd.create_char(1, [0b11000,
                    0b11000,
                    0b00000,
                    0b00111,
                    0b00100,
                    0b00110,
                    0b00100,
 def test_set_duty_cycle_valid(self):
     bbio_pwm = Mock()
     pwm = PWM.BBIO_PWM_Adapter(bbio_pwm)
     pwm.start('P9_16', 50)
     pwm.set_duty_cycle('P9_16', 75)
     bbio_pwm.set_duty_cycle.assert_called_with('P9_16', 75)
    def __init__(self, rs, en, d4, d5, d6, d7, cols, lines, backlight=None,
                    invert_polarity=True,
                    enable_pwm=False,
                    gpio=GPIO.get_platform_gpio(),
                    pwm=PWM.get_platform_pwm(),
                    initial_backlight=1.0):
        """Initialize the LCD.  RS, EN, and D4...D7 parameters should be the pins
        connected to the LCD RS, clock enable, and data line 4 through 7 connections.
        The LCD will be used in its 4-bit mode so these 6 lines are the only ones
        required to use the LCD.  You must also pass in the number of columns and
        lines on the LCD.  

        If you would like to control the backlight, pass in the pin connected to
        the backlight with the backlight parameter.  The invert_polarity boolean
        controls if the backlight is one with a LOW signal or HIGH signal.  The 
        default invert_polarity value is True, i.e. the backlight is on with a
        LOW signal.  

        You can enable PWM of the backlight pin to have finer control on the 
        brightness.  To enable PWM make sure your hardware supports PWM on the 
        provided backlight pin and set enable_pwm to True (the default is False).
        The appropriate PWM library will be used depending on the platform, but
        you can provide an explicit one with the pwm parameter.

        The initial state of the backlight is ON, but you can set it to an 
        explicit initial state with the initial_backlight parameter (0 is off,
        1 is on/full bright).

        You can optionally pass in an explicit GPIO class,
        for example if you want to use an MCP230xx GPIO extender.  If you don't
        pass in an GPIO instance, the default GPIO for the running platform will
        be used.
        """
        # Save column and line state.
        self._cols = cols
        self._lines = lines
        # Save GPIO state and pin numbers.
        self._gpio = gpio
        self._rs = rs
        self._en = en
        self._d4 = d4
        self._d5 = d5
        self._d6 = d6
        self._d7 = d7
        # Save backlight state.
        self._backlight = backlight
        self._pwm_enabled = enable_pwm
        self._pwm = pwm
        self._blpol = not invert_polarity
        # Setup all pins as outputs.
        for pin in (rs, en, d4, d5, d6, d7):
            gpio.setup(pin, GPIO.OUT)
        # Setup backlight.
        if backlight is not None:
            if enable_pwm:
                pwm.start(backlight, self._pwm_duty_cycle(initial_backlight))
            else:
                gpio.setup(backlight, GPIO.OUT)
                gpio.output(backlight, self._blpol if initial_backlight else not self._blpol)
        # Initialize the display.
        self.write8(0x33)
        self.write8(0x32)
        # Initialize display control, function, and mode registers.
        self.displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF
        self.displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_2LINE | LCD_5x8DOTS
        self.displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT
        # Write registers.
        self.write8(LCD_DISPLAYCONTROL | self.displaycontrol)
        self.write8(LCD_FUNCTIONSET | self.displayfunction)
        self.write8(LCD_ENTRYMODESET | self.displaymode)  # set the entry mode
        self.clear()
 def test_set_duty_cycle_invalid(self):
     bbio_pwm = Mock()
     pwm = PWM.BBIO_PWM_Adapter(bbio_pwm)
     pwm.start('P9_16', 50)
     self.assertRaises(ValueError, pwm.set_duty_cycle, 'P9_16', 150)
     self.assertRaises(ValueError, pwm.set_duty_cycle, 'P9_16', -10)