Esempio n. 1
0
class TheTapper():
    """
    Illuminate the neopixels in a counter-clockwise fashion with randomly generated colors.
    When you tap the playground express, the neopixels will stop changing and the
    program pauses. Tap again and the neopixels will start again.
    """
    def __init__(self):
        # create an instance of the API
        self.p = PyMataCpx()

        print('Tap the playground express to stop the neopixels from moving.')
        print('Tap again, to start them up')
        print('The tap state will be printed to the console')

        # Start monitoring for tap events and
        # send event notifications to the tapped method.
        self.p.cpx_tap_start(self.tapped)
        self.go = True

        while True:

            try:
                # run the light show
                for neopixel in range(0, 10):
                    if self.go:
                        self.p.cpx_pixels_clear()
                        self.p.cpx_pixels_show()
                        r = random.randint(0, 254)
                        g = random.randint(0, 254)
                        b = random.randint(0, 254)
                        self.p.cpx_pixel_set(neopixel, r, g, b)
                        self.p.cpx_pixels_show()
                        time.sleep(.2)
                    else:
                        self.p.cpx_pixels_clear()
                        self.p.cpx_pixels_show()
                        time.sleep(.001)
            except KeyboardInterrupt:
                # If you press control-C, cleanly exit
                self.p.cpx_pixels_clear()
                self.p.cpx_pixels_show()
                self.p.cpx_close_and_exit()

    def tapped(self, data):
        """
        :param data: data[0] = data type (analog = 2, digital =32)
                     data[1] = pin for device 27
                     data[2] = tap data - list of booleans.
                               First value for 1 tap
                               Second value for 2 taps
        """
        # for any taps, toggle the go flag
        # print out the current tap state
        if data[2] != [False, False]:
            self.go = not self.go
            print(self.go)
Esempio n. 2
0
class TheClapper:
    """
    Turn LEDs in a counter-clockwise fashion with randomly generated colors.
    When you clap your hand loudly, the pixels will stop changing and the
    program exits. Clap again and they start moving again.
    """
    def __init__(self):
        self.p = PyMataCpx()

        print('Clap your hands loudly to stop the neopixels from moving.')
        print('Clap again, to start them up')

        self.p.cpx_microphone_start(self.listen)
        self.go = True

        while True:
            try:
                # run the light show
                for neopixel in range(0, 10):
                    if self.go:

                        self.p.cpx_pixels_clear()
                        self.p.cpx_pixels_show()
                        r = random.randint(0, 254)
                        g = random.randint(0, 254)
                        b = random.randint(0, 254)
                        self.p.cpx_pixel_set(neopixel, r, g, b)
                        self.p.cpx_pixels_show()
                        time.sleep(.2)
                    else:
                        self.p.cpx_pixels_clear()
                        self.p.cpx_pixels_show()
                        time.sleep(.001)

            except KeyboardInterrupt:
                # If you press control-C, cleanly exit
                self.p.cpx_pixels_clear()
                self.p.cpx_pixels_show()
                self.p.cpx_close_and_exit()

    def listen(self, data):
        """
        Turn off the light show and exit
        :param data: data[0] = 2 indicating this is analog data
                     data[1] = pin attached to mic- pin 4
                     data[3] = readings from microphone
        """
        if data[2] > 600:
            # self.p.cpx_pixels_clear()
            # self.p.cpx_pixels_show()
            self.go = not self.go
Esempio n. 3
0
class LightMeter:
    """
    Using the light sensor, select and
    illuminate pixels based on the current light sensor reading.
    As the light sensor values increase, the pixels will be lit
    in a clockwise fashion.
    """
    def __init__(self):
        # a dictionary of pixels as the key and
        # associated rgb color values
        self.pix_d = {
            0: [153, 76, 0],
            1: [153, 0, 0],
            2: [153, 153, 0],
            3: [204, 204, 0],
            4: [0, 153, 0],
            5: [0, 153, 153],
            6: [0, 0, 233],
            7: [153, 0, 153],
            8: [255, 0, 255],
            9: [255, 255, 255],
        }

        # save the previous pixel number that was illuminated
        self.last_pixel_used = 0

        # instantiate pymatacpx
        self.p = PyMataCpx()

        # clear the pixels before monitoring the light
        # sensor values
        self.p.cpx_pixels_clear()
        # set pixel 0 to be the initial pixel and set its rgb
        # from the dictionary
        self.p.cpx_pixel_set(0, *self.pix_d[0])

        self.p.cpx_pixels_show()

        # enable the light sensor and provide a callback.
        # Note: the callback is a member of this class.
        self.p.cpx_light_sensor_start(self.light_sensor_callback)

        print()
        print(
            'Move a light source near the light sensor, and depending upon the'
        )
        print(
            'intensity of the light, an associated pixel will be illuminated.')
        print()

        while True:
            # just kill time waiting for a light data to arrive
            try:
                time.sleep(.1)
            except KeyboardInterrupt:
                # If you press control-C, cleanly exit
                self.p.cpx_close_and_exit()

    # This is the callback to process light sensor data
    def light_sensor_callback(self, data):
        """
        Light sensor data processor
        :param data: data[2] contains the current light sensor reading
        :return:
        """
        # get the currently reported level
        level = data[2]

        # the level is in the range of 0-1000.
        # adjust the level to map into a valid pixel number
        if level in range(0, 99):
            pixel = 0
        elif level in range(100, 199):
            pixel = 1
        elif level in range(200, 299):
            pixel = 2
        elif level in range(300, 399):
            pixel = 3
        elif level in range(400, 499):
            pixel = 4
        elif level in range(500, 599):
            pixel = 5
        elif level in range(600, 699):
            pixel = 6
        elif level in range(700, 799):
            pixel = 7
        elif level in range(800, 899):
            pixel = 8
        else:
            pixel = 9

        # get the rgb value for the pixel
        rgb = self.pix_d[pixel]

        # if this is not the same pixel as the last one enabled
        # then manipulate the pixels.

        # turn off the current pixel and turn on the new one.
        if pixel != self.last_pixel_used:
            # extinguish the previous pixel
            self.p.cpx_pixel_set(self.last_pixel_used, 0, 0, 0)

            # set the new pixel
            self.p.cpx_pixel_set(pixel, *rgb)

            # control the pixels
            self.p.cpx_pixels_show()
            self.last_pixel_used = pixel
