def main(): """Initialize SPI, send a few bytes, then close and exit.""" WATCH_DOG = 85 WATCH_DOG_REPLY = 170 DUMMY = 255 print 'initializing GPIO:', soc.bcm2835_init() # Reset the AVR and the force GPIO8, pin 24, to high to enable the AVR soc.bcm2835_gpio_fsel(soc.RPI_GPIO_P1_24, soc.BCM2835_GPIO_FSEL_OUTP) soc.bcm2835_gpio_set(soc.RPI_GPIO_P1_24) soc.bcm2835_gpio_clr(soc.RPI_GPIO_P1_24) soc.bcm2835_gpio_set(soc.RPI_GPIO_P1_24) time.sleep(10) print 'initializing SPI:', soc.bcm2835_spi_begin() soc.bcm2835_spi_setBitOrder(soc.BCM2835_SPI_BIT_ORDER_MSBFIRST) soc.bcm2835_spi_setDataMode(soc.BCM2835_SPI_MODE0) soc.bcm2835_spi_setClockDivider(soc.BCM2835_SPI_CLOCK_DIVIDER_65536) soc.bcm2835_spi_chipSelect(soc.BCM2835_SPI_CS1) soc.bcm2835_spi_setChipSelectPolarity(soc.BCM2835_SPI_CS0, soc.LOW) while (True): time.sleep(1) soc.bcm2835_spi_transfer(WATCH_DOG) # send command data_byte = soc.bcm2835_spi_transfer(DUMMY) # read response if data_byte != WATCH_DOG_REPLY: print 'Watchdog reply missing. received: ', data_byte break soc.bcm2835_spi_end() print 'closing GPIO:', soc.bcm2835_close()
def watchdog(param={}): """Function that sends SPI commands to reset AVR controller watchdog time-out period.""" soc.bcm2835_spi_transfer(SPI_CMD_WDOG) data_byte = soc.bcm2835_spi_transfer(DUMMY) if data_byte != WATCH_DOG_REPLY: # TODO is an AVR reset too harsh? _avr_reset()
def _scroll_rtl(digits=(0, 0, 0, 0), digit_delay=1.0): """Scroll the digits into the display shifting them from right to left.""" for shift in range(0, 4): cmd = SPI_CMD_MINUTES digit_out = digits[shift] for d in range(0, 4): soc.bcm2835_spi_transfer(cmd) digit_in = soc.bcm2835_spi_transfer(digit_out) digit_out = digit_in cmd = cmd + 1 watchdog() time.sleep(digit_delay)
def _get_brightness(): """Read light sensor then calculate and return brightness command value between 1 and 10.""" # Get light sensor value, which can be between 0 and 255 soc.bcm2835_spi_transfer(SPI_CMD_GET_LIGHT) light_sensor = soc.bcm2835_spi_transfer(DUMMY) br_cmd = int(light_sensor / 20.0) if br_cmd > 10: br_cmd = 10 elif br_cmd < 1: br_cmd = 1 return br_cmd
def main(): """Initialize SPI, send a few bytes, then close and exit.""" print 'initializing GPIO:', soc.bcm2835_init() print 'initializing SPI:', soc.bcm2835_spi_begin() soc.bcm2835_spi_setBitOrder(soc.BCM2835_SPI_BIT_ORDER_MSBFIRST) soc.bcm2835_spi_setDataMode(soc.BCM2835_SPI_MODE0) soc.bcm2835_spi_setClockDivider(soc.BCM2835_SPI_CLOCK_DIVIDER_65536) soc.bcm2835_spi_chipSelect(soc.BCM2835_SPI_CS1) soc.bcm2835_spi_setChipSelectPolarity(soc.BCM2835_SPI_CS0, soc.LOW) for i in range(0, 100): data_byte = soc.bcm2835_spi_transfer(i) print 'sent:', i, 'returned:', data_byte soc.bcm2835_spi_end() print 'closing GPIO:', soc.bcm2835_close()
def _display(digits=(0, 0, 0, 0), brightness=-1): """ Send digits passed in a tuple, one number per Nixie tube. Integers '0' to '9' correspond to number digits. Integer '10' (DIGIT_OFF) signals a blank 'off' digit. Integer '-1' signals skip digit update. 'brightness' of -1 skips display brightness change. """ # Send brightness command if brightness > -1: if brightness > 10: brightness = 10 soc.bcm2835_spi_transfer(SPI_CMD_BRIGHTNESS) soc.bcm2835_spi_transfer(int(brightness)) # Send digits cmd = SPI_CMD_TENS_HOURS for d in range(0, 4): if (digits[d] >= 0 and digits[d] <= 9) or (digits[d] == DIGIT_OFF): soc.bcm2835_spi_transfer(cmd) data_byte = soc.bcm2835_spi_transfer(digits[d]) cmd = cmd - 1
def test_spi(): send_data = 0x10 read_data = io.bcm2835_spi_transfer(send_data) print("Sent to SPI: 0x{0:2X}. Read back from SPI: 0x{1:2X}.".format(send_data, read_data)) if not send_data == read_data: print("Do you have the loopback from MOSI to MISO connected?")
def main(): """Initialize SPI, send a few bytes, then close and exit.""" SPI_CMD_WDOG = 85 WATCH_DOG_REPLY = 170 SPI_CMD_MINUTES = 1 SPI_CMD_TENS_MINUTES = 2 SPI_CMD_HOURS = 3 SPI_CMD_TENS_HOURS = 4 DUMMY = 255 print 'initializing GPIO:', soc.bcm2835_init() # Reset the AVR and the force GPIO8, pin 24, to high to enable the AVR soc.bcm2835_gpio_fsel(soc.RPI_GPIO_P1_24, soc.BCM2835_GPIO_FSEL_OUTP) soc.bcm2835_gpio_set(soc.RPI_GPIO_P1_24) soc.bcm2835_gpio_clr(soc.RPI_GPIO_P1_24) soc.bcm2835_gpio_set(soc.RPI_GPIO_P1_24) time.sleep(1) print 'initializing SPI:', soc.bcm2835_spi_begin() soc.bcm2835_spi_setBitOrder(soc.BCM2835_SPI_BIT_ORDER_MSBFIRST) soc.bcm2835_spi_setDataMode(soc.BCM2835_SPI_MODE0) soc.bcm2835_spi_setClockDivider(soc.BCM2835_SPI_CLOCK_DIVIDER_65536) soc.bcm2835_spi_chipSelect(soc.BCM2835_SPI_CS1) soc.bcm2835_spi_setChipSelectPolarity(soc.BCM2835_SPI_CS0, soc.LOW) while (True): for i in range(0, 5): time.sleep(1) soc.bcm2835_spi_transfer(SPI_CMD_MINUTES) # send command data_byte = soc.bcm2835_spi_transfer(i) # send digit soc.bcm2835_spi_transfer(SPI_CMD_TENS_MINUTES) # send command data_byte = soc.bcm2835_spi_transfer(i * 2) # send digit soc.bcm2835_spi_transfer(SPI_CMD_HOURS) # send command data_byte = soc.bcm2835_spi_transfer((i * 2) + 1) # send digit soc.bcm2835_spi_transfer(SPI_CMD_TENS_HOURS) # send command data_byte = soc.bcm2835_spi_transfer(i + 5) # send digit print 'cycle: ', i # Periodic watch-dog 'keep alive' soc.bcm2835_spi_transfer(SPI_CMD_WDOG) # send command data_byte = soc.bcm2835_spi_transfer(DUMMY) # read response if data_byte != WATCH_DOG_REPLY: print 'Watchdog reply missing. received: ', data_byte break soc.bcm2835_spi_end() print 'closing GPIO:', soc.bcm2835_close()
def main(): """Initialize SPI, send a few bytes, then close and exit.""" SPI_CMD_MINUTES = 1 SPI_CMD_TENS_MINUTES = 2 SPI_CMD_HOURS = 3 SPI_CMD_TENS_HOURS = 4 SPI_CMD_BRIGHTNESS = 5 SPI_CMD_GET_LIGHT = 6 SPI_CMD_WDOG = 85 WATCH_DOG_REPLY = 170 DUMMY = 255 # Brightness value filter br = [0, 0, 0, 0, 0] print 'initializing GPIO:', soc.bcm2835_init() # Reset the AVR and the force GPIO8, pin 24, to high to enable the AVR soc.bcm2835_gpio_fsel(soc.RPI_GPIO_P1_24, soc.BCM2835_GPIO_FSEL_OUTP) soc.bcm2835_gpio_set(soc.RPI_GPIO_P1_24) soc.bcm2835_gpio_clr(soc.RPI_GPIO_P1_24) soc.bcm2835_gpio_set(soc.RPI_GPIO_P1_24) print 'initializing SPI:', soc.bcm2835_spi_begin() soc.bcm2835_spi_setBitOrder(soc.BCM2835_SPI_BIT_ORDER_MSBFIRST) soc.bcm2835_spi_setDataMode(soc.BCM2835_SPI_MODE0) soc.bcm2835_spi_setClockDivider(soc.BCM2835_SPI_CLOCK_DIVIDER_65536) soc.bcm2835_spi_chipSelect(soc.BCM2835_SPI_CS1) soc.bcm2835_spi_setChipSelectPolarity(soc.BCM2835_SPI_CS0, soc.LOW) while (True): for i in range(0, 10): time.sleep(0.5) # Get light sensor value, which can be between 0 and 255 soc.bcm2835_spi_transfer(SPI_CMD_GET_LIGHT) # send command light_sensor = soc.bcm2835_spi_transfer(DUMMY) # read response # Convert light sensor input to a 0 to 10 brightness range br.insert(0, int(light_sensor / 20.0)) br = br[:5] brightness = (br[0] + br[1] + br[2] + br[3] + br[4]) / 5 if brightness > 10.0: brightness = 10.0 elif brightness < 1: brightness = 1 print 'Light sensor: ', light_sensor, ' Brightness: ', brightness # Send brightness command soc.bcm2835_spi_transfer(SPI_CMD_BRIGHTNESS) # send command soc.bcm2835_spi_transfer(int(brightness)) # send brightness # Send digits soc.bcm2835_spi_transfer(SPI_CMD_MINUTES) # send command data_byte = soc.bcm2835_spi_transfer(i) # send digit soc.bcm2835_spi_transfer(SPI_CMD_TENS_MINUTES) # send command data_byte = soc.bcm2835_spi_transfer(i) # send digit soc.bcm2835_spi_transfer(SPI_CMD_HOURS) # send command data_byte = soc.bcm2835_spi_transfer(i) # send digit soc.bcm2835_spi_transfer(SPI_CMD_TENS_HOURS) # send command data_byte = soc.bcm2835_spi_transfer(i) # send digit # Periodic watch-dog 'keep alive' soc.bcm2835_spi_transfer(SPI_CMD_WDOG) # send command data_byte = soc.bcm2835_spi_transfer(DUMMY) # read response if data_byte != WATCH_DOG_REPLY: print 'Watch-dog reply missing. received: ', data_byte break soc.bcm2835_spi_end() print 'closing GPIO:', soc.bcm2835_close()