Advertisement
Guest User

taxameter

a guest
Oct 22nd, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.41 KB | None | 0 0
  1. #!/usr/bin/python
  2. # --------------------------------------
  3. #    ___  ___  _ ____
  4. #   / _ \/ _ \(_) __/__  __ __
  5. #  / , _/ ___/ /\ \/ _ \/ // /
  6. # /_/|_/_/  /_/___/ .__/\_, /
  7. #                /_/   /___/
  8. #
  9. #  lcd_16x2.py
  10. #  20x4 LCD Test Script with
  11. #  backlight control and text justification
  12. #
  13. # Author : Matt Hawkins
  14. # Date   : 06/04/2015
  15. #
  16. # http://www.raspberrypi-spy.co.uk/
  17. #
  18. # --------------------------------------
  19.  
  20. # The wiring for the LCD is as follows:
  21. # 1 : GND
  22. # 2 : 5V
  23. # 3 : Contrast (0-5V)*
  24. # 4 : RS (Register Select)
  25. # 5 : R/W (Read Write)       - GROUND THIS PIN
  26. # 6 : Enable or Strobe
  27. # 7 : Data Bit 0             - NOT USED
  28. # 8 : Data Bit 1             - NOT USED
  29. # 9 : Data Bit 2             - NOT USED
  30. # 10: Data Bit 3             - NOT USED
  31. # 11: Data Bit 4
  32. # 12: Data Bit 5
  33. # 13: Data Bit 6
  34. # 14: Data Bit 7
  35. # 15: LCD Backlight +5V**
  36. # 16: LCD Backlight GND
  37.  
  38. # import
  39. import RPi.GPIO as GPIO
  40. import time
  41. import os
  42. import datetime
  43. import lcd
  44. from time import sleep
  45. def getTimeNow():
  46.     now = datetime.datetime.now()
  47.     return(now)
  48. def convertDeltaToHours(input):
  49.     remainingSeconds = int(input.total_seconds())
  50.     hoursDecimal = remainingSeconds / 3600
  51.     return(hoursDecimal)
  52. def calcMoney(worktime, stundenlohn):
  53.     lohn = float(convertDeltaToHours(worktime) * stundenlohn)
  54.     return(lohn)
  55. def stundenlohnMarcelEinlesen():
  56.     #file = open('stundenlohn_marcel.txt', 'r')
  57.     file = open(os.path.join(os.path.dirname(__file__), 'stundenlohn_marcel.txt'), 'r')
  58.     stundenlohn_marcel = file.read()
  59.     return(stundenlohn_marcel)
  60. def calcWorkTime(starttime, stoptime):
  61.     worktime = stoptime - starttime
  62.     return(worktime)
  63. def taxameterNotRunning():
  64.     global readable_clock
  65.     if readable_clock != getTimeNow().strftime("%H:%M"):
  66.         readable_clock = getTimeNow().strftime("%H:%M")
  67.         text_line1 = "Team 1 ES-Leiberg"
  68.         text_line2 = readable_clock
  69.         text_line3 = getTimeNow().strftime("%a, %d. %B %Y")
  70.         text_line4 = "KW: 7980"
  71.         array = [text_line1, text_line2, text_line3, text_line4]
  72.         orientationarray = [2, 2, 1, 3]
  73.         array_with_everything = array + orientationarray
  74.         lcd.lcd_show(*array_with_everything)
  75. def taxameterRunning():
  76.     global stundenlohn_marcel
  77.     global starttime
  78.     global starttime_without_breaks
  79.     global stoptime
  80.     global money_earned_brutto
  81.     global money_earned_netto
  82.     global worktime
  83.     global output_array
  84.     global readable_clock
  85.     global text_line1
  86.     global text_line2
  87.     global text_line3
  88.     global text_line4
  89.     global break_flipflop
  90.     global start_flipflop
  91.  
  92.     if setStart_Flipflop() is True:
  93.         start_flipflop = True
  94.     while start_flipflop is True:
  95.         if GPIO.input(14) is not 1:
  96.             start_flipflop = False
  97.     break_flipflop = True
  98.     readable_clock = "0"
  99.     stundenlohn_marcel = float(stundenlohnMarcelEinlesen())
  100.     starttime = getTimeNow()
  101.     starttime_without_breaks = starttime
  102.     print("Program is running...")
  103.     print('\n')
  104.     print("Started at: "+starttime.strftime("%H:%M:%S"))
  105.     print('\n')
  106.  
  107.     while GPIO.input(14) is not 1:
  108.         stoptime = getTimeNow()
  109.         worktime = calcWorkTime(starttime_without_breaks, stoptime)
  110.         money_earned_brutto = round((calcMoney(calcWorkTime(starttime_without_breaks, stoptime), stundenlohn_marcel)), 2)
  111.         money_earned_netto = round((money_earned_brutto * 0.6), 2)
  112.         sleep(0.2)
  113.         if break_flipflop is not True:
  114.  
  115.             if GPIO.input(15) is 1:
  116.                 starttime_without_breaks = (starttime_without_breaks + datetime.timedelta(minutes=15))
  117.                 break_flipflop = True
  118.         if GPIO.input(15) is not 1:
  119.             break_flipflop = False
  120.         if readable_clock != getTimeNow().strftime("%H:%M:%S"):
  121.             readable_clock = getTimeNow().strftime("%H:%M:%S")
  122.             text_line1 = "Uhr " + readable_clock
  123.             text_line2 = "Gearbeitet: " + str(worktime).split(".")[0]
  124.             text_line3 = "Brutto:  " + str(format(money_earned_brutto).replace('.', ',')) + " Eur"
  125.             text_line4 = "Netto:  " + str(format(money_earned_netto).replace('.', ',')) + " Eur"
  126.             array = [text_line1, text_line2, text_line3, text_line4]
  127.             updateLCD(array[0], array[1], array[2], array[3])
  128.     if True:
  129.         sleep(2)
  130.         stoptime = getTimeNow()                                                                       #Delete Me!
  131.         worktime = calcWorkTime(starttime_without_breaks, stoptime)
  132.         money_earned_brutto = round((calcMoney(calcWorkTime(starttime_without_breaks, stoptime), stundenlohn_marcel)), 2)
  133.         money_earned_netto = round((money_earned_brutto * 0.6), 2)
  134.  
  135.     text_line1 = "Feierabend: " + getTimeNow().strftime("%H:%M")
  136.     text_line2 = "Gearbeitet: " + str(worktime).split(".")[0]
  137.     text_line3 = "Brutto:  " + str(format(money_earned_brutto).replace('.', ',')) + " Eur"
  138.     text_line4 = "Netto:  " + str(format(money_earned_netto).replace('.', ',')) + " Eur"
  139.     export = [text_line1, text_line2, text_line3, text_line4, 1, 1, 3, 3]
  140.     lcd.lcd_show(*export)
  141.     return()
  142. def setStart_Flipflop():
  143.     started = True
  144.     return(started)
  145. def setStop_Flipflop():
  146.     stopped = True
  147.     return(stopped)
  148. def start():
  149.     while True:
  150.         global stop_flipflop
  151.         stop_flipflop = True
  152.  
  153.         while GPIO.input(14) is not 1:
  154.             taxameterNotRunning()
  155.         setStart_Flipflop()
  156.         taxameterRunning()
  157.         setStop_Flipflop()
  158.         while GPIO.input(14) is not 1:
  159.             pass
  160.         if setStop_Flipflop() is True:
  161.             stop_flipflop = True
  162.         while stop_flipflop is True:
  163.             if GPIO.input(14) is not 1:
  164.                 stop_flipflop = False
  165. def lcd_init():
  166.     # Initialise display
  167.     lcd_byte(0x33, LCD_CMD)  # 110011 Initialise
  168.     lcd_byte(0x32, LCD_CMD)  # 110010 Initialise
  169.     lcd_byte(0x06, LCD_CMD)  # 000110 Cursor move direction
  170.     lcd_byte(0x0C, LCD_CMD)  # 001100 Display On,Cursor Off, Blink Off
  171.     lcd_byte(0x28, LCD_CMD)  # 101000 Data length, number of lines, font size
  172.     lcd_byte(0x01, LCD_CMD)  # 000001 Clear display
  173.     time.sleep(E_DELAY)
  174. def lcd_byte(bits, mode):
  175.     # Send byte to data pins
  176.     # bits = data
  177.     # mode = True  for character
  178.     #        False for command
  179.  
  180.     GPIO.output(LCD_RS, mode)  # RS
  181.  
  182.     # High bits
  183.     GPIO.output(LCD_D4, False)
  184.     GPIO.output(LCD_D5, False)
  185.     GPIO.output(LCD_D6, False)
  186.     GPIO.output(LCD_D7, False)
  187.     if bits & 0x10 == 0x10:
  188.         GPIO.output(LCD_D4, True)
  189.     if bits & 0x20 == 0x20:
  190.         GPIO.output(LCD_D5, True)
  191.     if bits & 0x40 == 0x40:
  192.         GPIO.output(LCD_D6, True)
  193.     if bits & 0x80 == 0x80:
  194.         GPIO.output(LCD_D7, True)
  195.  
  196.     # Toggle 'Enable' pin
  197.     lcd_toggle_enable()
  198.  
  199.     # Low bits
  200.     GPIO.output(LCD_D4, False)
  201.     GPIO.output(LCD_D5, False)
  202.     GPIO.output(LCD_D6, False)
  203.     GPIO.output(LCD_D7, False)
  204.     if bits & 0x01 == 0x01:
  205.         GPIO.output(LCD_D4, True)
  206.     if bits & 0x02 == 0x02:
  207.         GPIO.output(LCD_D5, True)
  208.     if bits & 0x04 == 0x04:
  209.         GPIO.output(LCD_D6, True)
  210.     if bits & 0x08 == 0x08:
  211.         GPIO.output(LCD_D7, True)
  212.  
  213.     # Toggle 'Enable' pin
  214.     lcd_toggle_enable()
  215. def lcd_toggle_enable():
  216.     # Toggle enable
  217.     time.sleep(E_DELAY)
  218.     GPIO.output(LCD_E, True)
  219.     time.sleep(E_PULSE)
  220.     GPIO.output(LCD_E, False)
  221.     time.sleep(E_DELAY)
  222. def lcd_string(message, line, style):
  223.     # Send string to display
  224.     # style=1 Left justified
  225.     # style=2 Centred
  226.     # style=3 Right justified
  227.  
  228.     if style == 1:
  229.         message = message.ljust(LCD_WIDTH, " ")
  230.     elif style == 2:
  231.         message = message.center(LCD_WIDTH, " ")
  232.     elif style == 3:
  233.         message = message.rjust(LCD_WIDTH, " ")
  234.  
  235.     lcd_byte(line, LCD_CMD)
  236.  
  237.     for i in range(LCD_WIDTH):
  238.         lcd_byte(ord(message[i]), LCD_CHR)
  239. def lcd_backlight(flag):
  240.     # Toggle backlight on-off-on
  241.     GPIO.output(LED_ON, flag)
  242. def updateLCD(input1, input2, input3, input4):
  243.     global line1
  244.     global line2
  245.     global line3
  246.     global line4
  247.     line1 = input1
  248.     line2 = input2
  249.     line3 = input3
  250.     line4 = input4
  251.     # Main program block
  252.     """
  253.    GPIO.setmode(GPIO.BCM)  # Use BCM GPIO numbers
  254.    GPIO.setup(LCD_E, GPIO.OUT)  # E
  255.    GPIO.setup(LCD_RS, GPIO.OUT)  # RS
  256.    GPIO.setup(LCD_D4, GPIO.OUT)  # DB4
  257.    GPIO.setup(LCD_D5, GPIO.OUT)  # DB5
  258.    GPIO.setup(LCD_D6, GPIO.OUT)  # DB6
  259.    GPIO.setup(LCD_D7, GPIO.OUT)  # DB7
  260.    GPIO.setup(LED_ON, GPIO.OUT)  # Backlight enable
  261.    """
  262.     # Initialise display
  263.     lcd_init()
  264.     """
  265.    # Toggle backlight on-off-on
  266.    lcd_backlight(True)
  267.    time.sleep(0.5)
  268.    lcd_backlight(False)
  269.    time.sleep(0.5)
  270.    lcd_backlight(True)
  271.    time.sleep(0.5)
  272.    """
  273.     if True:
  274.         # Blank display
  275.         lcd_byte(0x01, LCD_CMD)
  276.  
  277.         # Send some centred test
  278.         """ standard test
  279.        lcd_string("--------------------", LCD_LINE_1, 2)
  280.        lcd_string("Rasbperry Pi", LCD_LINE_2, 2)
  281.        lcd_string("Model B", LCD_LINE_3, 2)
  282.        lcd_string("--------------------", LCD_LINE_4, 2)
  283.        """
  284.         lcd_byte(0x01, LCD_CMD)
  285.         lcd_string(line1, LCD_LINE_1, 2)
  286.         lcd_string(line2, LCD_LINE_2, 2)
  287.         lcd_string(line3, LCD_LINE_3, 2)
  288.         lcd_string(line4, LCD_LINE_4, 2)
  289.  
  290. GPIO.setwarnings(False)
  291.  
  292. # Define GPIO to LCD mapping
  293. LCD_RS = 7
  294. LCD_E  = 8
  295. LCD_D4 = 25
  296. LCD_D5 = 24
  297. LCD_D6 = 23
  298. LCD_D7 = 18
  299. #LED_ON = 15
  300.  
  301. ##############################################
  302. GPIO.setmode(GPIO.BCM)  # Use BCM GPIO numbers
  303. GPIO.setup(LCD_E, GPIO.OUT)  # E
  304. GPIO.setup(LCD_RS, GPIO.OUT)  # RS
  305. GPIO.setup(LCD_D4, GPIO.OUT)  # DB4
  306. GPIO.setup(LCD_D5, GPIO.OUT)  # DB5
  307. GPIO.setup(LCD_D6, GPIO.OUT)  # DB6
  308. GPIO.setup(LCD_D7, GPIO.OUT)  # DB7
  309. #GPIO.setup(LED_ON, GPIO.OUT)  # Backlight enable
  310. GPIO.setup(14, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
  311. GPIO.setup(15, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
  312. ##############################################
  313.  
  314. # Define some device constants
  315. LCD_WIDTH = 20    # Maximum characters per line
  316. LCD_CHR = True
  317. LCD_CMD = False
  318.  
  319. LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
  320. LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
  321. LCD_LINE_3 = 0x94 # LCD RAM address for the 3rd line
  322. LCD_LINE_4 = 0xD4 # LCD RAM address for the 4th line
  323.  
  324. # Timing constants
  325. E_PULSE = 0.000008
  326. E_DELAY = 0.000008
  327.  
  328.  
  329. readable_clock = "0"
  330. buttonPressed = False
  331.  
  332. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement