pastebin - collaborative debugging tool
smolgraph.kpaste.net RSS


Python.smolgraph
Posted by Anonymous on Tue 28th Aug 2018 02:42
raw | new post

  1. #2018.08.27
  2. import os
  3. import pygame
  4. import time
  5. import random
  6.  
  7. try:
  8.     import RPi.GPIO as GPIO
  9. except RuntimeError:
  10.     print("Error importing RPi.GPIO!  This is probably because you need superuser privileges.  You can achieve this by using 'sudo' to run your script")
  11.  
  12. #the reason this is not part of pydisplay is that there can be many smolGraphs on one pydisplay
  13. class smolGraph:
  14.     smolScreen= None
  15.  
  16.     physicalWidth=0
  17.     physicalHeight=0
  18.  
  19.     #what is the incoming data going to look like
  20.     minValueX=0
  21.     maxValueX=0
  22.     minValueY=0
  23.     maxValueY=0
  24.  
  25.     #Where does it live physicall on the screen
  26.     startX=0
  27.     widthX=0
  28.     startY=0
  29.     heightY=0
  30.  
  31.     #cartesian center relative to the X Y of physical screen
  32.     cartCenterX=0
  33.     cartCenterY=0
  34.  
  35.     font=None
  36.  
  37.     def test(self,theValue):
  38.         print(theValue)
  39.  
  40.     def drawLine(self,x,y,h,v,theColor,lineWidth):
  41.         colorWhite=(255,255,255)
  42.         pygame.draw.line(self.smolScreen,theColor,(x,y),( h,v),lineWidth)
  43.  
  44.     def graphLine(self,x,y,h,v,theColor,lineWidth):
  45.         x1=self.map(x,self.minValueX,self.maxValueX,self.startX,self.startX+self.widthX)-(self.cartCenterX)
  46.         x2=self.map(h,self.minValueX,self.maxValueX,self.startX,self.startX+self.widthX)-(self.cartCenterX)
  47.         y1=self.map(y,self.minValueY,self.maxValueY,self.startY+self.heightY,self.startY)+self.cartCenterY
  48.         y2=self.map(v,self.minValueY,self.maxValueY,self.startY+self.heightY,self.startY)+self.cartCenterY
  49.         pygame.draw.line(self.smolScreen,theColor,(x1,y1),(x2,y2),lineWidth)
  50.         #print("x="+str(x)+" y="+str(y) + " h="+str(h)+" v="+str(v))
  51.         #print("x1="+str(x1)+" y1="+str(y1) + " x2="+str(x2)+" y2="+str(y2))
  52.  
  53.         # there should probably be a better way to handle this in the future
  54.     def graphText(self,screenDisplay,theText,x,y,theColor,theSize):
  55.         x1=self.map(x,self.minValueX,self.maxValueX,self.startX,self.startX+self.widthX)-(self.cartCenterX)
  56.         y1=self.map(y,self.minValueY,self.maxValueY,self.startY+self.heightY,self.startY)+self.cartCenterY
  57.         screenDisplay.texts(theText,x1,y1,theColor,theSize)
  58.  
  59.     def graphCircle(self,x,y,radius,theColor):
  60.         x1=self.map(x,self.minValueX,self.maxValueX,self.startX,self.startX+self.widthX)-(self.cartCenterX)
  61.         y1=self.map(y,self.minValueY,self.maxValueY,self.startY+self.heightY,self.startY)+self.cartCenterY
  62.         pygame.draw.circle(self.smolScreen, theColor, (x1,y1), radius)
  63.  
  64.     def map(self,value, fromLow, fromHigh, toLow, toHigh):
  65.         fromRange   = fromHigh - fromLow
  66.         toRange     = toHigh   - toLow
  67.         scaleFactor = toRange  / fromRange
  68.         tmpValue    = value    - fromLow
  69.         tmpValue    *= scaleFactor
  70.         return tmpValue + toLow
  71.  
  72. class pydisplay :
  73.     screen = None;
  74.     sizeX=0
  75.     sizeY=0
  76.     font= None
  77.  
  78.     def __init__(self):
  79.         "Ininitializes a new pygame screen using the framebuffer"
  80.         # Based on "Python GUI in Linux frame buffer"
  81.         # http://www.karoltomala.com/blog/?p=679
  82.         disp_no = os.getenv("DISPLAY")
  83.         if disp_no:
  84.             print "I'm running under X display = {0}".format(disp_no)
  85.        
  86.         # Check which frame buffer drivers are available
  87.         # Start with fbcon since directfb hangs with composite output
  88.         drivers = ['fbcon', 'directfb', 'svgalib']
  89.         found = False
  90.         for driver in drivers:
  91.             # Make sure that SDL_VIDEODRIVER is set
  92.             if not os.getenv('SDL_VIDEODRIVER'):
  93.                 os.putenv('SDL_VIDEODRIVER', driver)
  94.             try:
  95.                 pygame.display.init()
  96.             except pygame.error:
  97.                 print 'Driver: {0} failed.'.format(driver)
  98.                 continue
  99.             found = True
  100.             break
  101.    
  102.         if not found:
  103.             raise Exception('No suitable video driver found!')
  104.        
  105.         size = (pygame.display.Info().current_w, pygame.display.Info().current_h)
  106.         self.sizeX = pygame.display.Info().current_w
  107.         self.sizeY = pygame.display.Info().current_h
  108.         print "Framebuffer size: %d x %d == %d x %d " % (size[0], size[1],self.sizeX,self.sizeY)
  109.         self.screen = pygame.display.set_mode(size, pygame.FULLSCREEN)
  110.         # Clear the screen to start
  111.         self.screen.fill((0, 0, 0))        
  112.         # Initialise font support
  113.         pygame.font.init()
  114.         # Render the screen
  115.         pygame.display.update()
  116.         self.font=pygame.font.Font(None,30)
  117.        
  118.     def __del__(self):
  119.         "Destructor to make sure pygame shuts down, etc."
  120.  
  121.     def texts(self,theText,x,y,theColor,theSize): #theSize is ignored
  122.         scoretext=self.font.render(str(theText), 1,theColor)
  123.         self.screen.blit(scoretext, (x,y))
  124.        
  125.  
  126.  
  127. screenDisplay = pydisplay()
  128.  
  129. sg=smolGraph()
  130. sg.smolScreen=screenDisplay.screen
  131. sg.physicalWidth=0
  132. sg.physicalHeight=0
  133. #what is the incoming data going to look like
  134. sg.minValueX=-100
  135. sg.maxValueX=100
  136. sg.minValueY=-100
  137. sg.maxValueY=100
  138. #Where does it live physicall on the screen
  139. sg.startX=100
  140. sg.widthX=400
  141. sg.startY=200
  142. sg.heightY=400
  143. #cartesian center relative to the X Y of physical screen
  144. sg.cartCenterX=0
  145. sg.cartCenterY=0
  146.  
  147.  
  148. ng=smolGraph()
  149. ng.smolScreen=screenDisplay.screen
  150. ng.physicalWidth=0
  151. ng.physicalHeight=0
  152. #what is the incoming data going to look like
  153. ng.minValueX=-100
  154. ng.maxValueX=100
  155. ng.minValueY=-100
  156. ng.maxValueY=100
  157. #Where does it live physicall on the screen
  158. ng.startX=700
  159. ng.widthX=400
  160. ng.startY=200
  161. ng.heightY=400
  162. #cartesian center relative to the X Y of physical screen
  163. ng.cartCenterX=0
  164. ng.cartCenterY=0
  165.  
  166.  
  167.  
  168. count=0
  169.  
  170. count=sg.minValueX
  171.  
  172. while True:
  173.     screenDisplay.screen.fill((0,0,0))
  174.     count=count+(200/30)
  175.     if (count > sg.maxValueX):
  176.         count=sg.minValueX
  177.  
  178.     for i in range(sg.minValueX, sg.maxValueX+1,10):
  179.         x = i #60+i*50
  180.         sg.graphLine(x,sg.minValueY,x,sg.maxValueY,(96,96,96),1)
  181.  
  182.     for i in range(sg.minValueY, sg.maxValueY+1,10):
  183.         y = i #60+i*50
  184.         sg.graphLine(sg.minValueX,y,sg.maxValueX,y,(96,96,96),1)
  185.    
  186.     for i in range(ng.minValueX, ng.maxValueX+1,10):
  187.         x = i #60+i*50
  188.         ng.graphLine(x,ng.minValueY,x,ng.maxValueY,(96,96,96),1)
  189.  
  190.     for i in range(ng.minValueY, ng.maxValueY+1,10):
  191.         y = i #60+i*50
  192.         ng.graphLine(ng.minValueX,y,ng.maxValueX,y,(96,96,96),1)
  193.    
  194.     sg.graphLine(0,0,100,100,(0,255,0),4)
  195.     sg.graphLine(0,0,100,-100,(0,0,255),4)
  196.     sg.graphLine(0,0,-100,-100,(255,0,0),4)
  197.     sg.graphLine(0,0,-100,100,(255,255,255),4)
  198.  
  199.     sg.graphCircle(count,sg.minValueY,8,(255,0,0))
  200.  
  201.     sg.graphText(screenDisplay,"Hello World",0,0,(255,255,255),12)
  202.  
  203.     pygame.display.update()
  204.     time.sleep(0.033)#1/30)

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at