text
stringlengths
1
93.6k
songNumber must be between 0 and 15 (inclusive)
songDataList is a list of (note, duration) pairs (up to 16)
note is the midi note number, from 31 to 127
(outside this range, the note is a rest)
duration is from 0 to 255 in 1/64ths of a second
"""
# any notes to play?
if type(songDataList) != type([]) and type(songDataList) != type(()):
print 'songDataList was', songDataList
return
if len(songDataList) < 1:
print 'No data in the songDataList'
return
if songNumber < 0: songNumber = 0
if songNumber > 15: songNumber = 15
# indicate that a song is coming
self._write( SONG )
self._write( chr(songNumber) )
L = min(len(songDataList), 16)
self._write( chr(L) )
# loop through the notes, up to 16
for note in songDataList[:L]:
# make sure its a tuple, or else we rest for 1/4 second
if type(note) == type( () ):
#more error checking here!
self._write( chr(note[0]) ) # note number
self._write( chr(note[1]) ) # duration
else:
self._write( chr(30) ) # a rest note
self._write( chr(16) ) # 1/4 of a second
return
def playSong(self, list_of_notes):
""" The input to <tt>playSong</tt> should be specified as a list
of pairs of [ note_number, note_duration ] format. Thus,
r.playSong( [(60,8),(64,8),(67,8),(72,8)] ) plays a quick C chord.
"""
# implemented by setting song #1 to the notes and then playing it
self.setSong(1, list_of_notes)
self.playSongNumber(1)
def playSongNumber(self, songNumber):
""" plays song songNumber """
if songNumber < 0: songNumber = 0
if songNumber > 15: songNumber = 15
self._write( PLAY )
self._write( chr(songNumber) )
def playNote(self, noteNumber, duration, songNumber=0):
""" plays a single note as a song (at songNumber)
duration is in 64ths of a second (1-255)
the note number chart is on page 12 of the open interface manual
"""
# set the song
self.setSong(songNumber, [(noteNumber,duration)])
self.playSongNumber(songNumber)
def _getLower5Bits( self, r ):
""" r is one byte as an integer """
return [ _bitOfByte(4,r), _bitOfByte(3,r), _bitOfByte(2,r), _bitOfByte(1,r), _bitOfByte(0,r) ]
def _getOneBit( self, r ):
""" r is one byte as an integer """
if r == 1: return 1
else: return 0
def _getOneByteUnsigned( self, r ):
""" r is one byte as an integer """
return r
def _getOneByteSigned( self, r ):
""" r is one byte as a signed integer """
return _twosComplementInt1byte( r )
def _getTwoBytesSigned( self, r1, r2 ):
""" r1, r2 are two bytes as a signed integer """
return _twosComplementInt2bytes( r1, r2 )
def _getTwoBytesUnsigned( self, r1, r2 ):
""" r1, r2 are two bytes as an unsigned integer """
return r1 << 8 | r2
def _getButtonBits( self, r ):
""" r is one byte as an integer """
return [ _bitOfByte(2,r), _bitOfByte(0,r) ]
def _setNextDataFrame(self):
""" This function _asks_ the robot to collect ALL of