def __init__( self, pixel_count, spi_port, spi_device ): """ Create a new controller for the WS2801 based lights Arguments: pixel_count {int} -- The total number of neopixels. Probably a multiple of 25. spi_port {int} -- The SPI port the neopixels are on. spi_device {int} -- The SPI device on the port that the neopixels are on. """ super().__init__(pixel_count) self.__leds__ = pixel_count if not local_debug.is_debug(): # Specify a hardware SPI connection on /dev/spidev0.0: self.__leds__ = Adafruit_WS2801.WS2801Pixels( pixel_count, spi=SPI.SpiDev(spi_port, spi_device)) # Clear all the pixels to turn them off. self.__leds__.clear() self.set_all([0, 0, 0])
def __initialize_modem__(self, retries=4, seconds_between_retries=10): """ Attempts to initialize the modem over the serial port. """ serial_connection = None if local_debug.is_debug(): return None while retries > 0 and serial_connection is None: try: self.__logger__.log_info_message( "Opening on " + self.__configuration__.cell_serial_port) serial_connection = serial.Serial( self.__configuration__.cell_serial_port, self.__configuration__.cell_baud_rate) except: self.__logger__.log_warning_message( "SERIAL DEVICE NOT LOCATED." + " Try changing /dev/ttyUSB0 to different USB port" + " (like /dev/ttyUSB1) in configuration file or" + " check to make sure device is connected correctly") # wait 60 seconds and check again time.sleep(seconds_between_retries) retries -= 1 return serial_connection
def __init__( self, pixel_count, gpio_pin, pixel_order ): """ Create a new controller for the WS2801 based lights Arguments: pixel_count {int} -- The total number of neopixels. gpio_pin {int} -- The GPIO pin the WS281x data pin is on. This is IO addressing, NOT physical addressing. pixel_order {str} -- The RGB or GRB order that colors are provided in. """ super().__init__(pixel_count) if not local_debug.is_debug(): from adafruit_blinka.microcontroller.bcm283x.pin import Pin self.__leds__ = neopixel.NeoPixel( Pin(gpio_pin), # board.D18, #gpio_pin, pixel_count, auto_write=False, pixel_order=pixel_order) # Clear all the pixels to turn them off. self.set_all([0, 0, 0])
def get_renderer() -> Renderer: """ Returns the renderer to use based on the type of LED lights given in the config. Returns: renderer -- Object that takes the colors and airport config and sets the LEDs. """ if local_debug.is_debug(): return Renderer(configuration.CONFIG[configuration.PIXEL_COUNT_KEY]) if configuration.get_mode() == configuration.WS2801: pixel_count = configuration.CONFIG[configuration.PIXEL_COUNT_KEY] spi_port = configuration.CONFIG[configuration.SPI_PORT_KEY] spi_device = configuration.CONFIG[configuration.SPI_DEVICE_KEY] return ws2801.Ws2801Renderer(pixel_count, spi_port, spi_device) elif configuration.get_mode() == configuration.WS281x: pixel_count = configuration.CONFIG[configuration.PIXEL_COUNT_KEY] gpio_pin = configuration.CONFIG[configuration.GPIO_PIN_KEY] safe_log("Setting up WS281x on Pin{} for {} lights".format( gpio_pin, pixel_count)) return ws281x.Ws281xRenderer( pixel_count, gpio_pin, configuration.get_pixel_order()) return Renderer(configuration.CONFIG[configuration.PIXEL_COUNT_KEY])
def __init__(self, pixel_count, spi_port, spi_device): """ Create a new controller for the WS2801 based lights Arguments: pixel_count {int} -- The total number of neopixels. Probably a multple of 25. spi_port {int} -- The SPI port the neopixels are on. spi_device {int} -- The SPI device on the port that the neopixels are on. """ self.pixel_count = pixel_count if not local_debug.is_debug(): # Specify a hardware SPI connection on /dev/spidev0.0: self.pixels = Adafruit_WS2801.WS2801Pixels( self.pixel_count, spi=SPI.SpiDev(spi_port, spi_device)) # Clear all the pixels to turn them off. self.pixels.clear() self.pixels.show() # Make sure to call show() after changing any pixels! for pixel in range(0, self.pixel_count): self.pixels.set_pixel(pixel, Adafruit_WS2801.RGB_to_color(0, 0, 0)) self.pixels.show()
def get_renderer(): """ Returns the renderer to use based on the type of LED lights given in the config. Returns: renderer -- Object that takes the colors and airport config and sets the LEDs. """ if local_debug.is_debug(): return None if configuration.get_mode() == configuration.WS2801: pixel_count = configuration.CONFIG["pixel_count"] spi_port = configuration.CONFIG["spi_port"] spi_device = configuration.CONFIG["spi_device"] return ws2801.Ws2801Renderer(pixel_count, spi_port, spi_device) elif configuration.get_mode() == configuration.WS281X: pixel_count = configuration.CONFIG["pixel_count"] gpio_pin = configuration.CONFIG["gpio_pin"] return ws281x.Ws281xRenderer(pixel_count, gpio_pin) elif configuration.get_mode() == configuration.PWM: return led_pwm.LedPwmRenderer(airport_render_config) else: # "Normal" LEDs return led.LedRenderer(airport_render_config)
def get_log_directory(self): """ returns the location of the logfile to use. """ if local_debug.is_debug(): return self.__config_parser__.get('SETTINGS', 'DEBUGGING_LOGFILE_DIRECTORY') return self.__config_parser__.get('SETTINGS', 'LOGFILE_DIRECTORY')
def __handle_key_event__(self, event): """ Handles a keyboard/keypad press event. Arguments: event {pygame.event} -- The event from the keyboard. Returns: bool -- True if the loop should continue, False if it should quit. """ if event.type == pygame.QUIT: utilities.shutdown() return False if event.type != pygame.KEYUP: return True if event.key in [pygame.K_ESCAPE]: utilities.shutdown(0) if not local_debug.is_debug(): self.__shutdown_stratux__() return False # Quit to terminal only. if event.key in [pygame.K_q]: return False if event.key in [pygame.K_KP_PLUS, pygame.K_PLUS]: self.__view_index__ += 1 if event.key in [pygame.K_KP_MINUS, pygame.K_MINUS]: self.__view_index__ -= 1 if event.key in [pygame.K_r]: self.render_perf.reset() self.orient_perf.reset() if event.key in [pygame.K_BACKSPACE]: self.__level_ahrs__() if event.key in [pygame.K_DELETE, pygame.K_PERIOD, pygame.K_KP_PERIOD]: targets.TARGET_MANAGER.clear_targets() if event.key in [pygame.K_RETURN, pygame.K_KP_ENTER]: orientation = self.__aircraft__.get_orientation() targets.TARGET_MANAGER.add_target(orientation.position[0], orientation.position[1], orientation.alt) targets.TARGET_MANAGER.save() if event.key in [pygame.K_EQUALS, pygame.K_KP_EQUALS]: self.__should_render_perf__ = not self.__should_render_perf__ return True
def set_led(self, airport_pins, color): """ Sets the color of a LED based on the pins for that LED Arguments: pins {array of int} -- And array of the pins that control the LED color {array of int} -- The values to set each to for the colors. """ if not local_debug.is_debug(): GPIO.setup(airport_pins, GPIO.OUT) GPIO.output(airport_pins, color)
def set_led(self, pixel_index, color): """ Sets the given airport to the given color Arguments: pixel_index {int} -- The index of the pixel to set color {int array} -- The RGB (0-255) array of the color we want to set. """ if not local_debug.is_debug(): self.pixels.set_pixel(pixel_index, Adafruit_WS2801.RGB_to_color( color[0], color[1], color[2])) self.pixels.show()
def __init__(self, airport_pins): """ Create a new PWM LED renderer. Arguments: airport_pins {dictionary} -- GPIO pins to PWM keyed by airport name. """ for airport in airport_pins: pins = airport_pins[airport] if not local_debug.is_debug(): GPIO.setup(pins, GPIO.OUT) GPIO.output(pins, (0, 0, 0))
def get_ip_address(): """ Returns the local IP address of this unit. Returns: tuple -- The IP address as a string and the color to render it in. """ try: if not local_debug.is_debug(): ip_addr = commands.getoutput('hostname -I').strip() return (ip_addr, GREEN) else: host_name = socket.gethostname() return (socket.gethostbyname(host_name), GREEN) except: return ('UNKNOWN', RED)
def __init__(self, buddy_configuration, logger): """ Initialize the object. """ self.__configuration__ = buddy_configuration self.__logger__ = logger self.__lcd__ = None self.__lcd_status_id__ = 0 self.__initialize_lcd__() self.__is_gas_detected__ = False self.__system_start_time__ = datetime.datetime.now() self.__sensors__ = Sensors(buddy_configuration) serial_connection = self.__initialize_modem__() if serial_connection is None and not local_debug.is_debug(): self.__logger__.log_warning_message( "Unable to initialize serial connection, quiting.") sys.exit() self.__fona_manager__ = FonaManager( self.__logger__, serial_connection, self.__configuration__.cell_power_status_pin, self.__configuration__.cell_ring_indicator_pin, self.__configuration__.utc_offset) # create heater relay instance self.__relay_controller__ = RelayManager( buddy_configuration, logger, self.__heater_turned_on_callback__, self.__heater_turned_off_callback__, self.__heater_max_time_off_callback__) self.__gas_sensor_queue__ = MPQueue() self.__logger__.log_info_message( "Starting SMS monitoring and heater service") self.__clear_existing_messages__() self.__logger__.log_info_message("Begin monitoring for SMS messages") self.__queue_message_to_all_numbers__( "HangarBuddy monitoring started." + "\n" + self.__get_help_status__()) self.__queue_message_to_all_numbers__(self.__get_full_status__()) self.__lcd__.clear() self.__lcd__.write(0, 0, "Ready")
def set_led(self, pixel_index, color): """ Sets the given airport to the given color Arguments: pixel_index {int or int array} -- The index of the pixel to set color {int array} -- The RGB (0-255) array of the color we want to set. """ if not local_debug.is_debug(): if (type(pixel_index) is not list): pixel_index = [pixel_index] for p in pixel_index: if (self.rgb_order == 'GRB'): self.pixels[p] = (color[1], color[0], color[2]) elif (self.rgb_order is 'RGB'): self.pixels[p] = (color[0], color[1], color[2]) self.pixels.show()
def __init__(self, pixel_count, gpio_port, rgb_order='GRB'): """ Create a new controller for the WS2811 based lights Arguments: pixel_count {int} -- The total number of neopixels gpio_port {string} -- The GPIO port the neopixels are on. Will be eval'd with board module """ self.pixel_count = pixel_count self.rgb_order = rgb_order if not local_debug.is_debug(): # Specify a hardware SPI connection on /dev/spidev0.0: self.pixels = neopixel.NeoPixel(eval( "board.D{}".format(gpio_port)), pixel_count, auto_write=False) # Clear all the pixels to turn them off. self.pixels.fill((0, 0, 0)) self.pixels.show()
""" Handles configuration loading and constants. """ import json import os import unicodedata import lib.local_debug as local_debug import weather if local_debug.is_debug(): HIGH = 1 LOW = 0 else: import RPi.GPIO as GPIO HIGH = GPIO.HIGH LOW = GPIO.LOW CONFIG_FILE = "./data/config.json" __working_dir__ = os.path.dirname(os.path.abspath(__file__)) __full_config__ = os.path.join(__working_dir__, os.path.normpath(CONFIG_FILE)) # Modes STANDARD = 'led' PWM = 'pwm' WS2801 = 'ws2801' with open(__full_config__) as config_file: config_text = config_file.read() CONFIG = json.loads(config_text)
import logging import logging.handlers import re import time import urllib import threading import configuration import lib.local_debug as local_debug import lib.colors as colors_lib import weather from lib.logger import Logger from lib.recurring_task import RecurringTask from renderers import led, led_pwm if not local_debug.is_debug(): from renderers import ws2801 airport_conditions = {} python_logger = logging.getLogger("weathermap") python_logger.setLevel(logging.DEBUG) LOGGER = Logger(python_logger) HANDLER = logging.handlers.RotatingFileHandler( "weathermap.log", maxBytes=10485760, backupCount=10) HANDLER.setFormatter(logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s')) python_logger.addHandler(HANDLER) thread_lock_object = threading.Lock()