Activities/Turtle Art/Programmable Brick: Difference between revisions

Tonyforster (talk | contribs)
 
(23 intermediate revisions by 2 users not shown)
Line 38: Line 38:
[[Image:TA-dotted-line.png]]
[[Image:TA-dotted-line.png]]


def myblock(lc, x):
<pre>
     ###########################################################################
def myblock(tw, line_length):
    #
     ''' Draw a dotted line of length line_length. '''
    # Draw a dotted line of length x.
     try:  # make sure line_length is a number
    #
        line_length = float(line_length)
    ###########################################################################
     try:  # make sure x is a number
      x = float(x)
     except ValueError:
     except ValueError:
         return
         return
     if lc.tw.canvas.pendown:
     if tw.canvas.pendown:
         dist = 0
         dist = 0
         while dist+lc.tw.canvas.pensize < x:   # repeat drawing dots
         while dist + tw.canvas.pensize < line_length: # repeat drawing dots
             lc.tw.canvas.setpen(True)
             tw.canvas.setpen(True)
             lc.tw.canvas.forward(1)
             tw.canvas.forward(1)
             lc.tw.canvas.setpen(False)
             tw.canvas.setpen(False)
             lc.tw.canvas.forward((lc.tw.canvas.pensize*2)-1)
             tw.canvas.forward((tw.canvas.pensize * 2) - 1)
             dist += (lc.tw.canvas.pensize*2)
             dist += (tw.canvas.pensize * 2)
         lc.tw.canvas.forward(x-dist)           # make sure we have moved exactly x
         # make sure we have moved exactly line_length
         lc.tw.canvas.setpen(True)
        tw.canvas.forward(line_length - dist)
         tw.canvas.setpen(True)
     else:
     else:
         lc.tw.canvas.forward(x)
         tw.canvas.forward(line_length)
     return
     return
 
</pre>
You can pass a list of up to three arguments to tamyblock.py as in the example below that converts the input to an rgb value.
You can pass a list of up to three arguments to tamyblock.py as in the example below that converts the input to an rgb value.


[[Image:TA-rgb.png]]
[[Image:TA-rgb.png]]


def myblock(lc, x):
<pre>
     ###########################################################################
def myblock(tw, rgb_array):
    #
     ''' Set rgb color from values '''
    # Set rgb color
     tw.canvas.fgrgb = [(int(rgb_array[0]) % 256),
     #
                      (int(rgb_array[1]) % 256),
    ###########################################################################
                      (int(rgb_array[2]) % 256)]
    r = int(x[0])
</pre>
    while r < 0:
        r += 256
    while r > 255:
        r -= 256
    g = int(x[1])
    while g < 0:
        g += 256
    while g > 255:
        g -= 256
    b = int(x[0])
    while b < 0:
        b += 256
    while b > 255:
        b -= 256
    rgb = "#%02x%02x%02x" % (r,g,b)
    lc.tw.fgcolor = lc.tw.canvas.cm.alloc_color(rgb)
    return


  def myblock(lc, x):
  def myblock(tw, x):
     ###########################################################################
     ###########################################################################
     #
     #
Line 102: Line 83:
     else:
     else:
         X = x.upper()
         X = x.upper()
     lc.heap.append(X)
     tw.lc.heap.append(X)
     return
     return


  def myblock(lc, x):
  def myblock(tw, x):
     ###########################################################################
     ###########################################################################
     #
     #
Line 114: Line 95:
     #
     #
     ###########################################################################
     ###########################################################################
     lc.heap.append(localtime().tm_hour)
     tw.lc.heap.append(localtime().tm_hour)
     lc.heap.append(localtime().tm_min)
     tw.lc.heap.append(localtime().tm_min)
     lc.heap.append(localtime().tm_sec)
     tw.lc.heap.append(localtime().tm_sec)
     return
     return


  def myblock(lc, x):
  def myblock(tw, x):
     ###########################################################################
     ###########################################################################
     #
     #
Line 125: Line 106:
     #
     #
     ###########################################################################
     ###########################################################################
     val = 0.3 * lc.tw.rgb[0] + 0.6 * lc.tw.rgb[1] + 0.1 * lc.tw.rgb[2]
     val = 0.3 * tw.rgb[0] + 0.6 * tw.rgb[1] + 0.1 * tw.rgb[2]
     if x != 100:
     if x != 100:
         x = int(x)%100
         x = int(x)%100
     r = int((val*(100-x) + lc.tw.rgb[0]*x)/100)
     r = int((val*(100-x) + tw.rgb[0]*x)/100)
     g = int((val*(100-x) + lc.tw.rgb[1]*x)/100)
     g = int((val*(100-x) + tw.rgb[1]*x)/100)
     b = int((val*(100-x) + lc.tw.rgb[2]*x)/100)
     b = int((val*(100-x) + tw.rgb[2]*x)/100)
     # reallocate current color
     # reallocate current color
     rgb = "#%02x%02x%02x" % (r,g,b)
     rgb = "#%02x%02x%02x" % (r,g,b)
     lc.tw.fgcolor = lc.tw.canvas.cm.alloc_color(rgb)
     tw.fgcolor = tw.canvas.cm.alloc_color(rgb)
     return
     return


  def myblock(lc, x):
  def myblock(tw, x):
     ###########################################################################
     ###########################################################################
     #
     #
Line 142: Line 123:
     #
     #
     ###########################################################################
     ###########################################################################
     lc.tw.save_as_image(str(x))
     tw.save_as_image(str(x))
     return
     return