Esempio n. 4
0
class Thermometer:
    """
    Touch the temperature sensor on the playground express.
    As the temperature increases, pixels will illuminate to
    indicate the temperature.
    """

    def __init__(self):
        # a dictionary of pixels as the key and
        # associated rgb color values
        self.pix_d = {
            0: [153, 76, 0],
            1: [153, 0, 0],
            2: [153, 153, 0],
            3: [204, 204, 0],
            4: [0, 153, 0],
            5: [0, 153, 153],
            6: [0, 0, 233],
            7: [153, 0, 153],
            8: [255, 0, 255],
            9: [255, 255, 255],
        }

        # save the previous pixel number that was illuminated
        self.last_pixel_used = 0

        # save the first temperature read as the ambient temperature
        # and use that as the basis of comparison.
        self.ambient = 0

        # instantiate pymatacpx
        self.p = PyMataCpx()

        # clear the pixels before monitoring the light
        # sensor values
        self.p.cpx_pixels_clear()
        # set pixel 0 to be the initial pixel and set its rgb
        # from the dictionary
        self.p.cpx_pixel_set(0, *self.pix_d[0])

        self.p.cpx_pixels_show()

        # enable the temperature sensor and provide a callback.
        self.p.cpx_temperature_start(self.temp_callback)

        print()
        print('Touch the temperature sensor. As the temperature changes')
        print('Different neopixels will light up.')
        print()
        print('The current temperature and pixel selected will be shown on the console.')
        print()

        while True:
            # just kill time waiting for a light data to arrive
            try:
                time.sleep(.1)
            except KeyboardInterrupt:
                # If you press control-C, cleanly exit
                self.p.cpx_close_and_exit()

    # This is the callback to process light sensor data
    def temp_callback(self, data):
        """
        Light sensor data processor
        :param data: data[0] = 2 analog mode
                     data[1] = pin 0
                     data[2] = temp in degrees C
        """
        # the current temperature
        the_current_temperature = data[2]

        # save the temperature the first time
        # through as ambient
        if not self.ambient:
            self.ambient = the_current_temperature

        # select the pixel based on the current temperature
        if self.ambient < the_current_temperature < (self.ambient + .6):
            pixel = 0
        elif (self.ambient + 0.6) < the_current_temperature < (self.ambient + 1.2):
            pixel = 1
        elif (self.ambient < the_current_temperature + 1.2) < the_current_temperature < (self.ambient + 1.8):
            pixel = 2
        elif (self.ambient < the_current_temperature + 1.8) < the_current_temperature < (self.ambient + 2.4):
            pixel = 3
        elif (self.ambient < the_current_temperature + 2.4) < the_current_temperature < (self.ambient + 3.0):
            pixel = 4
        elif (self.ambient < the_current_temperature + 3.0) < the_current_temperature < (self.ambient + 3.6):
            pixel = 5
        elif (self.ambient < the_current_temperature + 3.6) < the_current_temperature < (self.ambient + 4.2):
            pixel = 6
        elif (self.ambient < the_current_temperature + 4.2) < the_current_temperature < (self.ambient + 4.8):
            pixel = 7
        elif (self.ambient < the_current_temperature + 4.8) < the_current_temperature < (self.ambient + 5.4):
            pixel = 8
        else:
            pixel = 9

        # get the rgb value for the pixel
        rgb = self.pix_d[pixel]

        # if this is not the same pixel as the last one enabled
        # then manipulate the pixels.

        # turn off the current pixel and turn on the new one.
        if pixel != self.last_pixel_used:
            # extinguish the previous pixel
            self.p.cpx_pixel_set(self.last_pixel_used, 0, 0, 0)

            # set the new pixel
            self.p.cpx_pixel_set(pixel, *rgb)

            # control the pixels
            self.p.cpx_pixels_show()
            self.last_pixel_used = pixel

        print('ambient: {} current: {} pixel{}'.format(round(self.ambient, 3),
                                                       round(the_current_temperature, 3),
                                                       pixel))