Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Copilot Lvl 2
Message 1 of 2

Read fader's value and display it (optimization)

Hello there ! I'm sorry for that bad English...

 

I am a French student and I am working on an open DMX lighting console.

 

I writed a programm whitch reads and converts a fader's values and which displays it on a little OLED screen. The problem is that I can't refresh the text on the screen and the only solution that I have found is to delete the object and to create an other one. This solution is not optimized and I would like to know if someone has an other solution.

 

The fader is a Profader MF-914-B10K-10V

The ADC is a MCP3008

The OLED screen is a SSD1306

 

I am using Adafruit_GPIO.SPI, Adafruit_MCP3008, Adafruit_SSD1306 and PIL libs.

 

Here is the code :

 

import time
import Adafruit_GPIO.SPI as SPI
#ADC lib
import Adafruit_MCP3008
#OLED screen lib
import Adafruit_SSD1306
#Displaying libs
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

		###Hardware SPI configuration###
		
SPI_PORT   = 0
SPI_DEVICE = 0
mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))
RST = 0

		###DISPLAY CONFIGURATION###
		
#Create display object 
disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)
#init display
disp.begin()
#Cleat display
disp.clear()
disp.display()
# Create blank image for drawing with mode '1' for 1-bit color.
width = disp.width
height = disp.height
image = Image.new('1', (width, height))
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
#load default font
font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 20)


start=time.time()
raw_value = mcp.read_adc(0)
while True:
	new_raw_value = mcp.read_adc(0)

	#Because of a non stable input voltage, the read value is not constant
	#We need to do a hysteresis
	if new_raw_value < raw_value-5 or new_raw_value  > raw_value+5:
		raw_value = new_raw_value

	raw_value_str = str(raw_value)
	value = (raw_value*100)/1023 
	value_str = str(value)
	#print "The value is", value

	#Draw some text
	draw.text((0,0), value_str, font=font, fill=255)
        draw.text((40,0), "%", font=font, fill=255)
        draw.text((0,30), raw_value_str, font=font, fill=255)
	#Update display
	disp.image(image)
	disp.display()
	#Refresh text
	image = Image.new('1', (width, height))
	draw = ImageDraw.Draw(image)
	draw.text((0,0), " ", font=font, fill=255)
        draw.text((0,30), " ", font=font, fill=255)

I am running this on a raspberry Pi3B

When I run this program, it takes 23% of one CPU's core. Thank you for your help !

1 Reply
Commander Lvl 1
Message 2 of 2

Re: Read fader's value and display it (optimization)

You might take a look at the Adafruit example - in there they clear the image without re-creating it:

 

    # Draw a black filled box to clear the image.
    draw.rectangle((0,0,width,height), outline=0, fill=0)

As to the 23% of CPU issue, the program as written is a tight little loop which is polling the fader as fast as possible.

Does the fader value change that often? Maybe check only once every couple of seconds, 5 seconds, ???

 

Two possibilities to reduce CPU usage, off the top of my head:

1. If the new raw value hasn't changed since the last time you checked, don't do the work of updating the display

2. Use sleep() inside the loop to delay for a certain number of seconds.

 

 

Please follow-up to let us know how you made out. For good karma, mark a reply as the answer if it helped!