Пример #1
0
    def work(self):
        self.agent.parallel_cycle()

        ui.canvas.draw_string(10, 30, "9 Audio Test", (127, 127, 255), scale=3)
        ui.canvas.draw_string(10,
                              70,
                              "isconnected: %s" % (str)(self.isconnected),
                              (255, 127, 0),
                              scale=2)

        ui.canvas.draw_string(10,
                              100,
                              "Test: %s" %
                              ('play' if self.state == 0 else 'record'),
                              (255, 127, 0),
                              scale=3)

        #print(time.ticks_ms())

        if self.isconnected:
            if self.state == 0 and self.is_play:
                if CubeAudio.event() == False:
                    CubeAudio.load(os.getcwd() + "/res/sound/loop.wav", 100)
                    #print('self.count', self.count)
                    if self.count > 1:
                        CubeAudio.i2s.set_sample_rate(22050)
                    else:
                        # pass
                        CubeAudio.i2s.set_sample_rate(22050)
            elif self.state == 1:
                ui.canvas.draw_string(10,
                                      200,
                                      "Press Any-Key \n Start", (255, 127, 0),
                                      scale=3)
            elif self.state == 2 and self.is_record:
                tmp = CubeAudio.i2s.record(1024)
                fft_res = FFT.run(tmp.to_bytes(), 512)
                fft_amp = FFT.amplitude(fft_res)
                if fft_amp[50] > 100 and fft_amp[100] > 100:
                    AudioTest.RecordTest = True
                    sample_page.next()
                for x_shift in range(240):
                    hist_height = fft_amp[x_shift]
                    ui.canvas.draw_rectangle((x_shift, 0, 1, hist_height),
                                             [255, 255, 255], 1, True)
                    #print((x_shift, 0, 1, hist_height))

        if self.isError != None:
            ui.canvas.draw_string(40,
                                  80,
                                  self.isError, (255, 255, 255),
                                  scale=2)
            sample_page.next()
Пример #2
0
    def draw_demo():
        app.loop = (app.loop + 1) % 200
        value = math.cos(math.pi * app.loop / 16) * 10

        ui.canvas.draw_string(10,
                              5,
                              "Seeed Grove",
                              color=(40 + int(value) * 2, 240 + int(value) * 2,
                                     40 + int(value) * 2),
                              scale=3,
                              mono_space=0)

        try:
            CubeAudio.event()
            if app.isconnected == False:
                CubeAudio.event()

                if app.loop % 5 == 1:
                    result = app.i2c4.scan()
                    ui.canvas.draw_string(290,
                                          80,
                                          "Scan Dev: " + str(result),
                                          color=(140 + int(value) * 5,
                                                 240 + int(value) * 5,
                                                 140 + int(value) * 5),
                                          scale=2,
                                          mono_space=0)

                    if SHT3x_ADDR in result:
                        app.sht3x = SHT3x(app.i2c4, SHT3x_ADDR)
                        app.isconnected = True
                    CubeAudio.event()
                    if SHT31_ADDR in result:
                        app.sht3x = SHT3x(app.i2c4, SHT31_ADDR)
                        app.isconnected = True
                    CubeAudio.event()

                ui.canvas.draw_string(
                    280,
                    25,
                    "Wait Grove Sensor \n sht31/35 <<<  <<  <-",
                    color=(140 + int(value) * 5, 240 + int(value) * 5,
                           140 + int(value) * 5),
                    scale=2,
                    mono_space=0)

                if CubeAudio.event() == False:

                    value = math.cos(math.pi * app.loop / 100) * 50

                    tmp = int(value)
                    #print(value)

                    ui.canvas.draw_circle(0,
                                          0,
                                          100 + tmp,
                                          fill=False,
                                          color=(0, (150 + tmp) + 10, 0))
                    ui.canvas.draw_circle(0,
                                          0,
                                          100 + tmp * 2,
                                          fill=False,
                                          color=(0, (150 + tmp) + 20, 0))
                    ui.canvas.draw_circle(0,
                                          0,
                                          100 + tmp * 3,
                                          fill=False,
                                          color=(0, (150 + tmp) + 30, 0))
                    ui.canvas.draw_circle(0,
                                          0,
                                          100 + tmp * 4,
                                          fill=False,
                                          color=(0, (150 + tmp) + 40, 0))

            else:
                data = app.sht3x.read_temp_humd()
                # print(data)
                if app.sidu == None:
                    app.sidu = image.Image(os.getcwd() +
                                           "/res/images/sidu.jpg")

                ui.canvas.draw_circle(350,
                                      160,
                                      100,
                                      fill=True,
                                      color=(255, 255, 255))
                ui.canvas.draw_image(app.sidu,
                                     270,
                                     60,
                                     alpha=235 + int(value) * 2)
                ui.canvas.draw_string(330,
                                      190,
                                      "%.2d" % data[1],
                                      scale=4,
                                      color=(80, 80, 80))

                ui.canvas.draw_rectangle(60,
                                         60,
                                         180,
                                         200,
                                         thickness=4,
                                         color=(155, 155, 155))
                if len(app.points) > 18:
                    app.points.pop(0)
                elif data[0] > 1 and app.temp != int(data[0] * 10):
                    app.temp = int(data[0] * 10)
                    app.points.append(app.temp)
                for p in range(len(app.points)):
                    #print(app.points)
                    if p < 1:
                        b = (60 + int(10 * (p))), 450 - app.points[p]
                        ui.canvas.draw_circle(b[0],
                                              b[1],
                                              3,
                                              fill=True,
                                              color=(255, 155, 150))
                    else:
                        a, b = ((60 + int(10 * (p - 1))), 450 -
                                app.points[p - 1]), ((60 + int(10 * (p))),
                                                     450 - app.points[p])
                        ui.canvas.draw_circle(b[0],
                                              b[1],
                                              3,
                                              fill=False,
                                              color=(155, 155, 155))
                        ui.canvas.draw_line(a[0],
                                            a[1],
                                            b[0],
                                            b[1],
                                            thickness=4,
                                            color=(255, 255, 255))

                ui.canvas.draw_string(
                    60,
                    280,
                    "Average temperature: %s" %
                    str(sum(app.points) / len(app.points) / 10.0),
                    color=(240 + int(value) * 5, 240 + int(value) * 5,
                           240 + int(value) * 5),
                    scale=2,
                    mono_space=0)

            CubeAudio.event()
            ui.display()
        except Exception as e:
            app.layer = 1
            app.isconnected = False
            app.points = []
            raise e