Facebook
From PussyHounter69, 5 Years ago, written in Python.
Embed
Download Paste or View Raw
Hits: 245
  1. import time
  2. import math
  3. import Adafruit_Nokia_LCD as LCD
  4. import Adafruit_GPIO.SPI as SPI
  5.  
  6. from PIL import Image
  7. from PIL import ImageFont
  8. from PIL import ImageDraw
  9.  
  10. # Set fonts
  11. font_speed = ImageFont.truetype('pixelmix.ttf', 28)
  12. font_kmh = ImageFont.truetype('pixelmix.ttf', 8)
  13. font_parameter = ImageFont.truetype('pixelmix.ttf', 16)
  14.  
  15. class DisplaySetter(object):
  16.     ## Raspberry Pi hardware SPI config:
  17.     DC = 23
  18.     RST = 24
  19.     SPI_PORT = 0
  20.     SPI_DEVICE = 0
  21.  
  22.     # Hardware SPI usage:
  23.     disp = LCD.PCD8544(DC, RST, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE, max_speed_hz=4000000))
  24.  
  25.     image = Image.new('1', (LCD.LCDWIDTH, LCD.LCDHEIGHT))
  26.  
  27.     draw = ImageDraw.Draw(image)
  28.  
  29.     def __init__(self):
  30.         # Initialize library.
  31.         self.disp.begin(contrast=50)
  32.        
  33.         # Clear display
  34.         self.disp.clear()
  35.         self.disp.display()
  36.        
  37.         # Draw a white filled box to clear the image
  38.         self.draw.rectangle((0, 0, LCD.LCDWIDTH, LCD.LCDHEIGHT), outline=255, fill=255)
  39.    
  40.     # Variables in use
  41.     speed = 85
  42.     distance = 10.3
  43.     laptime = 430.0
  44.     screen = 0  # flag that informs which screen to show (for example: distance, time, etc)
  45.     signalbar = False # true if there is signal, false otherwise
  46.     currentposition = False # false if you have to speed up, true if you are faster than before
  47.  
  48.     def printmenu(self):
  49.         # km/h
  50.         self.draw.text((62, 17), 'km/h', font=font_kmh)
  51.  
  52.         # left menu (rectangle)
  53.         self.draw.line((13, 0, 13, 47), fill=0)
  54.         self.draw.line((0, 0, 0, 47), fill=0)
  55.         self.draw.line((0, 0, 13, 0), fill=0)
  56.         self.draw.line((0, 47, 13, 47), fill=0)
  57.  
  58.         # left menu (?shelves?)
  59.         self.draw.line((0, 12, 13, 12), fill=0)
  60.         self.draw.line((0, 24, 13, 24), fill=0)
  61.         self.draw.line((0, 36, 13, 36), fill=0)
  62.        
  63.             #print letters in submenu
  64.         self.draw.text((4, 2), 'S', font=font_kmh)
  65.         self.draw.text((4, 14), 'T', font=font_kmh)
  66.         self.draw.text((4, 26), 'L', font=font_kmh)
  67.         self.draw.line((6, 39, 6, 44), fill=0)
  68.  
  69.  
  70.     def printspeed(self):
  71.         # Clear the speed
  72.         self.draw.rectangle((17, 0, 61, 28), outline=255, fill=255)
  73.  
  74.         # Print the speed
  75.         self.draw.text((17, -3), str(self.speed), font=font_speed)
  76.  
  77.         ## Test of speed
  78.         #self.speed += 5
  79.         #if self.speed > 80:
  80.         #    self.speed = 1
  81.         ## End of the test
  82.         return
  83.  
  84.     def nextscreen(self):
  85.         #Changes the flag of a screen after clicking the button
  86.         self.screen += 1
  87.         if self.screen == 4:
  88.             self.screen = 0
  89.        
  90.             #Lines below print currently chosen submenu
  91.         if self.screen == 0:
  92.             self.draw.rectangle((1, 1, 12, 11), outline=0, fill=255)
  93.             self.draw.rectangle((1, 37, 12, 46), outline=255, fill=255)
  94.             self.draw.text((4, 2), 'S', font=font_kmh)
  95.             self.draw.line((6, 39, 6, 44), fill=0)
  96.         elif self.screen == 1:
  97.             self.draw.rectangle((1, 1, 12, 11), outline=255, fill=255)
  98.             self.draw.rectangle((1, 13, 12, 23), outline=0, fill=255)
  99.             self.draw.text((4, 14), 'T', font=font_kmh)
  100.             self.draw.text((4, 2), 'S', font=font_kmh)
  101.         elif self.screen == 2:
  102.             self.draw.rectangle((1, 13, 12, 23), outline=255, fill=255)
  103.             self.draw.rectangle((1, 25, 12, 35), outline=0, fill=255)
  104.             self.draw.text((4, 26), 'L', font=font_kmh)
  105.             self.draw.text((4, 14), 'T', font=font_kmh)
  106.         elif self.screen == 3:
  107.             self.draw.rectangle((1, 25, 12, 35), outline=255, fill=255)
  108.             self.draw.rectangle((1, 37, 12, 46), outline=0, fill=255)
  109.             self.draw.line((6, 39, 6, 44), fill=0)
  110.             self.draw.text((4, 26), 'L', font=font_kmh)
  111.  
  112.  
  113.     def printdistance(self):
  114.             ## Used for test
  115.         #self.distance+=0.25
  116.             #if self.distance > 99:
  117.                    # self.distance -= 99
  118.                
  119.         # Clear the display
  120.             self.draw.rectangle((14, 26, 83, 47), outline=255, fill=255)
  121.             self.draw.text((70, 35), 'km', font=font_kmh)
  122.         # Print the distance
  123.             self.draw.text((17, 28), str(self.distance), font=font_parameter)
  124.  
  125.     def printlaptime(self):
  126.         # Clear the display
  127.             self.draw.rectangle((14, 26, 83, 47), outline=255, fill=255)
  128.             self.draw.text((75, 35), 's', font=font_kmh)
  129.         # Print lap time
  130.             self.draw.text((17, 28), str(self.laptime), font=font_parameter)
  131.        
  132.     def something1(self):
  133.         # Clear the display
  134.             self.draw.rectangle((14, 26, 83, 47), outline=255, fill=255)
  135.             self.draw.text((70, 35), '', font=font_kmh)
  136.         # Print sth
  137.             self.draw.text((17, 28), str('sth :('), font=font_parameter)
  138.  
  139.     def something2(self):
  140.         # Clear the display
  141.             self.draw.rectangle((14, 26, 83, 47), outline=255, fill=255)
  142.             self.draw.text((70, 35), '', font=font_kmh)
  143.         # Print sth
  144.             self.draw.text((17, 28), str('sth :)'), font=font_parameter)
  145.  
  146.     def printsubmenu(self):
  147.             if self.screen == 0:
  148.                     self.printdistance()
  149.             elif self.screen == 1:
  150.                     self.printlaptime()
  151.             elif self.screen == 2:
  152.                     self.something1()
  153.             else:
  154.                     self.something2()
  155.  
  156.     def printsignalbar(self): #prints gps signal
  157.         if self.signalbar == True:
  158.             self.draw.rectangle((62, 0, 69, 8), outline=255, fill=255)
  159.             self.draw.rectangle((62, 4, 63, 8), outline=0, fill=0)
  160.             self.draw.rectangle((65, 2, 66, 8), outline=0, fill=0)
  161.             self.draw.rectangle((68, 0, 69, 8), outline=0, fill=0)
  162.         elif self.signalbar == False:
  163.             self.draw.rectangle((62, 0, 69, 8), outline=255, fill=255)
  164.             self.draw.text((64, 1), 'X', font=font_kmh)
  165.         # Used for tests
  166.         #self.signalbar = not self.signalbar
  167.  
  168.     def printcurrentposition(self): #prints information about current position
  169.         if self.currentposition == False:
  170.             self.draw.rectangle((72, 0, 83, 15), outline=255, fill=255)
  171.             #draw SPEEDUP symbol
  172.             self.draw.rectangle((76, 4, 79, 15), outline=0, fill=0)
  173.             self.draw.line((73, 4, 82, 4), fill=0)
  174.             self.draw.line((74, 3, 81, 3), fill=0)
  175.             self.draw.line((75, 2, 80, 2), fill=0)
  176.             self.draw.line((76, 1, 79, 1), fill=0)
  177.             self.draw.line((77, 0, 78, 0), fill=0)
  178.         elif self.currentposition == True:
  179.             self.draw.rectangle((72, 0, 83, 15), outline=255, fill=255)    
  180.             #draw SLOWDWON symbol
  181.             self.draw.rectangle((76, 0, 79, 10), outline=0, fill=0)
  182.             self.draw.line((73, 11, 82, 11), fill=0)
  183.             self.draw.line((74, 12, 81, 12), fill=0)
  184.             self.draw.line((75, 13, 80, 13), fill=0)
  185.             self.draw.line((76, 14, 79, 14), fill=0)
  186.             self.draw.line((77, 15, 78, 15), fill=0)
  187.        
  188.         # Used for test
  189.         #self.currentposition = not self.currentposition
  190.  
  191.     # SETTERS for speed, distance, laptime
  192.     # TOGGLERS for signalbar currentposition
  193.     # TO TURN SWITCH TO NEXT SCREEN USE displayInUse.nextscreen()
  194.     def setspeed(self, new):
  195.         self.speed = new
  196.  
  197.     def setdistance(self, new):
  198.         self.distance = new
  199.  
  200.     def setlaptime(self, new):
  201.         self.laptime = new
  202.  
  203.     def togglesignalbar(self):
  204.         self.signalbar = not self.signalbar
  205.  
  206.     def togglecurrentposition(self):
  207.         self.currentposition = not self.currentposition
  208.  
  209.  
  210. displayInUse = DisplaySetter()
  211.  
  212. print('It works (Press Ctrl+C to stop)')
  213. displayInUse.printmenu()
  214.  
  215. while True:
  216.         displayInUse.printspeed()
  217.         displayInUse.printsubmenu()
  218.         displayInUse.printsignalbar()
  219.         displayInUse.printcurrentposition()
  220.         displayInUse.disp.image(displayInUse.image)
  221.         displayInUse.disp.display()
  222.         displayInUse.nextscreen() # Used to tests
  223.         time.sleep(1)