Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Using Bluetooth to control Asterisk calls.

Redirect an Asterisk call

    de61.old_status = get_status(de61.file) # found is either true or false
    de61.old_location = get_location(de61.file) # location is either 'at home', 'gone to bed' or 'asleep'
    
    while count < 1

      puts 'old status : ' + de61.old_status.to_s
      de61.new_status = check_device(de61.id, de61.name)
      puts 'new_status: ' + de61.new_status.to_s


      if de61.new_status and de61.old_status
        if de61.old_status == false
          puts 'connecting to the bluetooth device ...'
          Thread.new{`rfcomm connect 0 #{de61.id}`}
          sleep 12
        end
        de61.new_location = check_location(de61.id) 
      end
      
      puts 'old_location: ' + de61.old_location.to_s
      puts 'new_location: ' + de61.new_location.to_s
      update_status(de61.project, de61.new_status, de61.new_location) if ((de61.old_status != de61.new_status.to_s) or (de61.old_location != de61.new_location)) 
        
      mystatus = ''
      
      if  (not de61.old_status == de61.new_status) then
        if (de61.new_status.to_s == 'true') then
            mystatus = ' has entered the building'
            callto('SIP/line1')
        elsif  (de61.new_status == false) then
          mystatus = ' has left the building'
          callto('SIP/07736668666@sipgate')
        end
        puts 'updating in or out the building'
        ms = MyStatus.new(mystatus)
      elsif (de61.old_location.to_s != de61.new_location.to_s)
        if de61.old_location.match(/sleep/) and de61.new_location.match(/home|bed/) and not old_mystatus.match(/awoken/) then
          mystatus = ' has awoken'
        elsif (de61.old_location.match(/home|bed/)) and de61.new_location.match(/sleep/) 
          mystatus = ' has gone to sleep'           
        elsif (de61.old_location.match(/home/)) and de61.new_location.match(/bed/) 
          mystatus = ' has gone to bed'
        elsif de61.old_location.match(/bed/) and de61.new_location.match(/home/) 
          mystatus = ' has got out of bed'
        end
        puts 'updating home location'
        ms = MyStatus.new(mystatus) if mystatus != ''
      end


      de61.old_location = de61.new_location
      de61.new_location = 'at home'
      de61.old_status = de61.new_status
      old_mystatus = mystatus
      sleep 7
    end
  end
    
  def callto(dialstring)
    file = File.new('/etc/asterisk/ruby/call-to.txt','w')
    file.puts 'Dial(' + dialstring + ')'
    file.close
  end


file: extensions.conf
exten => 5168666,n,ReadFile(dialplan1=/etc/asterisk/ruby/call-to.txt,130)
exten => 5168666,n,Exec(${dialplan1})

Like a Touch screen in the air... Like a mouse with a pencil.

I had create a motion detection that find a black pixel in the square, and centralize on it.
Its very simple but very fun!

References
http://www.danilocesar.com/blog/2006/12/03/smartphones-aonde-podemos-parar

If you want to see this working:
http://www.youtube.com/watch?v=sjm_g9MSYPc



#################################################################
# Developed by Danilo Cesar [http://www.danilocesar.com]
# Inspired on:  http://www.bigbold.com/snippets/posts/show/636
#################################################################

from appuifw import *
from graphics import Image
import camera, e32

app.body = c = Canvas()

running = 1
def quit():
    global running
    running = 0

app.exit_key_handler=quit
app.title = u"O controle"
app.screen = 'full'   # or 'normal', 'large'

def getdata(im, bpp=24):
    import struct, zlib
    im.save('D:\\pixels.png', bpp=bpp, compression='no')
    f = open('D:\\pixels.png', 'rb')
    f.seek(8 +8+13+4)
    chunk = []
    while 1:
        n = struct.unpack('>L', f.read(4))[0]
        if n==0: break  # 'IEND' chunk
        f.read(4) # 'IDAT'
        chunk.append(f.read(n))
        f.read(4)   # CRC
    f.close()
    return zlib.decompress(''.join(chunk))  # '\x00' prefix each line


X = 80
Y = 60
while running:
    if X < 0: X = 0
    if Y< 0: Y = 0
    if X > 160 - 30: X = 160 - 30
    if Y > 120 - 30: Y = 120-30
    im = camera.take_photo('RGB', (160,120))
    im.rectangle([(X,Y),(X+30,Y+30)], 0xff0000)   # red outline
    # check hot spot whether active
    box = Image.new((30,30), 'L')  # gray scale
    box.blit(im, (X,Y,X+30,Y+30))
    data = getdata(box, 8)

    # check black
    for i in range(len(data)):
        if ord(data[i]) < 30 and ord(data[i]) > 0:
            X += i%31 - 15
            Y += int(i/31) - 15
            break

    c.blit(im, (0,0), (8,12))   # show camera
 


« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS