pastebin - collaborative debugging tool
smolgraph.kpaste.net RSS


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

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