Skip to content

Instantly share code, notes, and snippets.

View thekitchenscientist's full-sized avatar

thekitchenscientist

View GitHub Profile
@safe_add
def add_input_device_ready_raw3(self, input_port,
mode=-1,
device_type=0,
layer=USB_CHAIN_LAYER_MASTER):
"""Waits until the device on the specified InputPort is ready and then
returns its value as a raw value.
"""
self._msg.append(Opcode.INPUT_DEVICE)
# -*- coding: utf-8 -*-
"""
Created on Sat Jan 31 19:02:16 2015
@author: thekitchenscientist@gmail.com
"""
import ev3
import direct_command
import time
@thekitchenscientist
thekitchenscientist / MainWindow.xaml.cs
Last active August 29, 2015 14:27
The basic C# code required to send commands to a LEGO EV3 on windows and receive sensor data. A copy of the dll from https://legoev3.codeplex.com/ is required.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
@thekitchenscientist
thekitchenscientist / EV3Examples.py
Created August 20, 2015 18:19
Examples of commands that can be sent to the brick using DirectCommand. Requires library from https://github.com/inductivekickback/ev3
import ev3
import direct_command
"""
The first example creates an object called fwd to which we bind the class called direct command . We can then call the functions of the direct command using the magic dot. When you type fwd. on each line a list of the available functions appear (in Spyder) which you can scroll through and choose the correct one. Then when you type ( the object inspector tells you about the commands this function accepts.
"""
# move forward
fwd = direct_command.DirectCommand()
fwd.add_output_speed(direct_command.OutputPort.PORT_C,100)
@thekitchenscientist
thekitchenscientist / EV3CycleLEDs.py
Created August 20, 2015 19:14
Byte Codes to control EV3 LEDs by dirrect command
import time
LED_off = '\x08\x00\x00\x00\x80\x00\x00\x82\x1B\x00'
LED_green = '\x08\x00\x00\x00\x80\x00\x00\x82\x1B\x01'
LED_orange = '\x08\x00\x00\x00\x80\x00\x00\x82\x1B\x03'
LED_red = '\x08\x00\x00\x00\x80\x00\x00\x82\x1B\x02'
# send commands to EV3 via bluetooth
with open('/dev/rfcomm0', 'w', 0) as bt:
bt.write(LED_red)
@thekitchenscientist
thekitchenscientist / EV3playtone.py
Created August 20, 2015 19:29
Send a note to an EV3 brick from Linux
play_tone = '\x0F\x00\x00\x00\x80\x00\x00\x94\x01\x81\x02\x82\xE8\x03\x82\xE8\x03'
# send commands to EV3 via bluetooth
with open('/dev/rfcomm0', 'w', 0) as bt:
bt.write(play_tone)
@thekitchenscientist
thekitchenscientist / EV3ControlMotor.py
Created August 20, 2015 19:31
Basic motor control by direct command on Linux
import time
# command to start motor on port A at speed 20
start_motor = '\x0C\x00\x00\x00\x80\x00\x00\xA4\x00\x01\x14\xA6\x00\x01'
# command to stop motor on port A
stop_motor = '\x09\x00\x01\x00\x80\x00\x00\xA3\x00\x01\x00'
# send commands to EV3 via bluetooth
with open('/dev/rfcomm0', 'w', 0) as bt:
@thekitchenscientist
thekitchenscientist / EV3pyDirectCommand.py
Created August 20, 2015 19:36
A simple test of direct commands using ev3py (https://github.com/thiagomarzagao/ev3py).
#import modules
import time
from ev3py import ev3
#create a reference to the ev3 called 'mybrick'
mybrick = ev3()
# connect with EV3 via Bluetooth
mybrick.connect('bt')
import serial
from dec_to_hex import h # decimal-to-hexadecimal dictionary
EV3 = serial.Serial('/dev/rfcomm0')
# message length
bytecount = h[44] #look up 44 in hex dictionary
comm_0 = '\x00'
@thekitchenscientist
thekitchenscientist / EV3pyHexConversion.py
Created August 20, 2015 19:45
EV3py using python hex conversion rather than library
import serial
EV3 = serial.Serial('/dev/rfcomm0')
textstring = "2C000000800000841300820000820000841C018200008232008475692F6D696E6473746F726D732E726766008400"
command = textstring.decode("hex")
print(command)
EV3.write(command)