menu("city", ["Boston", "Bournemouth", "Barcelona", "Belgium"]) @reporter("lookup weather in %m.city") def weather(city="Boston"): import time return "{weather} in {city}".format(city=city, weather=["sunny", "cloudy", "rainy", "snowy"][int(time.time() % 4)]) menu("motor", [1, 2, 3, "A", "B", "C"]) @command("move motor %d.motor by %n degrees", blocking=True) def move_motor(motor="A", angle=4): print("move {motor} by {angle} degrees!".format(motor=motor, angle=angle)) time.sleep(1) print("...") @reporter("light sensor value") def light(): import random return random.randint(0, 100) @command("drive %n steps") def move(steps): print("." * steps) if __name__ == "__main__": blockext.run("Disappointing UFO", "ufo", 5678)
bot.beep(time,tone) @command("set S2 name to %s") def nameset(name): bot.setName(name) menu("ledpos",["right","left"]) menu("onoff",["on","off"]) @command("turn %m.ledpos LED %m.onoff") def setled(ledpos="right", onoff="on"): bot.setLED(ledpos,onoff) if onoff == "off": bot.setLED(ledpos,"off") @reporter("get line sensor %n value") def getline(n=1): return bot.getLine(n - 1) @reporter("get IR sensor %n value") def getline(n=1): return bot.getIR(n - 1) @reporter("get light sensor %n value") def getlight(n=1): return bot.getLight(n - 1) @reporter("get S2 name") def getname(): return bot.getName() @reset def resetall(): bot.stop() bot = Scribbler() blockext.run("Parallax S2 robot", "s2", 3610)
A = "" @command("set list %s") def set_list(m): global A A = m.split(" ") print(A) global Res0 global Res1 Res0 = "" Res1 = "" Res0, Res1 = combinatorialOptimization4.comb1_main(A) @reporter("get list1") def get_list1(): return ((Res0.replace("'", "")).replace("[", "")).replace("]", "") @reporter("get list2") def get_list2(): return ((Res1.replace("'", "")).replace("[", "")).replace("]", "") if __name__ == "__main__": run("CombinationOptimization", "CombinationOptimization", 1234)
import blockext from blockext import * import sys, atexit, msvcrt from time import sleep sys.path.append("../lib/") sys.path.append("blocks_scratch_interface") from moway_lib import * from lib_sen_leds import * from lib_sen_obstac import * from lib_sen_line import * from lib_sen_light import * from lib_mot_moway import * from lib_accel_moway import * from lib_speaker_moway import * from lib_sen_light import * from lib_sen_volume import * if __name__ == "__main__": blockext.run("Moway", "ufo", 1234)
def run(self): blockext.run('minesweeper', 'minesweeper', 5678)
@blockext.command('change lower') def change_lower(): sk.upper = 0 sk.valid_result(sk.result) @blockext.command('hold on shift') def hold_on_shift(): sk.hold = 1 sk.valid_result(sk.result) @blockext.command('hold out shift') def hold_out_shift(): sk.hold = 0 sk.valid_result(sk.result) @blockext.reporter('shift') def shift(): return sk.upper @blockext.reporter('hold') def hold(): return sk.hold if __name__ == '__main__': blockext.run('SwiftKey', 'swiftkey', 5678)
@command("set x1 %n") def set_x1(n): model.test[0] = n @command("set x2 %n") def set_x2(n): model.test[1] = n @predicate("is busy") def is_busy(): return model.is_busy @reporter("get score") def get_score(): return "%d" % (model.score[1] * 100) @reporter("predict") def predict(): return model.predict() if __name__ == "__main__": model = Model() run("XOR", "xor", 5678)
def finger_pos(xyz="x", fingerNumber=1): frame = controller.frame() if xyz == "x": return frame.hands[int(fingerNumber) - 1].palm_position.x elif xyz == "y": return frame.hands[int(fingerNumber) - 1].palm_position.y else: return frame.hands[int(fingerNumber) - 1].palm_position.z menu("ypr", ["yaw", "pitch", "roll"]) menu("handNumber", ["1", "2"]) @reporter("%m.ypr of hand %d.handNumber") def hand_ypr(ypr="yaw", handNumber="1"): frame = controller.frame() if ypr == "yaw": return (frame.hands[int(handNumber) - 1].direction.yaw * Leap.RAD_TO_DEG) elif ypr == "pitch": return (frame.hands[int(handNumber) - 1].direction.pitch * Leap.RAD_TO_DEG) else: return (frame.hands[int(handNumber) - 1].palm_normal.roll * Leap.RAD_TO_DEG) controller = Leap.Controller() blockext.run("Leap Motion", "leap", 3434)
if onoff == 'off': bot.setLED(ledpos, 'off') @reporter('get line sensor %n value') def getline(n=1): return bot.getLine(n - 1) @reporter('get IR sensor %n value') def getline(n=1): return bot.getIR(n - 1) @reporter('get light sensor %n value') def getlight(n=1): return bot.getLight(n - 1) @reporter('S2 robot name') def getname(): return bot.getName() @reset def resetall(): bot.stop() blockext.run("Parallax S2 robot", 's2', 3610)
from blockext import run, reporter, command message = "" @command("set message %s") def set_message(m): global message message = m @reporter("get message") def get_message(): return message if __name__ == "__main__": run("Extension Test", "extension_test", 5678)
def weather(city="Boston"): import time return "{weather} in {city}".format(city=city, weather=["sunny", "cloudy", "rainy", "snowy"][int(time.time() % 4)]) menu("motor", [1, 2, 3, "A", "B", "C"]) @command("move motor %d.motor by %n degrees", blocking=True) def move_motor(motor="A", angle=4): print("move {motor} by {angle} degrees!".format(motor=motor, angle=angle)) time.sleep(1) print("...") @reporter("light sensor value") def light(): import random return random.randint(0, 100) @command("drive %n steps") def move(steps): print("." * steps) if __name__ == "__main__": blockext.run("Disappointing UFO", "ufo", 5678)
def add_loc(self, loc): name, x, y = loc.split(',') self.log[name] = [round(float(x)), round(float(y))] def save(self): with open(keylocfile, 'w') as f: json.dump(self.log, f) logger = LocLogger() @blockext.command("init log") def init_log(): logger.init_log() @blockext.command("add loc %s") def add_loc(loc): logger.add_loc(loc) @blockext.command("save") def save(): logger.save() if __name__ == '__main__': blockext.run("Loc Logger", "setup_keyboard", port=5678)
logger.upper = True @blockext.command('change lower') def change_lower(): logger.upper = False @blockext.command('hold on shift') def hold_on_shift(): logger.hold = True @blockext.command('hold out shift') def hold_out_shift(): logger.hold = False @blockext.reporter('shift') def shift(): return 1 if logger.upper else 0 @blockext.reporter('hold') def hold(): return 1 if logger.hold else 0 if __name__ == '__main__': blockext.run('LocusRecognizer', 'locus_recognizer', 5678)