def myblock(lc, x):
def myblock(tw, x):
 
     ###########################################################################
     ###########################################################################
     #
     #
Line 152: Line 132:
     #
     #
     ###########################################################################
     ###########################################################################
 
     if tw.mouse_flag == 1:
     if lc.tw.mouse_flag == 1:
         # push y first so x will be popped first
         # push y first so x will be popped first
         lc.heap.append((lc.tw.canvas.height / 2) - lc.tw.mouse_y)
         tw.lc.heap.append((tw.canvas.height / 2) - tw.mouse_y)
         lc.heap.append(lc.tw.mouse_x - (lc.tw.canvas.width / 2))
         tw.lc.heap.append(tw.mouse_x - (tw.canvas.width / 2))
         lc.heap.append(1) # mouse event
         tw.lc.heap.append(1) # mouse event
         lc.tw.mouse_flag = 0
         tw.mouse_flag = 0
     else:
     else:
         lc.heap.append(0) # no mouse event
         tw.lc.heap.append(0) # no mouse event


====Device I/O====
====Device I/O====
This Python block returns with the brightness sensor value in the heap.
This Python block returns with the brightness sensor value in the heap.
A range of parameters can be measured, for example, substitute any of
A range of parameters can be measured, for example, substitute any of
these strings for the string in the program below.
the path strings in the table for the 'device' in the program below.


* /sys/devices/platform/olpc-battery.0/power_supply/olpc-battery/current_now
 
* /sys/devices/platform/olpc-battery.0/power_supply/olpc-battery/voltage_now
{| border=1 cellpadding=3 style="border: 1px solid white; border-collapse: collapse; background: #e3e4e5;"
* /sys/devices/platform/dcon/backlight/dcon-bl/actual_brightness
|-style="background:#787878; color: white;"
! device !! path !! notes
|-
| battery current || /sys/devices/platform/olpc-battery.0/power_supply/olpc-battery/current_now || OLPC XO
|-
| battery voltage || /sys/devices/platform/olpc-battery.0/power_supply/olpc-battery/voltage_now || OLPC XO
|-
| screen brightness || /sys/devices/platform/dcon/backlight/dcon-bl/actual_brightness || OLPC XO
|-
| accelerometer || /sys/devices/platform/lis3lv02d/position || OLPC XO 1.75, 3.0
|-
| light sensor || /sys/devices/platform/olpc-ols.0/level || OLPC XO 1.75, 3.0
|-
| screen mode || /sys/devices/platform/dcon/monochrome || OLPC XO 1.75, 3.0
|-
| charging || /sys/devices/platform/olpc-battery.0/power_supply/olpc-ac/online || OLPC XO
|-
| clock date|| /sys/devices/platform/pxa2xx-i2c.1/i2c-1/1-0068/rtc/rtc0/date || OLPC XO 1.75
|-
| clock time|| /sys/devices/platform/pxa2xx-i2c.1/i2c-1/1-0068/rtc/rtc0/time || OLPC XO 1.75
|-
|cpu temperature|| /sys/devices/platform/via_cputemp.0/temp1_input || OLPC XO 1.5
|}




Line 176: Line 177:
    
    
     # The light sensor is only available on the XO 1.75
     # The light sensor is only available on the XO 1.75
     device = '/sys/devices/platform/olpc-ols.0/power_state'
     device = '/sys/devices/platform/olpc-ols.0/level'
      
      
     if os.path.exists(device):
     if os.path.exists(device):
Line 182: Line 183:
         string = fh.read()
         string = fh.read()
         fh.close()
         fh.close()
         tw.lc.heap.append(int(string))  # append as integer value to heap
         tw.lc.heap.append(float(string))  # append as float value to heap
     else:
     else:
         tw.lc.heap.append(-1)
         tw.lc.heap.append(-1)
 
 
If you can work out how to use them...
Look in /sys/devices on your computer to find other devices you may be able to access.
 
You can also read events in /dev/input . Use the od (octal dump) command to inspect these events, eg
sudo od /dev/input/event0
 
You can access the following events on the X0-1.75, (some on X0-1.0 -1.5)
* accelerometer: /dev/input/event0 ???
* accelerometer: /dev/input/event0 ???
* power button: /dev/input/event1
* power button: /dev/input/event1
* lid switch: /dev/input/event2
* lid switch: /dev/input/event2
* ebook: /dev/input/event3
* ebook: /dev/input/event3 (XO-1.75) event4 (XO-1.0 - 1.5)
* headphone jack: /dev/input/event7
* headphone jack: /dev/input/event7
* microphone jack: /dev/input/event8
* microphone jack: /dev/input/event8
* rotate, cursor, and game pad keys: /dev/input/event10
* rotate, cursor, and game pad keys: /dev/input/event10 (XO-1.75) event6 (XO-1.0 - 1.5)
* mouse : /dev/input/mice
 
 
For example, the code below waits till the ebook switch state changes and pushes the status to the heap
 
def myblock(tw, x):  # ignores second argument
 
  import os
  devicestr='/dev/input/event3'              #the ebook switch
  cmd='sudo chmod 777 {!s}'.format(devicestr)
  os.system(cmd)                            #caution! changing system file permissions
  fd = open(devicestr, 'rb')
  for x in range(12):
      fd.read(1)                              #does not return till the switch state changes
  tw.lc.heap.append( ord(fd.read(1)))        #push ebook switch state to heap
  fd.close


===Understanding the structure of the Turtle Art program===
===Understanding the structure of the Turtle Art program===