Advertisement
Guest User

Sean Brewer

a guest
Jan 24th, 2010
768
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.25 KB | None | 0 0
  1. #synctime - synchronize your chronos watch to your computer
  2. #
  3. #
  4. # Copyright (c) 2010 Sean Brewer
  5. #
  6. # Permission is hereby granted, free of charge, to any person
  7. # obtaining a copy of this software and associated documentation
  8. # files (the "Software"), to deal in the Software without
  9. # restriction, including without limitation the rights to use,
  10. #
  11. # copies of the Software, and to permit persons to whom the
  12. # Software is furnished to do so, subject to the following
  13. # conditions:
  14. #
  15. # The above copyright notice and this permission notice shall be
  16. # included in all copies or substantial portions of the Software.
  17. #
  18. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  20. # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  22. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  23. # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  24. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  25. # OTHER DEALINGS IN THE SOFTWARE.
  26. #
  27.  
  28. #You can contact me at seabre986@gmail.com or on reddit (seabre) if you want.
  29.  
  30. import serial
  31. import datetime
  32. import array
  33. def splitIntoNPieces(s,n):
  34.     return [s[i:i+n] for i in range(0, len(s), n)]    
  35.  
  36. def startAccessPoint():
  37.     return array.array('B', [0xFF, 0x07, 0x03]).tostring()
  38.  
  39. def stopAccessPoint():
  40.     return array.array('B', [0xFF, 0x09, 0x03]).tostring()
  41.  
  42.  
  43. """
  44. kinda messy
  45.  
  46. Here's how this works.
  47. A packet for the watch sync looks like this:
  48. FF 31 16 03 94 34 01 07 DA 01 17 06 1E 00 00 00 00 00 00 00 00 00
  49. You have the first four bytes which we're just going to ignore.
  50. The 5th byte represents the hour you want to sync to.
  51. The 6th byte represents the minute you want to sync to.
  52. The 7th byte represents the second you want to sync to.
  53. The 9th byte represents the year you want to sync to.
  54. The 10th byte represents the month you want to sync to.
  55. The 11th byte represents the day you want to sync to.
  56. THe 14th and 15th bytes represent the temperature in celcius.
  57. The 16th and 17th bytes represent the altitude in meters.
  58.  
  59. It also stores some of these values in some unusual (to me) ways.
  60.  
  61. For hour, the value 0x80 needs to be added to the 24hr representation of the desired
  62. hour to sync to.
  63.  
  64. For year the value 0x700 needs to be subtracted from the desired year to sync to.
  65.  
  66. For temperature, the temperature (in celcius) needs to be multiplied by 0x0A.
  67. """
  68. def syncTimeDateTempAlt(hour,minute,second,month,day,year,tempCelcius,altMeters):
  69.     adjHour = hour + 0x80 #assumes 24h based time entry
  70.     adjYear = year - 0x700
  71.     adjTempCelcius = tempCelcius * 0x0A
  72.  
  73.     cmd = [0xFF, 0x31, 0x16, 0x03,adjHour,minute,second,0x07,adjYear,month,day,0x06,0x1E]
  74.  
  75.     hexCelcius = hex(adjTempCelcius)[2:]
  76.     hexMeters = hex(altMeters)[2:]
  77.    
  78.     if len(hexCelcius) % 2 == 0:
  79.         for i in splitIntoNPieces(hexCelcius,2):
  80.             cmd.append(int(i,16))
  81.     else:
  82.         for i in splitIntoNPieces(hexCelcius.zfill(len(hexCelcius)+1),2):
  83.             cmd.append(int(i,16))
  84.  
  85.    
  86.     if len(hexMeters) % 2 == 0:
  87.         for i in splitIntoNPieces(altMeters,2):
  88.             cmd.append(int(i,16))
  89.     else:
  90.         for i in splitIntoNPieces(hexMeters.zfill(len(hexMeters)+1),2):
  91.             cmd.append(int(i,16))
  92.     for i in xrange(0,5):
  93.         cmd.append(0)        
  94.    
  95.     return array.array('B', cmd).tostring()
  96.  
  97.  
  98. #Open COM port 6 (check your system info to see which port
  99. #yours is actually on.)
  100. #argments are 5 (COM6), 115200 (bit rate), and timeout is set so
  101. #the serial read function won't loop forever.
  102. ser = serial.Serial(5,115200,timeout=1)
  103.  
  104. #Start access point
  105. ser.write(startAccessPoint())
  106.  
  107. raw_input("Please turn your watch to sync mode and turn on the transceiver (though if the transciever is already on you may have to turn it off then on again), then press enter...")
  108.  
  109. time = datetime.datetime.now()
  110.  
  111. ser.write(syncTimeDateTempAlt(time.hour,time.minute,time.second,time.month,time.day,time.year,0,0))
  112.  
  113. #The start access point command needs to come before the stop access point command
  114. #in order for the access point to turn off.
  115. ser.write(startAccessPoint())
  116. ser.write(stopAccessPoint())
  117.    
  118. ser.close()
  119.    
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement