- #2018.08.27
- import os
- import pygame
- import time
- import random
- try:
- import RPi.GPIO as GPIO
- except RuntimeError:
- print("Error importing RPi.GPIO! This is probably because you need superuser privileges. You can achieve this by using 'sudo' to run your script")
- #the reason this is not part of pydisplay is that there can be many smolGraphs on one pydisplay
- class smolGraph:
- smolScreen= None
- physicalWidth=0
- physicalHeight=0
- #what is the incoming data going to look like
- minValueX=0
- maxValueX=0
- minValueY=0
- maxValueY=0
- #Where does it live physicall on the screen
- startX=0
- widthX=0
- startY=0
- heightY=0
- #cartesian center relative to the X Y of physical screen
- cartCenterX=0
- cartCenterY=0
- font=None
- def test(self,theValue):
- print(theValue)
- def drawLine(self,x,y,h,v,theColor,lineWidth):
- colorWhite=(255,255,255)
- pygame.draw.line(self.smolScreen,theColor,(x,y),( h,v),lineWidth)
- def graphLine(self,x,y,h,v,theColor,lineWidth):
- x1=self.map(x,self.minValueX,self.maxValueX,self.startX,self.startX+self.widthX)-(self.cartCenterX)
- x2=self.map(h,self.minValueX,self.maxValueX,self.startX,self.startX+self.widthX)-(self.cartCenterX)
- y1=self.map(y,self.minValueY,self.maxValueY,self.startY+self.heightY,self.startY)+self.cartCenterY
- y2=self.map(v,self.minValueY,self.maxValueY,self.startY+self.heightY,self.startY)+self.cartCenterY
- pygame.draw.line(self.smolScreen,theColor,(x1,y1),(x2,y2),lineWidth)
- #print("x="+str(x)+" y="+str(y) + " h="+str(h)+" v="+str(v))
- #print("x1="+str(x1)+" y1="+str(y1) + " x2="+str(x2)+" y2="+str(y2))
- # there should probably be a better way to handle this in the future
- def graphText(self,screenDisplay,theText,x,y,theColor,theSize):
- x1=self.map(x,self.minValueX,self.maxValueX,self.startX,self.startX+self.widthX)-(self.cartCenterX)
- y1=self.map(y,self.minValueY,self.maxValueY,self.startY+self.heightY,self.startY)+self.cartCenterY
- screenDisplay.texts(theText,x1,y1,theColor,theSize)
- def graphCircle(self,x,y,radius,theColor):
- x1=self.map(x,self.minValueX,self.maxValueX,self.startX,self.startX+self.widthX)-(self.cartCenterX)
- y1=self.map(y,self.minValueY,self.maxValueY,self.startY+self.heightY,self.startY)+self.cartCenterY
- pygame.draw.circle(self.smolScreen, theColor, (x1,y1), radius)
- def map(self,value, fromLow, fromHigh, toLow, toHigh):
- fromRange = fromHigh - fromLow
- toRange = toHigh - toLow
- scaleFactor = toRange / fromRange
- tmpValue = value - fromLow
- tmpValue *= scaleFactor
- return tmpValue + toLow
- class pydisplay :
- screen = None;
- sizeX=0
- sizeY=0
- font= None
- def __init__(self):
- "Ininitializes a new pygame screen using the framebuffer"
- # Based on "Python GUI in Linux frame buffer"
- # http://www.karoltomala.com/blog/?p=679
- disp_no = os.getenv("DISPLAY")
- if disp_no:
- print "I'm running under X display = {0}".format(disp_no)
- # Check which frame buffer drivers are available
- # Start with fbcon since directfb hangs with composite output
- drivers = ['fbcon', 'directfb', 'svgalib']
- found = False
- for driver in drivers:
- # Make sure that SDL_VIDEODRIVER is set
- if not os.getenv('SDL_VIDEODRIVER'):
- os.putenv('SDL_VIDEODRIVER', driver)
- try:
- pygame.display.init()
- except pygame.error:
- print 'Driver: {0} failed.'.format(driver)
- continue
- found = True
- break
- if not found:
- raise Exception('No suitable video driver found!')
- size = (pygame.display.Info().current_w, pygame.display.Info().current_h)
- self.sizeX = pygame.display.Info().current_w
- self.sizeY = pygame.display.Info().current_h
- print "Framebuffer size: %d x %d == %d x %d " % (size[0], size[1],self.sizeX,self.sizeY)
- self.screen = pygame.display.set_mode(size, pygame.FULLSCREEN)
- # Clear the screen to start
- self.screen.fill((0, 0, 0))
- # Initialise font support
- pygame.font.init()
- # Render the screen
- pygame.display.update()
- self.font=pygame.font.Font(None,30)
- def __del__(self):
- "Destructor to make sure pygame shuts down, etc."
- def texts(self,theText,x,y,theColor,theSize): #theSize is ignored
- scoretext=self.font.render(str(theText), 1,theColor)
- self.screen.blit(scoretext, (x,y))
- screenDisplay = pydisplay()
- sg=smolGraph()
- sg.smolScreen=screenDisplay.screen
- sg.physicalWidth=0
- sg.physicalHeight=0
- #what is the incoming data going to look like
- sg.minValueX=-100
- sg.maxValueX=100
- sg.minValueY=-100
- sg.maxValueY=100
- #Where does it live physicall on the screen
- sg.startX=100
- sg.widthX=400
- sg.startY=200
- sg.heightY=400
- #cartesian center relative to the X Y of physical screen
- sg.cartCenterX=0
- sg.cartCenterY=0
- ng=smolGraph()
- ng.smolScreen=screenDisplay.screen
- ng.physicalWidth=0
- ng.physicalHeight=0
- #what is the incoming data going to look like
- ng.minValueX=-100
- ng.maxValueX=100
- ng.minValueY=-100
- ng.maxValueY=100
- #Where does it live physicall on the screen
- ng.startX=700
- ng.widthX=400
- ng.startY=200
- ng.heightY=400
- #cartesian center relative to the X Y of physical screen
- ng.cartCenterX=0
- ng.cartCenterY=0
- count=0
- count=sg.minValueX
- while True:
- screenDisplay.screen.fill((0,0,0))
- count=count+(200/30)
- if (count > sg.maxValueX):
- count=sg.minValueX
- for i in range(sg.minValueX, sg.maxValueX+1,10):
- x = i #60+i*50
- sg.graphLine(x,sg.minValueY,x,sg.maxValueY,(96,96,96),1)
- for i in range(sg.minValueY, sg.maxValueY+1,10):
- y = i #60+i*50
- sg.graphLine(sg.minValueX,y,sg.maxValueX,y,(96,96,96),1)
- for i in range(ng.minValueX, ng.maxValueX+1,10):
- x = i #60+i*50
- ng.graphLine(x,ng.minValueY,x,ng.maxValueY,(96,96,96),1)
- for i in range(ng.minValueY, ng.maxValueY+1,10):
- y = i #60+i*50
- ng.graphLine(ng.minValueX,y,ng.maxValueX,y,(96,96,96),1)
- sg.graphLine(0,0,100,100,(0,255,0),4)
- sg.graphLine(0,0,100,-100,(0,0,255),4)
- sg.graphLine(0,0,-100,-100,(255,0,0),4)
- sg.graphLine(0,0,-100,100,(255,255,255),4)
- sg.graphCircle(count,sg.minValueY,8,(255,0,0))
- sg.graphText(screenDisplay,"Hello World",0,0,(255,255,255),12)
- pygame.display.update()
- time.sleep(0.033)#1/30)
Python.smolgraph
Posted by Anonymous on Tue 28th Aug 2018 02:42
raw | new post
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.