<commit_before>import usb.core, time, binascii
from datetime import datetime

dev = usb.core.find(idVendor=0x3561, idProduct=0x1942)
dev.set_configuration()

#possible resistance values to be sent
#-5% (0xf34d or -0x0cb3 or -3251)
#-2.5% (0xf9a7 or -0x659 or -1625), 
#0 (0), 
#2.5% (0x0659 or 1625), 
#5% (0x0cb3 or 3251), 
#7.5% (0x130c or 4876),
#10% (0x1966 or 6502), 
#12.5% (0x1fbf or 8127), 
#15% (0x2618 or 9752), 
#17.5% (0x2c72 or 11378) and 
#20% (0x32cb or 13003).

#speed values to receive
#0x0800 : 5km/h,  int 8
#0x0c00 : 10km/h, int 12
#0x1300 : 15km/h, int 19
#0x1600 : 20km/h, int 22
#0x1c00 : 25 km/h int 30
#0x2200 : 30 km/h int 34

#initialise TACX USB device
byte_ints = [2,0,0,0] # will not read cadence until initialisation byte is sent
byte_str = "".join(chr(n) for n in byte_ints)
if not simulatetrainer:
 dev.write(0x02,byte_str)
time.sleep(1)
data=""
try:
  while True:
    last_measured_time = time.time() * 1000
    data = dev.read(0x82,64) #get data from device
    print datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3],"TRAINER RX DATA",binascii.hexlify(data)
    r6=int(4876)>>8 & 0xff #byte6
    r5=int(4876) & 0xff #byte 5
    #echo pedal cadence back to trainer
    if len(data) > 40:
      pedecho = data[42]
    else:
      pedecho = 0
    byte_ints = [0x01, 0x08, 0x01, 0x00, r5, r6, pedecho, 0x00 ,0x02, 0x52, 0x10, 0x04]
    print datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3],"TRAINER TX DATA",byte_ints
    byte_str = "".join(chr(n) for n in byte_ints)
    dev.write(0x02,byte_str)#send data to device
    
    #add wait so we only send every 250ms
    time_to_process_loop = time.time() * 1000 - last_measured_time
    sleep_time = 0.25 - (time_to_process_loop)/1000
    if sleep_time < 0: sleep_time = 0
    time.sleep(sleep_time)
except KeyboardInterrupt: # interrupt power data sending with ctrl c, make sure script continues to reset device
  pass<commit_msg>fixed script<commit_after>import usb.core, time, binascii
from datetime import datetime

dev = usb.core.find(idVendor=0x3561, idProduct=0x1942)
dev.set_configuration()

#possible resistance values to be sent
#-5% (0xf34d or -0x0cb3 or -3251)
#-2.5% (0xf9a7 or -0x659 or -1625), 
#0 (0), 
#2.5% (0x0659 or 1625), 
#5% (0x0cb3 or 3251), 
#7.5% (0x130c or 4876),
#10% (0x1966 or 6502), 
#12.5% (0x1fbf or 8127), 
#15% (0x2618 or 9752), 
#17.5% (0x2c72 or 11378) and 
#20% (0x32cb or 13003).

#speed values to receive
#0x0800 : 5km/h,  int 8
#0x0c00 : 10km/h, int 12
#0x1300 : 15km/h, int 19
#0x1600 : 20km/h, int 22
#0x1c00 : 25 km/h int 30
#0x2200 : 30 km/h int 34

#initialise TACX USB device
byte_ints = [2,0,0,0] # will not read cadence until initialisation byte is sent
byte_str = "".join(chr(n) for n in byte_ints)
dev.write(0x02,byte_str)
time.sleep(1)
data=""
try:
  while True:
    last_measured_time = time.time() * 1000
    data = dev.read(0x82,64) #get data from device
    print datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3],"TRAINER RX DATA",binascii.hexlify(data)
    r6=int(4876)>>8 & 0xff #byte6
    r5=int(4876) & 0xff #byte 5
    #echo pedal cadence back to trainer
    if len(data) > 40:
      pedecho = data[42]
    else:
      pedecho = 0
    byte_ints = [0x01, 0x08, 0x01, 0x00, r5, r6, pedecho, 0x00 ,0x02, 0x52, 0x10, 0x04]
    print datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3],"TRAINER TX DATA",byte_ints
    byte_str = "".join(chr(n) for n in byte_ints)
    dev.write(0x02,byte_str)#send data to device
    
    #add wait so we only send every 250ms
    time_to_process_loop = time.time() * 1000 - last_measured_time
    sleep_time = 0.25 - (time_to_process_loop)/1000
    if sleep_time < 0: sleep_time = 0
    time.sleep(sleep_time)
except KeyboardInterrupt: # interrupt power data sending with ctrl c, make sure script continues to reset device
  pass
{'fraction_non_alphanumeric': 0.1430439330543933, 'fraction_numerical': 0.13336820083682008, 'mean_word_length': 3.7456575682382134, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2183302', 'n_tokens_mistral': 1939, 'n_tokens_neox': 1627, 'n_words': 449}