Skip to content

Commit

Permalink
Merge pull request schodet#144 from dlech/nxt-temp
Browse files Browse the repository at this point in the history
NXT Temperature Sensor
  • Loading branch information
GoldSloth authored Apr 28, 2018
2 parents 9851bee + ae449f8 commit be0e257
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
2 changes: 1 addition & 1 deletion nxt/direct.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def ls_write(opcode, port, tx_data, rx_bytes):
tgram.add_u8(port)
tgram.add_u8(len(tx_data))
tgram.add_u8(rx_bytes)
tgram.add_string(len(tx_data), tx_data)
tgram.add_bytes(tx_data)
return tgram

def ls_read(opcode, port):
Expand Down
2 changes: 1 addition & 1 deletion nxt/sensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from .common import *
from .analog import BaseAnalogSensor
from .digital import BaseDigitalSensor, find_class
from .generic import Touch, Light, Sound, Ultrasonic, Color20
from .generic import Touch, Light, Sound, Ultrasonic, Color20, Temperature
from . import mindsensors
MSSumoEyes = mindsensors.SumoEyes
MSCompassv2 = mindsensors.Compassv2
Expand Down
4 changes: 2 additions & 2 deletions nxt/sensor/digital.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def _i2c_command(self, address, value, format):
a tuple of values corresponding to the given format.
"""
value = struct.pack(format, *value)
msg = chr(self.I2C_DEV) + chr(address) + value
msg = bytes((self.I2C_DEV, address)) + value
now = time()
if self.last_poll+self.poll_delay > now:
diff = now - self.last_poll
Expand All @@ -109,7 +109,7 @@ def _i2c_query(self, address, format):
module. See http://docs.python.org/library/struct.html#format-strings
"""
n_bytes = struct.calcsize(format)
msg = chr(self.I2C_DEV) + chr(address)
msg = bytes((self.I2C_DEV, address))
now = time()
if self.last_poll+self.poll_delay > now:
diff = now - self.last_poll
Expand Down
31 changes: 31 additions & 0 deletions nxt/sensor/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,34 @@ def get_color(self):
return self.get_input_values().scaled_value

get_sample = get_color


class Temperature(BaseDigitalSensor):
"""Object for LEGO MINDSTORMS NXT Temperature sensors"""
# This is actually a TI TMP275 chip: http://www.ti.com/product/tmp275
I2C_DEV = 0x98
I2C_ADDRESS = {'raw_value': (0x00, '>h')}

def __init__(self, brick, port):
# This sensor does not follow the convention of having version/vendor/
# product at I2C registers 0x00/0x08/0x10, so check_compatible is
# always False
super(Temperature, self).__init__(brick, port, False)

def _get_raw_value(self):
"""Returns raw unscaled value"""
# this is a 12-bit value
return self.read_value('raw_value')[0] >> 4

def get_deg_c(self):
"""Returns the temperature in degrees C"""
v = self._get_raw_value()
# technically, 16 should be 0x7ff/128 but 16 is close enough
return round(v / 16, 1)

def get_deg_f(self):
v = self._get_raw_value()
# technically, 16 should be 0x7ff/128 but 16 is close enough
return round(9 / 5 * v / 16 + 32, 1)

get_sample = get_deg_c
3 changes: 3 additions & 0 deletions nxt/telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ def bytes(self):
def is_reply(self):
return self.typ == Telegram.TYPE_REPLY

def add_bytes(self, b):
self.pkt.write(pack('%ds' % len(b), b))

def add_string(self, n_bytes, v):
self.pkt.write(pack('%ds' % n_bytes, v.encode('windows-1252')))

Expand Down

0 comments on commit be0e257

Please sign in to comment.