Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorC. Scott Ananian <cscott@laptop.org>2007-12-11 09:24:34 (GMT)
committer C. Scott Ananian <cscott@laptop.org>2007-12-11 09:24:34 (GMT)
commit9df72c5591aee015089a4e96f9403183262dcc01 (patch)
tree4a01e662068fe7977dbb4fd14e232fc7354863d4
parent90c54afd7ea81be376b078eb08b87e4f20e71dcf (diff)
Factor out a pippy library, which programs can use with 'import pippy'.
The pygame portion of the library uses 'best practices' to avoid eating up CPU, and pauses & suspends after 20 seconds of inactivity.
-rw-r--r--activity.py8
-rw-r--r--data/graphics/bounce4
-rw-r--r--data/graphics/camera4
-rw-r--r--data/graphics/jump24
-rw-r--r--data/graphics/lines4
-rw-r--r--data/graphics/pong4
-rw-r--r--data/math/guess3
-rw-r--r--data/math/sierpinski6
-rw-r--r--data/sound/getSoundList7
-rw-r--r--data/sound/playSine9
-rw-r--r--data/sound/playWave9
-rw-r--r--data/sound/sequence10
-rw-r--r--data/sound/sndInfo8
-rw-r--r--library/pippy/__init__.py9
-rw-r--r--library/pippy/console.py5
-rw-r--r--library/pippy/game.py61
-rwxr-xr-xlibrary/pippy/sound.py (renamed from sound/sound.py)0
-rw-r--r--sound/__init__.py0
18 files changed, 117 insertions, 58 deletions
diff --git a/activity.py b/activity.py
index 868cc03..ec15a16 100644
--- a/activity.py
+++ b/activity.py
@@ -216,7 +216,13 @@ class PippyActivity(Activity):
file.write(line)
file.close()
- self._pid = self._vte.fork_command("/bin/sh", ["/bin/sh", "-c", "python /tmp/pippy.py; sleep 1"])
+ self._pid = self._vte.fork_command \
+ (command="/bin/sh",
+ argv=["/bin/sh", "-c",
+ "python /tmp/pippy.py; sleep 1"],
+ envv=["PYTHONPATH=%s/library" % get_bundle_path()],
+ directory=get_bundle_path(),
+ lastlog=False, utmp=False, wtmp=False)
def stopbutton_cb(self, button):
os.kill(self._pid, SIGTERM)
diff --git a/data/graphics/bounce b/data/graphics/bounce
index 02bc34f..d0bcd3c 100644
--- a/data/graphics/bounce
+++ b/data/graphics/bounce
@@ -1,6 +1,6 @@
# bounce: move some text around the screen
-import sys,pygame
+import pippy, pygame, sys
from pygame.locals import *
from random import *
@@ -44,7 +44,7 @@ textRect = text.get_rect()
textRect.left = 0;
textRect.top = 0;
-while 1:
+while pippy.pygame.next_frame():
for event in pygame.event.get():
if event.type == QUIT:
diff --git a/data/graphics/camera b/data/graphics/camera
index 8a1bbb8..6eea88f 100644
--- a/data/graphics/camera
+++ b/data/graphics/camera
@@ -1,6 +1,6 @@
# image: take a picture
-import sys, pygame, gst, time
+import gst, pippy, pygame, sys, time
from random import *
# XO screen is 1200 by 900
@@ -32,7 +32,7 @@ image = pygame.image.load("/tmp/pippypic.jpg")
angle = 0.0
scale = 2.0
-while 1:
+while pippy.pygame.next_frame():
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
elif event.type == pygame.KEYDOWN: sys.exit()
diff --git a/data/graphics/jump b/data/graphics/jump
index ed08950..be9b49e 100644
--- a/data/graphics/jump
+++ b/data/graphics/jump
@@ -1,31 +1,27 @@
-def clear_scr():
- print '\x1B[H\x1B[J' # clear screen
-
-def wait():
- import time
- time.sleep(0.1)
+import pippy
for i in xrange(0,50):
- clear_scr()
+ pippy.console.clear()
+ # Note that we have to escape backslashes
print "\\o/"
print "_|_"
print " "
- wait()
+ pippy.wait()
- clear_scr()
+ pippy.console.clear()
print "_o_"
print " | "
print "/ \\"
- wait()
+ pippy.wait()
- clear_scr()
+ pippy.console.clear()
print " o "
print "/|\\"
print "| |"
- wait()
+ pippy.wait()
- clear_scr()
+ pippy.console.clear()
print "_o_"
print " | "
print "/ \\"
- wait()
+ pippy.wait()
diff --git a/data/graphics/lines b/data/graphics/lines
index 682ab37..c0faf93 100644
--- a/data/graphics/lines
+++ b/data/graphics/lines
@@ -1,6 +1,6 @@
# lines: make lots of lines on the screen
-import sys,pygame
+import pippy, pygame, sys
from pygame.locals import *
from random import *
@@ -32,7 +32,7 @@ mvect_end = [choice((-1,1)) * randint(1,3), choice((-1,1)) * randint(1,3)]
color = [randint(0,255), randint(0,255), randint(0,255)]
direction = [choice((-1,1)), choice((-1,1)), choice((-1,1))]
-while 1:
+while pippy.pygame.next_frame():
for event in pygame.event.get():
if event.type == QUIT:
diff --git a/data/graphics/pong b/data/graphics/pong
index 351f7ce..0ce3a9e 100644
--- a/data/graphics/pong
+++ b/data/graphics/pong
@@ -5,7 +5,7 @@
# on the XO, the escape key is the top lefthand key,
# circle with an x in it.
-import sys,pygame
+import pippy, pygame, sys
from pygame.locals import *
from random import *
@@ -50,7 +50,7 @@ textRect = text.get_rect()
textRect.centerx = screen.get_rect().centerx
textRect.centery = screen.get_rect().centery
-while 1:
+while pippy.pygame.next_frame():
# display msg
screen.fill(bgcolor)
diff --git a/data/math/guess b/data/math/guess
index 97ee098..d80514c 100644
--- a/data/math/guess
+++ b/data/math/guess
@@ -1,6 +1,5 @@
import random
-from random import randrange
-R = randrange(1,100)
+R = random.randrange(1,100)
print "Guess a number between 1 and 100!"
N = input("Enter a number: ")
diff --git a/data/math/sierpinski b/data/math/sierpinski
index 133acc4..8b5f291 100644
--- a/data/math/sierpinski
+++ b/data/math/sierpinski
@@ -14,13 +14,13 @@ for i in range(0,lines):
newvector = vector[:]
for j in range(0,len(vector)-1):
if (newvector[j] == 0):
- sys.stdout.write(" ")
+ print " ",
else:
remainder = newvector[j] % modulus
if (remainder == 0):
- sys.stdout.write("O")
+ print "O",
else:
- sys.stdout.write(".")
+ print ".",
newvector[j] = vector[j-1] + vector[j+1]
print
vector = newvector[:]
diff --git a/data/sound/getSoundList b/data/sound/getSoundList
index 215e97a..8d722bb 100644
--- a/data/sound/getSoundList
+++ b/data/sound/getSoundList
@@ -1,7 +1,4 @@
-import sys
-from sugar.activity.activity import get_bundle_path
-sys.path.append(get_bundle_path() + '/sound')
-from sound import *
+import pippy
-for sound in getSoundList():
+for sound in pippy.sound.getSoundList():
print sound
diff --git a/data/sound/playSine b/data/sound/playSine
index cd97576..b68746c 100644
--- a/data/sound/playSine
+++ b/data/sound/playSine
@@ -1,8 +1,5 @@
-import sys
-from sugar.activity.activity import get_bundle_path
-sys.path.append(get_bundle_path() + '/sound')
-from sound import *
+import pippy
-playSine()
-audioOut()
+pippy.sound.playSine()
+pippy.sound.audioOut()
diff --git a/data/sound/playWave b/data/sound/playWave
index 27605bd..503b475 100644
--- a/data/sound/playWave
+++ b/data/sound/playWave
@@ -1,8 +1,5 @@
-import sys
-from sugar.activity.activity import get_bundle_path
-sys.path.append(get_bundle_path() + '/sound')
-from sound import *
+import pippy
-playWave(sound='didjeridu', loop=True, duration=5)
-audioOut()
+pippy.sound.playWave(sound='didjeridu', loop=True, duration=5)
+pippy.sound.audioOut()
diff --git a/data/sound/sequence b/data/sound/sequence
index 1577c9d..3922e7a 100644
--- a/data/sound/sequence
+++ b/data/sound/sequence
@@ -1,14 +1,10 @@
-import sys
-import random
-from sugar.activity.activity import get_bundle_path
-sys.path.append(get_bundle_path() + '/sound')
-from sound import *
+import pippy, random
for i in range(25):
pitch = random.randint(500,2000)
amplitude = 5000
duration = 0.1
starttime = i * 0.1
- playSine(pitch, amplitude, duration, starttime)
-audioOut()
+ pippy.sound.playSine(pitch, amplitude, duration, starttime)
+pippy.sound.audioOut()
diff --git a/data/sound/sndInfo b/data/sound/sndInfo
index 529995e..7767245 100644
--- a/data/sound/sndInfo
+++ b/data/sound/sndInfo
@@ -1,6 +1,2 @@
-import sys
-from sugar.activity.activity import get_bundle_path
-sys.path.append(get_bundle_path() + '/sound')
-from sound import *
-
-print playSine.__doc__
+import pippy
+help(pippy.sound.playSine)
diff --git a/library/pippy/__init__.py b/library/pippy/__init__.py
new file mode 100644
index 0000000..8ddcc7e
--- /dev/null
+++ b/library/pippy/__init__.py
@@ -0,0 +1,9 @@
+"""Pippy standard library."""
+import pippy.console as console
+import pippy.game as pygame
+import pippy.sound as sound
+
+def wait(delay=0.1):
+ """Pause briefly, for animations."""
+ import time
+ time.sleep(delay)
diff --git a/library/pippy/console.py b/library/pippy/console.py
new file mode 100644
index 0000000..cdbb023
--- /dev/null
+++ b/library/pippy/console.py
@@ -0,0 +1,5 @@
+"""Console helpers for pippy."""
+def clear():
+ """Clear screen on console."""
+ # magic escape sequence
+ print '\x1B[H\x1B[J' # clear screen
diff --git a/library/pippy/game.py b/library/pippy/game.py
new file mode 100644
index 0000000..e5996fa
--- /dev/null
+++ b/library/pippy/game.py
@@ -0,0 +1,61 @@
+"""pygame support for pippy."""
+
+import pygame
+
+def pause():
+ # display a "Paused" screen and suspend.
+ caption, icon_caption = pygame.display.get_caption()
+ screen = pygame.display.get_surface()
+ old_screen = screen.copy() # save this for later.
+
+ # dim the screen and display the 'paused' message in the center.
+ BLACK = (0,0,0)
+ WHITE = (255,255,255)
+ dimmed = screen.copy()
+ dimmed.set_alpha(128)
+ screen.fill(WHITE)
+ screen.blit(dimmed, (0,0))
+ font = pygame.font.Font(None, 36) # 36px high
+ msg = " PAUSED "
+ msg_surf = font.render(msg, True, BLACK, WHITE)
+ def center(rect, screen):
+ rect.center = (screen.get_width()/2, screen.get_height()/2)
+
+ rect = pygame.Rect((0,0),msg_surf.get_size())
+ rect.inflate_ip(rect.width, rect.height)
+ center(rect, screen)
+ screen.fill(WHITE, rect)
+ rect = msg_surf.get_rect()
+ center(rect, screen)
+ screen.blit(msg_surf, rect)
+ pygame.display.flip()
+
+ # SUSPEND
+ try:
+ open('/sys/power/state','w').write('mem')
+ except: # XXX: couldn't suspend (no permissions?)
+ pygame.event.post(pygame.event.wait())
+
+ pygame.display.set_caption(caption, icon_caption)
+ screen.blit(old_screen, (0,0))
+ pygame.display.flip()
+
+_last_event_time=0
+def next_frame(max_fps=20, idle_timeout=20, clock=pygame.time.Clock()):
+ """Limit maximum frame rate of pygame. Returns True.
+
+ If idle longer than the idle_timeout (in seconds), then we'll put up a
+ "paused" message and the XO will suspend. This ensures that we don't
+ burn up all of our battery running an animation!"""
+ global _last_event_time
+ clock.tick(max_fps)
+
+ if pygame.event.peek(xrange(pygame.NOEVENT, pygame.USEREVENT)):
+ # we're not idle anymore.
+ _last_event_time = pygame.time.get_ticks()
+ elif (pygame.time.get_ticks() - _last_event_time) >= idle_timeout*1000:
+ # we've been idle for a long time. Pause & suspend.
+ pause()
+ _last_event_time = pygame.time.get_ticks()
+
+ return True
diff --git a/sound/sound.py b/library/pippy/sound.py
index b84cbe6..b84cbe6 100755
--- a/sound/sound.py
+++ b/library/pippy/sound.py
diff --git a/sound/__init__.py b/sound/__init__.py
deleted file mode 100644
index e69de29..0000000
--- a/sound/__init__.py
+++ /dev/null