Advertisement
AverageMan

PiBash Radio

Dec 11th, 2014
656
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 26.76 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # Pi & Bash radio code by @AverageManVsPi
  4.  
  5. # Copyright 2012 Daniel Berlin (with some changes by Adafruit Industries/Limor Fried)
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining a copy of
  8. # this software and associated documentation files (the "Software"), to deal  MCP230XX_GPIO(1, 0xin
  9. # the Software without restriction, including without limitation the rights to
  10. # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  11. # of the Software, and to permit persons to whom the Software is furnished to do
  12. # so, subject to the following conditions:
  13.  
  14. # The above copyright notice and this permission notice shall be included in all
  15. # copies or substantial portions of the Software.
  16.  
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. # SOFTWARE.
  24.  
  25. ##################################
  26. ############# IMPORTS ############  
  27. ##################################
  28.  
  29. from Adafruit_I2C import Adafruit_I2C
  30. import smbus
  31. import time
  32. import spidev
  33. import time
  34. import os
  35. import threading
  36. from threading import Thread
  37. import RPi.GPIO as GPIO
  38. import time
  39. #
  40. import subprocess
  41. import socket
  42. import fcntl
  43. import struct
  44. import datetime
  45. #import smtplib
  46. #from email.mime.text import MIMEText
  47.  
  48. MCP23017_IODIRA = 0x00
  49. MCP23017_IODIRB = 0x01
  50. MCP23017_GPIOA  = 0x12
  51. MCP23017_GPIOB  = 0x13
  52. MCP23017_GPPUA  = 0x0C
  53. MCP23017_GPPUB  = 0x0D
  54. MCP23017_OLATA  = 0x14
  55. MCP23017_OLATB  = 0x15
  56. MCP23008_GPIOA  = 0x09
  57. MCP23008_GPPUA  = 0x06
  58. MCP23008_OLATA  = 0x0A
  59.  
  60. ###############################################
  61. ########## DEFINE CLASS FOR MCP23017 ##########  
  62. ###############################################
  63.  
  64. class Adafruit_MCP230XX(object):
  65.     OUTPUT = 0
  66.     INPUT = 1
  67.  
  68.     def __init__(self, address, num_gpios, busnum=-1):
  69.         assert num_gpios >= 0 and num_gpios <= 16, "Number of GPIOs must be between 0 and 16"
  70.         self.i2c = Adafruit_I2C(address=address, busnum=busnum)
  71.         self.address = address
  72.         self.num_gpios = num_gpios
  73.  
  74.         # set defaults
  75.         if num_gpios <= 8:
  76.             self.i2c.write8(MCP23017_IODIRA, 0xFF)  # all inputs on port A
  77.             self.direction = self.i2c.readU8(MCP23017_IODIRA)
  78.             self.i2c.write8(MCP23008_GPPUA, 0x00)
  79.         elif num_gpios > 8 and num_gpios <= 16:
  80.             self.i2c.write8(MCP23017_IODIRA, 0xFF)  # all inputs on port A
  81.             self.i2c.write8(MCP23017_IODIRB, 0xFF)  # all inputs on port B
  82.             self.direction = self.i2c.readU8(MCP23017_IODIRA)
  83.             self.direction |= self.i2c.readU8(MCP23017_IODIRB) << 8
  84.             self.i2c.write8(MCP23017_GPPUA, 0x00)
  85.             self.i2c.write8(MCP23017_GPPUB, 0x00)
  86.  
  87.     def _changebit(self, bitmap, bit, value):
  88.         assert value == 1 or value == 0, "Value is %s must be 1 or 0" % value
  89.         if value == 0:
  90.             return bitmap & ~(1 << bit)
  91.         elif value == 1:
  92.             return bitmap | (1 << bit)
  93.  
  94.     def _readandchangepin(self, port, pin, value, currvalue = None):
  95.         assert pin >= 0 and pin < self.num_gpios, "Pin number %s is invalid, only 0-%s are valid" % (pin, self.num_gpios)
  96.         #assert self.direction & (1 << pin) == 0, "Pin %s not set to output" % pin
  97.         if not currvalue:
  98.              currvalue = self.i2c.readU8(port)
  99.         newvalue = self._changebit(currvalue, pin, value)
  100.         self.i2c.write8(port, newvalue)
  101.         return newvalue
  102.  
  103.  
  104.     def pullup(self, pin, value):
  105.         if self.num_gpios <= 8:
  106.             return self._readandchangepin(MCP23008_GPPUA, pin, value)
  107.         if self.num_gpios <= 16:
  108.             lvalue = self._readandchangepin(MCP23017_GPPUA, pin, value)
  109.             if (pin < 8):
  110.                 return
  111.             else:
  112.                 return self._readandchangepin(MCP23017_GPPUB, pin-8, value) << 8
  113.  
  114.     # Set pin to either input or output mode
  115.     def config(self, pin, mode):
  116.         if self.num_gpios <= 8:
  117.             self.direction = self._readandchangepin(MCP23017_IODIRA, pin, mode)
  118.         if self.num_gpios <= 16:
  119.             if (pin < 8):
  120.                 self.direction = self._readandchangepin(MCP23017_IODIRA, pin, mode)
  121.             else:
  122.                 self.direction |= self._readandchangepin(MCP23017_IODIRB, pin-8, mode) << 8
  123.  
  124.         return self.direction
  125.  
  126.     def output(self, pin, value):
  127.         # assert self.direction & (1 << pin) == 0, "Pin %s not set to output" % pin
  128.         if self.num_gpios <= 8:
  129.             self.outputvalue = self._readandchangepin(MCP23008_GPIOA, pin, value, self.i2c.readU8(MCP23008_OLATA))
  130.         if self.num_gpios <= 16:
  131.             if (pin < 8):
  132.                 self.outputvalue = self._readandchangepin(MCP23017_GPIOA, pin, value, self.i2c.readU8(MCP23017_OLATA))
  133.             else:
  134.                 self.outputvalue = self._readandchangepin(MCP23017_GPIOB, pin-8, value, self.i2c.readU8(MCP23017_OLATB)) << 8
  135.  
  136.         return self.outputvalue
  137.  
  138.  
  139.         self.outputvalue = self._readandchangepin(MCP23017_IODIRA, pin, value, self.outputvalue)
  140.         return self.outputvalue
  141.  
  142.     def input(self, pin):
  143.         assert pin >= 0 and pin < self.num_gpios, "Pin number %s is invalid, only 0-%s are valid" % (pin, self.num_gpios)
  144.         assert self.direction & (1 << pin) != 0, "Pin %s not set to input" % pin
  145.         if self.num_gpios <= 8:
  146.             value = self.i2c.readU8(MCP23008_GPIOA)
  147.         elif self.num_gpios > 8 and self.num_gpios <= 16:
  148.             value = self.i2c.readU8(MCP23017_GPIOA)
  149.             value |= self.i2c.readU8(MCP23017_GPIOB) << 8
  150.         return value & (1 << pin)
  151.  
  152.     def readU8(self):
  153.         result = self.i2c.readU8(MCP23008_OLATA)
  154.         return(result)
  155.  
  156.     def readS8(self):
  157.         result = self.i2c.readU8(MCP23008_OLATA)
  158.         if (result > 127): result -= 256
  159.         return result
  160.  
  161.     def readU16(self):
  162.         assert self.num_gpios >= 16, "16bits required"
  163.         lo = self.i2c.readU8(MCP23017_OLATA)
  164.         hi = self.i2c.readU8(MCP23017_OLATB)
  165.         return((hi << 8) | lo)
  166.  
  167.     def readS16(self):
  168.         assert self.num_gpios >= 16, "16bits required"
  169.         lo = self.i2c.readU8(MCP23017_OLATA)
  170.         hi = self.i2c.readU8(MCP23017_OLATB)
  171.         if (hi > 127): hi -= 256
  172.         return((hi << 8) | lo)
  173.  
  174.     def write8(self, value):
  175.         self.i2c.write8(MCP23008_OLATA, value)
  176.  
  177.     def write16(self, value):
  178.         assert self.num_gpios >= 16, "16bits required"
  179.         self.i2c.write8(MCP23017_OLATA, value & 0xFF)
  180.         self.i2c.write8(MCP23017_OLATB, (value >> 8) & 0xFF)
  181.  
  182. ###############################################
  183. ########## DEFINE CLASS FOR MCP23017 ##########  
  184. ###############################################
  185.        
  186. # RPi.GPIO compatible interface for MCP23017 and MCP23008
  187. class MCP230XX_GPIO(object):
  188.     OUT = 0
  189.     IN = 1
  190.     BCM = 0
  191.     BOARD = 0
  192.     def __init__(self, busnum, address, num_gpios):
  193.         self.chip = Adafruit_MCP230XX(address, num_gpios, busnum)
  194.     def setmode(self, mode):
  195.         # do nothing
  196.         pass
  197.     def setup(self, pin, mode):
  198.         self.chip.config(pin, mode)
  199.     def input(self, pin):
  200.         return self.chip.input(pin)
  201.     def output(self, pin, value):
  202.         self.chip.output(pin, value)
  203.     def pullup(self, pin, value):
  204.         self.chip.pullup(pin, value)
  205.  
  206. ###############################################
  207. ################## SET UP LCD #################  
  208. ###############################################
  209.        
  210. #!/usr/bin/python
  211. #
  212.  
  213. # The wiring for the LCD is as follows:
  214. # 1 : GND
  215. # 2 : 5V
  216. # 3 : Contrast (0-5V)*
  217. # 4 : RS (Register Select)
  218. # 5 : R/W (Read Write)       - GROUND THIS PIN
  219. # 6 : Enable or Strobe
  220. # 7 : Data Bit 0             - NOT USED
  221. # 8 : Data Bit 1             - NOT USED
  222. # 9 : Data Bit 2             - NOT USED
  223. # 10: Data Bit 3             - NOT USED
  224. # 11: Data Bit 4
  225. # 12: Data Bit 5
  226. # 13: Data Bit 6
  227. # 14: Data Bit 7
  228. # 15: LCD Backlight +5V**
  229. # 16: LCD Backlight GND
  230.  
  231. GPIO.setwarnings(False)
  232. # Define GPIO to LCD mapping
  233. LCD_RS = 17
  234. LCD_E  = 18
  235. LCD_D4 = 22
  236. LCD_D5 = 23
  237. LCD_D6 = 24
  238. LCD_D7 = 25
  239.  
  240. # Define some device constants
  241. LCD_WIDTH = 16    # Maximum characters per line
  242. LCD_CHR = True
  243. LCD_CMD = False
  244.  
  245. LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
  246. LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
  247.  
  248. # Timing constants
  249. E_PULSE = 0.0001
  250. E_DELAY = 0.00005
  251.  
  252.  
  253. def lcd_init():
  254.   # Initialise display
  255.   lcd_byte(0x33,LCD_CMD)
  256.   lcd_byte(0x32,LCD_CMD)
  257.   lcd_byte(0x28,LCD_CMD)
  258.   lcd_byte(0x0C,LCD_CMD)
  259.   lcd_byte(0x06,LCD_CMD)
  260.   lcd_byte(0x01,LCD_CMD)  
  261.  
  262. def lcd_string(message,style):
  263.   # Send string to display
  264.  
  265.   #message = message.ljust(LCD_WIDTH," ")  
  266.   if style==1:
  267.     message = message.ljust(LCD_WIDTH," ")  
  268.   elif style==2:
  269.     message = message.center(LCD_WIDTH," ")
  270.   elif style==3:
  271.     message = message.rjust(LCD_WIDTH," ")
  272.  
  273.   for i in range(LCD_WIDTH):
  274.     lcd_byte(ord(message[i]),LCD_CHR)
  275.  
  276. def lcd_byte(bits, mode):
  277.   # Send byte to data pins
  278.   # bits = data
  279.   # mode = True  for character
  280.   #        False for command
  281.  
  282.   GPIO.output(LCD_RS, mode) # RS
  283.  
  284.   # High bits
  285.   GPIO.output(LCD_D4, False)
  286.   GPIO.output(LCD_D5, False)
  287.   GPIO.output(LCD_D6, False)
  288.   GPIO.output(LCD_D7, False)
  289.   if bits&0x10==0x10:
  290.     GPIO.output(LCD_D4, True)
  291.   if bits&0x20==0x20:
  292.     GPIO.output(LCD_D5, True)
  293.   if bits&0x40==0x40:
  294.     GPIO.output(LCD_D6, True)
  295.   if bits&0x80==0x80:
  296.     GPIO.output(LCD_D7, True)
  297.  
  298.   # Toggle 'Enable' pin
  299.   time.sleep(E_DELAY)
  300.   GPIO.output(LCD_E, True)
  301.   time.sleep(E_PULSE)
  302.   GPIO.output(LCD_E, False)
  303.   time.sleep(E_DELAY)      
  304.  
  305.   # Low bits
  306.   GPIO.output(LCD_D4, False)
  307.   GPIO.output(LCD_D5, False)
  308.   GPIO.output(LCD_D6, False)
  309.   GPIO.output(LCD_D7, False)
  310.   if bits&0x01==0x01:
  311.     GPIO.output(LCD_D4, True)
  312.   if bits&0x02==0x02:
  313.     GPIO.output(LCD_D5, True)
  314.   if bits&0x04==0x04:
  315.     GPIO.output(LCD_D6, True)
  316.   if bits&0x08==0x08:
  317.     GPIO.output(LCD_D7, True)
  318.  
  319.   # Toggle 'Enable' pin
  320.   time.sleep(E_DELAY)
  321.   GPIO.output(LCD_E, True)
  322.   time.sleep(E_PULSE)
  323.   GPIO.output(LCD_E, False)
  324.   time.sleep(E_DELAY)
  325.  
  326.  
  327. ###############################################
  328. ################## MCP2308??? #################
  329. ###############################################
  330.  
  331. if __name__ == '__main__':
  332.                        
  333.     def analogue_input(analogue0,analogue1,analogue2,analogue3,analogue4,analogue5,analogue6,analogue7, ):
  334.         global analogue_out0
  335.         global analogue_out1
  336.         global analogue_out2
  337.         global analogue_out3
  338.         global analogue_out4
  339.         global analogue_out5
  340.         global analogue_out6
  341.         global analogue_out7
  342.         while(True):
  343.             analogue_out0 = ReadChannel(analogue0)
  344.             analogue_out1 = ReadChannel(analogue1)
  345.             analogue_out2 = ReadChannel(analogue2)
  346.             analogue_out3 = ReadChannel(analogue3)
  347.             analogue_out4 = ReadChannel(analogue4)
  348.             analogue_out5 = ReadChannel(analogue5)
  349.             analogue_out6 = ReadChannel(analogue6)
  350.             analogue_out7 = ReadChannel(analogue7)
  351.             time.sleep(0.5);
  352.    
  353.     # Function to read SPI data from MCP3008 chip
  354.     # Channel must be an integer 0-7
  355.     def ReadChannel(channel):
  356.         adc = spi.xfer2([1,(8+channel)<<4,0])
  357.         data = ((adc[1]&3) << 8) + adc[2]
  358.         return data
  359.  
  360.     # Define sensor channels
  361.     analogue0  = 0
  362.     analogue1  = 1
  363.     analogue2  = 2
  364.     analogue3  = 3
  365.     analogue4  = 4
  366.     analogue5  = 5
  367.     analogue6  = 6
  368.     analogue7  = 7
  369.     # Open SPI bus
  370.     spi = spidev.SpiDev()
  371.     spi.open(0,0)
  372.    
  373.     GPIO.setmode(GPIO.BCM)       # Use BCM GPIO numbers
  374.     GPIO.setup(LCD_E, GPIO.OUT)  # E
  375.     GPIO.setup(LCD_RS, GPIO.OUT) # RS
  376.     GPIO.setup(LCD_D4, GPIO.OUT) # DB4
  377.     GPIO.setup(LCD_D5, GPIO.OUT) # DB5
  378.     GPIO.setup(LCD_D6, GPIO.OUT) # DB6
  379.     GPIO.setup(LCD_D7, GPIO.OUT) # DB7
  380.  
  381.     # Initialise display
  382.     lcd_init()
  383.    
  384.     # ***************************************************
  385.     # Set num_gpios to 8 for MCP23008 or 16 for MCP23017!
  386.     # ***************************************************
  387.     # mcp = Adafruit_MCP230XX(address = 0x20, num_gpios = 8) # MCP23008
  388.     mcp = Adafruit_MCP230XX(address = 0x20, num_gpios = 16) # MCP23017
  389.  
  390.     # **************************************************************************
  391.     # Digital I/O Configuration Section
  392.     # **************************************************************************
  393.    
  394.     # This section contains the configuration for the 8 pre-installed MCP23017
  395.     # Inputs (5 pushbuttons) and Outputs (3 LEDs).
  396.    
  397.     # Outputs:
  398.     mcp.config(0, mcp.OUTPUT) # LCD Backlight
  399.     mcp.config(8, mcp.OUTPUT) # Green LED
  400.     mcp.config(10, mcp.OUTPUT) # Amber LED
  401.     mcp.config(12, mcp.OUTPUT) # Red LED
  402.    
  403.    
  404.     # Inputs:
  405.     mcp.config(15, mcp.INPUT) # LCD Lower Line Select Button
  406.     mcp.pullup(15, 1)
  407.     mcp.config(14, mcp.INPUT) # LCD Upper Line Select Button
  408.     mcp.pullup(14, 1)
  409.     mcp.config(13, mcp.INPUT) # Down Button
  410.     mcp.pullup(13, 1)
  411.     mcp.config(11, mcp.INPUT) # Enter Button
  412.     mcp.pullup(11, 1)
  413.     mcp.config(9, mcp.INPUT) # Up Button
  414.     mcp.pullup(9, 1)
  415.    
  416.     #***************************************************************************
  417.     #***************************************************************************
  418.     #***************************************************************************
  419.     # This section is where you can modify the configuration for the 8 remaining
  420.     # MCP23017 Input/Outputs.
  421.    
  422.     # OUTPUTS:
  423.     # Remove the # character from any I/O lines which
  424.     # you want to configure as an output
  425.     #mcp.config(1, mcp.OUTPUT)
  426.     #mcp.config(2, mcp.OUTPUT)
  427.     #mcp.config(3, mcp.OUTPUT)
  428.     #mcp.config(4, mcp.OUTPUT)
  429.     #mcp.config(5, mcp.OUTPUT)
  430.     #mcp.config(6, mcp.OUTPUT)
  431.     #mcp.config(7, mcp.OUTPUT)  
  432.    
  433.     # INPUTS:
  434.     # Remove the # character from both lines associated with any I/O lines which
  435.     # you want to configure as an output
  436.     #mcp.config(0, mcp.INPUT)
  437.     #mcp.pullup(0, 1)
  438.     #mcp.config(1, mcp.INPUT)
  439.     #mcp.pullup(1, 1)
  440.     #mcp.config(2, mcp.INPUT)
  441.     #mcp.pullup(2, 1)
  442.     #mcp.config(3, mcp.INPUT)
  443.     #mcp.pullup(3, 1)
  444.     #mcp.config(4, mcp.INPUT)
  445.     #mcp.pullup(4, 1)
  446.     #mcp.config(5, mcp.INPUT)
  447.     #mcp.pullup(5, 1)
  448.     #mcp.config(6, mcp.INPUT)
  449.     #mcp.pullup(6, 1)
  450.     #mcp.config(7, mcp.INPUT)
  451.     #mcp.pullup(7, 1)
  452.    
  453.     #***************************************************************************#
  454.     #***************************************************************************#
  455.     #***************************************************************************#
  456.     #***************************************************************************#
  457.     #************************The Programming Starts Here************************#
  458.     #***************************************************************************#
  459.     #***************************************************************************#
  460.     #***************************************************************************#    
  461.     #***************************************************************************#
  462.    
  463.     #####  Buttons  #####
  464.     #
  465.     # NO BUTTONS:
  466.     #     while (((mcp.input(15) >> 15)+(mcp.input(14) >> 14)+(mcp.input(13) >> 13)+(mcp.input(11) >> 11)+(mcp.input(9) >> 9)) == 5): #Detect if no buttons are pressed
  467.     #
  468.     # MORE THAN ONE BUTTON:
  469.     #     while (((mcp.input(15) >> 15)+(mcp.input(14) >> 14)+(mcp.input(13) >> 13)+(mcp.input(11) >> 11)+(mcp.input(9) >> 9))<4): # Detect if more than one button is pressed
  470.     #
  471.     # ONE BUTTON
  472.     #     while (((mcp.input(15) >> 15)+(mcp.input(14) >> 14)+(mcp.input(13) >> 13)+(mcp.input(11) >> 11)+(mcp.input(9) >> 9)) == 4): # Detect if one button is pressed
  473.     #
  474.     #          BOTTOM LEFT:  if (mcp.input(15) >> 15) == 0:
  475.     #
  476.     #          BOTTOM LEFT UPPER:  if (mcp.input(14) >> 14) == 0:
  477.     #
  478.     #          MIDDLE BOTTOM:  if (mcp.input(13) >> 13) == 0:
  479.     #
  480.     #          MIDDLE TOP:  if (mcp.input(9) >> 9) == 0:
  481.     #
  482.     #          RIGHT BUTTON:  if (mcp.input(11) >> 11) == 0:
  483.     #
  484.     #####################
  485.    
  486.     #######  MCP  #######
  487.     #
  488.     #  0 =   # LCD Backlight
  489.     #  8 =   # Green LED
  490.     #  10 =  # Amber LED
  491.     #  12 =  # Red LED
  492.     #
  493.     #####################
  494.    
  495.     # Start Analogue Data Capture Thread
  496.     analogue_thread = Thread(target = analogue_input, args = (analogue0,analogue1,analogue2,analogue3,analogue4,analogue5,analogue6,analogue7, ))
  497.     analogue_thread.start()
  498.  
  499.    
  500.     # START DISPLAY/LED
  501.     mcp.output(0, 1) #Turn on LCD Backlight
  502.    
  503.     mcp.output(8,0) #turn off any 'on' LEDSs
  504.     mcp.output(12,0) #turn off any 'on' LEDSs
  505.     mcp.output(10,0) #turn off any 'on' LEDSs
  506.    
  507.     lcd_init() # Do this frequently as interference can kill the display
  508.    
  509.     #START UP TEXT
  510.     lcd_byte(LCD_LINE_1, LCD_CMD)    #Line 1
  511.     lcd_string("Radio PiBash",2)     #Line 1 text display (1 = left justify, 2 = centre, 3 = right)
  512.     lcd_byte(LCD_LINE_2, LCD_CMD)    #Line 2
  513.     lcd_string("@AverageManVsPi",2)  #Line 2 text display (1 = left justify, 2 = centre, 3 = right)
  514.    
  515.     time.sleep(3) #Wait 3 seconds
  516.    
  517.     lcd_init() # Do this frequently as interference can kill display
  518.  
  519.     def frontmenu():
  520.         while (True):
  521.             counter = 0    
  522.             #
  523.             # NO BUTTONS BEING PRESSED
  524.             #
  525.             while (((mcp.input(15) >> 15)+(mcp.input(14) >> 14)+(mcp.input(13) >> 13)+(mcp.input(11) >> 11)+(mcp.input(9) >> 9)) == 5): # Detect if no buttons are pressed
  526.                 line1 = ""
  527.                 line2 = ""
  528.                 space = "  Temp:"
  529.                 space2 = "     "
  530.                 temp = ""
  531.                 mytime = ""
  532.                 radio = "<Radio"
  533.                 light = "<Light"
  534.                 temperature = ((analogue_out0 * 472)/float(1023))-50 # this line converts the anlaogue0 input from the TMP36 sensor into a temperature in deg C.
  535.                 temp = (str(int(temperature))) + "C"
  536.                 f=os.popen("date")
  537.                 for i in f.readlines():
  538.                     mytime += i
  539.                     mytime = mytime[11:-13]
  540.                     line1 = radio + space + temp
  541.                     line2 = light + space2 + mytime
  542.                     ######
  543.                     lcd_byte(LCD_LINE_1, LCD_CMD)
  544.                     lcd_string(line1,2) # radio and temp on line 1
  545.                     lcd_byte(LCD_LINE_2, LCD_CMD)
  546.                     lcd_string(line2,2) # light and time on line 2
  547.                     if (mcp.input(15) >> 15) == 0:
  548.                         if counter == 0:
  549.                             counter = 1
  550.                             lcd_byte(LCD_LINE_2, LCD_CMD) #Line 2
  551.                             mcp.output(12,1)
  552.                             lcd_string("Light Off",2)
  553.                             time.sleep(1)
  554.                             mcp.output(0, 0)
  555.                             mcp.output(12,0)
  556.                         elif counter == 1:
  557.                             counter = 0
  558.                             lcd_byte(LCD_LINE_2, LCD_CMD) #Line 2
  559.                             mcp.output(0, 1)
  560.                             mcp.output(8,1)
  561.                             lcd_string("Light On",2)
  562.                             time.sleep(1)
  563.                             mcp.output(8,0)
  564.                        
  565.  
  566.                 # BOTTOM LEFT UPPER BUTTON (radio) ---------------------------------------------------------------------------------------------------------------------------
  567.                 if (mcp.input(14) >> 14) == 0:
  568.                     radiomenu()
  569.                
  570.             # MULTIPLE BUTTONS PRESSED ----let's have some fun...
  571.             while (((mcp.input(15) >> 15)+(mcp.input(14) >> 14)+(mcp.input(13) >> 13)+(mcp.input(11) >> 11)+(mcp.input(9) >> 9))<4): # Detect if more than one button is pressed
  572.                 lcd_byte(LCD_LINE_1, LCD_CMD)
  573.                 lcd_string("WHY YOU PRESSING",3)
  574.                 lcd_byte(LCD_LINE_2, LCD_CMD)
  575.                 lcd_string("ALL MY BUTTONS?!",2)
  576.                 time.sleep(2.5)
  577.        
  578.             # PRE-CODE LINE FOR A SINGLE BUTTON PRESS
  579.             while (((mcp.input(15) >> 15)+(mcp.input(14) >> 14)+(mcp.input(13) >> 13)+(mcp.input(11) >> 11)+(mcp.input(9) >> 9)) == 4): # Detect if one button is pressed
  580.        
  581.                 # BOTTOM LEFT BUTTON -----------------------------------------------------------------------------------------------------------------------------
  582.                 #if (mcp.input(15) >> 15) == 0:
  583.                     #mcp.output(8,1)
  584.                     #lcd_init()
  585.                     #temperature = ((analogue_out0 * 472)/float(1023))-50 # this line converts the anlaogue0 input from the TMP36 sensor into a temperature in deg C.
  586.                     #lcd_byte(LCD_LINE_1, LCD_CMD)
  587.                     #lcd_string("Temp:" + (str(int(temperature))) + "C",3) # Display the temperature from the TMP36 sensor on the first line of the display.
  588.                     #lcd_byte(LCD_LINE_2, LCD_CMD)
  589.                     #lcd_string("VOLUME DOWN",2)
  590.                     #time.sleep(1)
  591.                     #mcp.output(8,0)
  592.                
  593.                 # MIDDLE BOTTOM BUTTON -------------------------------------------------------------------------------------------------------------------------------
  594.  
  595.                 if (mcp.input(13) >> 13) == 0:
  596.                     mcp.output(8,1)
  597.                     lcd_init()
  598.                     temperature = ((analogue_out0 * 472)/float(1023))-50 # this line converts the anlaogue0 input from the TMP36 sensor into a temperature in deg C.
  599.                     lcd_byte(LCD_LINE_1, LCD_CMD)
  600.                     lcd_string("Temp:" + (str(int(temperature))) + "C",3) # Display the temperature from the TMP36 sensor on the first line of the display.
  601.                     lcd_byte(LCD_LINE_2, LCD_CMD)
  602.                     lcd_string("NEXT STATION",2)
  603.                     time.sleep(1)
  604.                     mcp.output(8,0)
  605.            
  606.                 # TOP BUTTON -----------------------------------------------------------------------------------------------------------------------------------------
  607.                 if (mcp.input(9) >> 9) == 0:
  608.                     mcp.output(12,1)
  609.                     lcd_init()
  610.                     temperature = ((analogue_out0 * 472)/float(1023))-50 # this line converts the anlaogue0 input from the TMP36 sensor into a temperature in deg C.
  611.                     lcd_byte(LCD_LINE_1, LCD_CMD)
  612.                     lcd_string("Temp:" + (str(int(temperature))) + "C",3) # Display the temperature from the TMP36 sensor on the first line of the display.
  613.                     lcd_byte(LCD_LINE_2, LCD_CMD)
  614.                     lcd_string("PREVIOUS STATION",2)
  615.                     time.sleep(1)
  616.                     mcp.output(12,0)
  617.            
  618.                 # RIGHT BUTTON --------------------------------------------------------------------------------------------------------------------------------------
  619.                 if (mcp.input(11) >> 11) == 0:
  620.                     mcp.output(10,1)
  621.                     lcd_init()
  622.                     temperature = ((analogue_out0 * 465)/float(1023))-50 # this line converts the anlaogue0 input from the TMP36 sensor into a temperature in deg C.
  623.                     lcd_byte(LCD_LINE_1, LCD_CMD)
  624.                     lcd_string("Temp:" + (str(int(temperature))) + "C",3) # Display the temperature from the TMP36 sensor on the first line of the display.
  625.                     lcd_byte(LCD_LINE_2, LCD_CMD)
  626.                     lcd_string("SELECT STATION",2)
  627.                     time.sleep(1)
  628.                     mcp.output(10,0)
  629.  
  630.     def radiomenu():
  631.         mcp.output(8,0)
  632.         mcp.output(10,0)
  633.         mcp.output(12,0)
  634.         while(True):
  635.             line1 = ""
  636.             line2 = ""
  637.             space = "   Temp:"
  638.             space2 = "     "
  639.             temp = ""
  640.             mytime = ""
  641.             play = "<Play"
  642.             back = "<Back " #DELETED
  643.             temperature = ((analogue_out0 * 472)/float(1023))-50 # this line converts the anlaogue0 input from the TMP36 sensor into a temperature in deg C.
  644.             temp = (str(int(temperature))) + "C"
  645.             f=os.popen("date")
  646.             for i in f.readlines():
  647.                 mytime += i
  648.                 mytime = mytime[11:-13]
  649.                 line1 = play + space + temp
  650.                 line2 = back + space2 + mytime
  651.                 ######
  652.                 lcd_byte(LCD_LINE_1, LCD_CMD)
  653.                 lcd_string(line1,2) # radio and temp on line 1
  654.                 lcd_byte(LCD_LINE_2, LCD_CMD)
  655.                 lcd_string(line2,2) # back and time on line 2
  656.                 time.sleep(0.3)
  657.             if (mcp.input(14) >> 14) == 0:
  658.                 playradio()
  659.             if (mcp.input(15) >> 15) == 0:
  660.                 frontmenu()
  661.                    
  662.     def playradio():
  663.         mcp.output(8,1)
  664.         mcp.output(12,1)
  665.         os.system("mpc play 1")
  666.         while(True):
  667.             line1 = ""
  668.             back = "<Back "
  669.             pretemp = "  "
  670.             mytime = ""
  671.             temperature = ((analogue_out0 * 472)/float(1023))-50 # this line converts the anlaogue0 input from the TMP36 sensor into a temperature in deg C.
  672.             temp = (str(int(temperature))) + "C"
  673.             f=os.popen("date")
  674.             for i in f.readlines():
  675.                 mytime += i
  676.                 mytime = mytime[11:-13]
  677.                 line1 = back + mytime + pretemp + temp
  678.                 lcd_byte(LCD_LINE_1, LCD_CMD)
  679.                 lcd_string(line1,3) # Display the temperature from the TMP36 sensor on the first line of the display.
  680.                 f=os.popen("mpc current")
  681.                 for i in f.readlines():
  682.                     station = ""
  683.                     station += i
  684.                     str_pad = " " * 16
  685.                     station = station[0:-1]
  686.                     station = str_pad + station
  687.                     for i in range (0, len(station)):                  
  688.                         lcd_byte(LCD_LINE_2, LCD_CMD)  
  689.                         lcd_text = station[i:(i+16)]
  690.                         lcd_string(lcd_text,1)  
  691.                         time.sleep(0.4)  
  692.                         lcd_byte(LCD_LINE_2, LCD_CMD)  
  693.                         lcd_string(str_pad,1)
  694.                         mcp.output(10,0)
  695.                         if (mcp.input(14) >> 14) == 0:
  696.                             radiomenu()
  697.                         if (mcp.input(9) >> 9) == 0:
  698.                             mcp.output(10,1)
  699.                             os.system("mpc next")
  700.                         if (mcp.input(13) >> 13) == 0:
  701.                             os.system("mpc prev")
  702.                             mcp.output(10,1)
  703.                            
  704.     frontmenu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement