Skip to content
This repository was archived by the owner on Mar 23, 2019. It is now read-only.

Commit

Permalink
Incomplete - task : Raspberry Pi Experiments
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Nov 25, 2012
1 parent f3f1ce6 commit bcd8fcc
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ AdaPi

Python ports of Adafruit libraries for use with the Raspberry Pi.

Contains ported libraries from [Adafruit] (http://learn.adafruit.com) and other useful software.
Contains ported libraries from [Adafruit] (http://learn.adafruit.com) and other useful software.
2 changes: 1 addition & 1 deletion adafruit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This is a pure-python port of the Adafruit GFX c library:

This python clone is hosted here:

https://github.com/andig/py-Adafruit-GFX-Library
https://github.com/andig/AdaPi

Some enhancements like the draw_text and draw_text3 methods have
been added from the py-gaugette library
Expand Down
24 changes: 12 additions & 12 deletions adafruit/adafruitgfx.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,12 @@ def draw_pixel(self, x, y, color):
def draw_line(self, x0, y0, x1, y1, color):
steep = abs(y1 - y0) > abs(x1 - x0)
if (steep):
swap(x0, y0)
swap(x1, y1)
x0, y0 = y0, x0
x1, y1 = y1, x1

if (x0 > x1):
swap(x0, x1)
swap(y0, y1)
x0, x1 = x1, x0
y0, y1 = y1, y0

dx = x1 - x0
dy = abs(y1 - y0)
Expand Down Expand Up @@ -241,16 +241,16 @@ def fill_triangle (self, x0, y0, x1, y1, x2, y2, color):

# Sort coordinates by Y order (y2 >= y1 >= y0)
if (y0 > y1):
swap(y0, y1)
swap(x0, x1)
y0, y1 = y1, y0
x0, x1 = x1, x0

if (y1 > y2):
swap(y2, y1)
swap(x2, x1)
y2, y1 = y1, y2
x2, x1 = x1, x2

if (y0 > y1):
swap(y0, y1)
swap(x0, x1)
y0, y1 = y1, y0
x0, x1 = x1, x0

# Handle awkward all-on-same-line case as its own thing
if (y0 == y2):
Expand Down Expand Up @@ -293,7 +293,7 @@ def fill_triangle (self, x0, y0, x1, y1, x2, y2, color):
sa += dx01
sb += dx02
if(a > b):
swap(a,b)
a,b = b,a
self.draw_fast_hline(a, y, b-a+1, color)

# For lower part of triangle, find scanline crossings for segments
Expand All @@ -308,7 +308,7 @@ def fill_triangle (self, x0, y0, x1, y1, x2, y2, color):
sa += dx12
sb += dx02
if(a > b):
swap(a,b)
a,b = b,a
self.draw_fast_hline(a, y, b-a+1, color)
y+=1

Expand Down
61 changes: 57 additions & 4 deletions gaugette/ssd1306.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,61 @@ def draw_pixel(self, x, y, on=True):
self.buffer[offset] &= (0xFF - bit_mask)


# overwritten from adafruitgfx
def draw_fast_vline(self, x, y, h, color=1):
if (x<0 or x>=self.buffer_cols or y<0 or y+h>self.buffer_rows):
return
if (h<8):
self.draw_line(x, y, x, y+h-1, color)
return

mem_row_start = y / 8
mem_row_end = (y+h) / 8
x *= self.buffer_rows/8

# line start
bit_mask = (0xff << (y % 8)) & 0xff
offset = mem_row_start + x

if color:
self.buffer[offset] |= bit_mask
else:
self.buffer[offset] &= (0xFF - bit_mask)

# line end
b = 8 - (y+h) % 8
bit_mask = 0xff >> (8 - (y+h) % 8)
offset = mem_row_end + x

if color:
self.buffer[offset] |= bit_mask
else:
self.buffer[offset] &= (0xFF - bit_mask)

# line middle
for y in range(mem_row_start+1, mem_row_end):
offset = y + x
if color:
self.buffer[offset] = 0xFF
else:
self.buffer[offset] = 0


# overwritten from adafruitgfx
def draw_fast_hline(self, x, y, w, color=1):
# stupidest version - update in subclasses if desired!
if (x<0 or x+w>self.buffer_cols or y<0 or y>=self.buffer_rows):
return
mem_row = y / 8
bit_mask = 1 << (y % 8)

for offset in range(mem_row + self.buffer_rows/8 * x, mem_row + self.buffer_rows/8 * (x+w), self.buffer_rows/8):
if color:
self.buffer[offset] |= bit_mask
else:
self.buffer[offset] &= (0xFF - bit_mask)


# use fillrect instead
def clear_block(self, x0,y0,dx,dy):
for x in range(x0,x0+dx):
for y in range(y0,y0+dy):
self.draw_pixel(x,y,0)

fill_rect(x0,y0,dx,dy,0)
25 changes: 20 additions & 5 deletions test/test_adafruitgfx.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
import gaugette.ssd1306 as ssd1306
import fonts.arial_regular_10

ssd1306 = ssd1306.SSD1306()
ssd1306 = ssd1306.SSD1306(buffer_rows=32, buffer_cols=128)

#ssd1306.draw_line(0,0,ssd1306.cols,ssd1306.rows,1)
#ssd1306.draw_circle(16,18,12,1)

#for i in range(0,16):
# ssd1306.draw_fast_vline(i, i, ssd1306.buffer_rows-2*i)

#for i in range(0,16):
# ssd1306.draw_fast_hline(i, i, ssd1306.buffer_cols-2*i)

ssd1306.draw_fast_vline(0, 0, 1)
ssd1306.fill_rect(110, 8, 3, 3, 1)

#for i in range(0,4):
# ssd1306.draw_fast_hline(110, i, 10)
# ssd1306.draw_fast_vline(120-i, 0, ssd1306.rows)

#x = ssd1306.draw_text(30,0,"Test")
#ssd1306.draw_text3(x,0,"Test", fonts.arial_regular_10)

ssd1306.draw_line(0,0,ssd1306.cols,ssd1306.rows,1)
ssd1306.draw_circle(16,16,12,1)
x = ssd1306.draw_text(30,0,"Test")
ssd1306.draw_text3(x,0,"Test", fonts.arial_regular_10)
ssd1306.dump_buffer()

0 comments on commit bcd8fcc

Please sign in to comment.