Skip to content

Commit

Permalink
Add class for LEGO NXT Temperature sensor
Browse files Browse the repository at this point in the history
This adds a new class for the official LEGO NXT Temperature sensor.

Fixes schodet#109
Fixes schodet#111
  • Loading branch information
dlech committed Apr 28, 2018
1 parent da50b41 commit ae449f8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
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
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

0 comments on commit ae449f8

Please sign in to comment.