Viper1953
Posts: 19
Joined: Tue Dec 31, 2013 10:09 am

Repeating Character Issue

Wed Feb 26, 2014 8:58 am

This is another very niggly problem that I cant seem to solve.

I have coded a [very] simple progress bar for use with an oLED display. The bar itself is not 'realtime', but it is threaded with an upload function so it simply repeats untill that function is finished.

Currently it uses the '|' character to display the progress and repeats 30 times at an increment(offset) of 1 pixel each time.

I wish to be able to repeat the character with more than a 1 pixel increment (offset) in order to be able to use different characters (such as '#' for example) that wont overlap.

Here is the current code:

Code: Select all

def oledProgressBar (x,y):
        for i in range(30):
            x1 = x
            x2 = x1 + i
            led.draw_text3(x2,y,"|",font)
            led.display()
            time.sleep(0.1)
        led.clear_block(90,50,128,39)
        led.display()
        pass
I have tried the following (for a 5 pixel increment(offset) but all it does is indent the whole bar by 5 pixels:

Code: Select all

x2 = x1 + i+5
Here is a photo of the progress bar 'mid flow'
progressbar.jpg
progressbar.jpg (54.4 KiB) Viewed 958 times
any help greatly appreciated!

Cheers

Mark

gordon77
Posts: 5077
Joined: Sun Aug 05, 2012 3:12 pm

Re: Repeating Character Issue

Wed Feb 26, 2014 10:37 am

What about

Code: Select all

def oledProgressBar (x,y):
        while I <= 30:
           Z=5
            x1 = x
            x2 = x1 + z
            led.draw_text3(x2,y,"|",font)
            led.display()
            time.sleep(0.1)
             I = I +=z
        led.clear_block(90,50,128,39)
        led.display()
        pass

User avatar
jojopi
Posts: 3274
Joined: Tue Oct 11, 2011 8:38 pm

Re: Repeating Character Issue

Wed Feb 26, 2014 12:01 pm

Viper1953 wrote:x2 = x1 + i+5
x2 = x1 + i*5

(Which is equivalent to the method of adding five each time.)

Viper1953
Posts: 19
Joined: Tue Dec 31, 2013 10:09 am

Re: Repeating Character Issue

Wed Feb 26, 2014 3:15 pm

Hi guys,

Many many thanks for your suggestions. This worked for me:
jojopi wrote:
Viper1953 wrote:x2 = x1 + i+5
x2 = x1 + i*5

(Which is equivalent to the method of adding five each time.)
When I've got all the oLED functions completed I'll post them on here. There are some really usefull ones - this one for a start, but also align text left, center, right, and freely. There is also a neat little function to save writing the whole led.draw_text3(......... thing.

gordon77
Posts: 5077
Joined: Sun Aug 05, 2012 3:12 pm

Re: Repeating Character Issue

Thu Feb 27, 2014 5:08 am

jojopi wrote:
Viper1953 wrote:x2 = x1 + i+5
x2 = x1 + i*5

(Which is equivalent to the method of adding five each time.)
Don't you need to reduce the Range to 6 if stepping I x 5?

Viper1953
Posts: 19
Joined: Tue Dec 31, 2013 10:09 am

Re: Repeating Character Issue

Thu Feb 27, 2014 5:22 am

Hi Gordon,

Yeah, its just a divisible thing. range = 15 if offset = 2 (was 30/1)

I've modified it slightly to accept the range and offset from the function call, and to automatically clear the pixels used no matter where the position. I added loads of comments with a view of posting later anyway:

Code: Select all

def oledProgressBar (x,y,r,o):          # x pos | y pos | number of items (range) | spacing (offset)
        text_height = 16                    # set this according to what size font you are using
        tot_width = r*o                     # multiples the range by the offset to get the total width of a complete bar
        x_st = x                            # sets the x start variable for the clear
        y_st = y                            # sets the y start variable for the clear
        x_e = x+tot_width                   # sets the x end variable for the clear
        y_e = y-text_height                 # sets the y end variable for the clear
            
        for i in range(r):                  #
            x1 = x                          #
            x2 = x1 + i*o                   # repeats the selected character by the specified number of times (range(r))
            led.draw_text3(x2,y,"|",font)   # with the specified gap in between (offset(o))
            led.display()                   #
            time.sleep(0.1)                 #
        
        led.clear_block(x_st,y_st,x_e,y_e)  # after one full progression of the bar the pixels that it uses are then
        led.display()                       # cleared (turned off) so the next iteration of the bar can be seen correctly
        pass
And I call it like this:

Code: Select all

oledProgressBar(90,50,12,3) # x pos | y pos | number of items | spacing
Cheers

Mark

Return to “Python”