def ajax(request): data = request.GET.dict() return_values = dict() if 'init' in data: try: pfio.init() except pfio.InitError as error: return_values.update({'status': 'init failed'}) return_values.update({'error': str(error)}) return HttpResponseBadRequest(simplejson.dumps(return_values)) if 'read_input' in data: try: input_bitp = pfio.read_input() except Exception as e: return_values.update({'status': 'read_input failed'}) return_values.update({'error': str(e)}) return HttpResponseBadRequest(simplejson.dumps(return_values)) else: return_values.update({'input_bitp': input_bitp}) if 'read_output' in data: try: output_bitp = pfio.read_output() except Exception as e: return_values.update({'status': 'read_output failed'}) return_values.update({'error': str(e)}) return HttpResponseBadRequest(simplejson.dumps(return_values)) else: return_values.update({'output_bitp': output_bitp}) if 'write_output' in data: try: output_bitp = int(data['write_output']) except ValueError: return_values.update({'status': 'write_output failed'}) return_values.update( {'error': "write_output needs an integer bit pattern."}) return HttpResponseBadRequest(simplejson.dumps(return_values)) try: pfio.write_output(output_bitp) except Exception as e: return_values.update({'status': "write_output failed"}) return_values.update({'error': str(e)}) return HttpResponseBadRequest(simplejson.dumps(return_values)) return_values.update({'status': 'success'}) return HttpResponse(simplejson.dumps(return_values))
def ajax(request): data = request.GET.dict() return_values = dict() if 'init' in data: try: pfio.init() except pfio.InitError as error: return_values.update({'status' : 'init failed'}) return_values.update({'error' : str(error)}) return HttpResponseBadRequest(simplejson.dumps(return_values)) if 'read_input' in data: try: input_bitp = pfio.read_input() except Exception as e: return_values.update({'status' : 'read_input failed'}) return_values.update({'error' : str(e)}) return HttpResponseBadRequest(simplejson.dumps(return_values)) else: return_values.update({'input_bitp' : input_bitp}) if 'read_output' in data: try: output_bitp = pfio.read_output() except Exception as e: return_values.update({'status' : 'read_output failed'}) return_values.update({'error' : str(e)}) return HttpResponseBadRequest(simplejson.dumps(return_values)) else: return_values.update({'output_bitp' : output_bitp}) if 'write_output' in data: try: output_bitp = int(data['write_output']) except ValueError: return_values.update({'status' : 'write_output failed'}) return_values.update({'error' : "write_output needs an integer bit pattern."}) return HttpResponseBadRequest(simplejson.dumps(return_values)) try: pfio.write_output(output_bitp) except Exception as e: return_values.update({'status' : "write_output failed"}) return_values.update({'error' : str(e)}) return HttpResponseBadRequest(simplejson.dumps(return_values)) return_values.update({'status' : 'success'}) return HttpResponse(simplejson.dumps(return_values))
for i in array: # for each colour in current sequence (flash the sequence) pfio.digital_write(i+2,1) # turn the colour on if screen_output: # print the colour to the screen print colours[i-1] sleep(0.5) # wait to keep the colour showing pfio.digital_write(i+2,0) # turn the colour off sleep(0.2) # small break between colours sleep(0.4) pfio.write_output(0xFF) # signify it is their turn by turning all the LEDs on then off sleep(0.3) pfio.write_output(0x0) if screen_output: print "\nYour turn!" for i in array: # for each colour in current sequence (check against inputted sequence) event = pfio.read_input() # read the button port state while event != 0: # wait till no buttons pressed event = pfio.read_input() # so a single button press is not read as 2 sleep(0.001) # delay while event == 0: # wait for any input
if screen_output: # print the round number print "\nRound %s!" % game_round for i in array: # for each colour in current sequence (flash the sequence) pfio.digital_write(i + 2, 1) # turn the colour on if screen_output: # print the colour to the screen print colours[i - 1] sleep(0.5) # wait to keep the colour showing pfio.digital_write(i + 2, 0) # turn the colour off sleep(0.2) # small break between colours sleep(0.4) pfio.write_output( 0xFF) # signify it is their turn by turning all the LEDs on then off sleep(0.3) pfio.write_output(0x0) if screen_output: print "\nYour turn!" for i in array: # for each colour in current sequence (check against inputted sequence) event = pfio.read_input() # read the button port state while event != 0: # wait till no buttons pressed event = pfio.read_input( ) # so a single button press is not read as 2 sleep(0.001) # delay while event == 0: # wait for any input
if misses == 10: # too many misses = Game Over! break previous = current # current = next_colour() # get next colour while current == previous: # ensure differnt colour each time current = next_colour() # get next colour if ((score + misses) %30)==29: if set_time > 125: set_time /= 2 # reduce the allowed time print "Time left is: %s" %set_time time_left = set_time # set the countdown time pfio.digital_write(current+2,1) # turn the new light on time_left -=1 # decrement the time left to hit the current light pfio.write_output(0) # turn all lights off print "\nGame over!\n" print "Your score was: %s" %score # print the player's final score #pfio.deinit() # close the pfio Contact GitHub API Training Shop Blog About © 2016 GitHub, Inc. Terms Privacy Security Status Help
#!/usr/bin/python import piface.pfio as pfio import time pfio.init() for i in range(1,5): pfio.write_output(0b00000001) time.sleep(2) pfio.write_output(0b00000000) time.sleep(.4) pfio.write_output(0b00000010) time.sleep(2) pfio.write_output(0b00000000) time.sleep(.4)
from time import sleep #import piface.emulator as pfio import piface.pfio as pfio if __name__ == "__main__": pfio.init() while True: pfio.write_output(0xff) sleep(1) pfio.write_output(0) sleep(1)
elif time_left == 0: pfio.digital_write(current + 2, 0) # turn off hit light misses += 1 # increment misses print "Missed one!" if misses == 10: # too many misses = Game Over! break previous = current # current = next_colour() # get next colour while current == previous: # ensure differnt colour each time current = next_colour() # get next colour if ((score + misses) % 30) == 29: if set_time > 125: set_time /= 2 # reduce the allowed time print "Time left is: %s" % set_time time_left = set_time # set the countdown time pfio.digital_write(current + 2, 1) # turn the new light on time_left -= 1 # decrement the time left to hit the current light pfio.write_output(0) # turn all lights off print "\nGame over!\n" print "Your score was: %s" % score # print the player's final score # pfio.deinit() # close the pfio
def arcade_buttons(): while(True): pfio.write_output(pfio.read_input())