Exemplo n.º 1
0
import time
from machine import Timer


def on_timer(timer):
    print("time up:", timer)
    print("param:", timer.callback_arg())


tim = Timer(Timer.TIMER0,
            Timer.CHANNEL0,
            mode=Timer.MODE_PERIODIC,
            period=1,
            unit=Timer.UNIT_S,
            callback=on_timer,
            arg=on_timer,
            start=False,
            priority=1,
            div=0)
print("period:", tim.period())
tim.start()
time.sleep(5)
tim.stop()
time.sleep(5)
tim.restart()
time.sleep(5)
tim.stop()
del tim
Exemplo n.º 2
0
class Battery:
    def __init__(self):
        """Initialize Battery."""

        self.bar_pos = 0
        self.exit = False

        lcd.clear()
        lcd.font("data/fonts/aril24.fon")
        lcd.set_bg(lcd.BLACK)

        # Battery icon.
        lcd.rect(12, 22, 10, 16, lcd.BLACK, lcd.BLACK)
        # Percentage rectangle.
        lcd.roundrect(45, 58, 70, 20, 8, lcd.BLACK, lcd.BLACK)

        self.refresh = Timer(0)
        self.refresh.init(period=10000,
                          mode=Timer.PERIODIC,
                          callback=self.show_battery)

        self.show_battery()

        while not self.exit:
            if buttonA.was_pressed:
                # Deinit timer, reset bg and exit.
                self.refresh.deinit()
                lcd.set_bg(0xFF8000)

                self.exit = True

    def show_battery(self, t=None):
        """Convert battery voltage into bars."""

        if axp.get_icharge_data() / 2 > 0:
            # If pluged in.
            self.refresh.period(500)

            if self.bar_pos == 0:
                # Battery Icon.
                lcd.rect(22, 5, 125, 50, lcd.BLACK, lcd.BLACK)

                lcd.roundrect(45, 58, 70, 20, 8, lcd.BLACK, lcd.BLACK)
                lcd.text(lcd.CENTER, 63, "--%", lcd.WHITE)

            else:
                lcd.rect(((6 - self.bar_pos) * 20) + 27, 10, 15, 40, lcd.GREEN,
                         lcd.GREEN)

            self.bar_pos += 1
            self.bar_pos %= 7

        else:
            self.refresh.period(10000)

            bat_data = axp.get_vbat_data() * 1.1

            vbat = round(self.map_value(bat_data, 3000, 4100, 0, 7))
            pbat = max(
                min(round(self.map_value(bat_data, 3000, 4100, 0, 100)), 100),
                0)

            print(bat_data)
            print(vbat)
            print(pbat)

            # Battery Icon.
            lcd.rect(22, 5, 125, 50, lcd.BLACK, lcd.BLACK)

            if vbat == 1:
                color = lcd.RED
            elif vbat == 2:
                color = lcd.ORANGE
            else:
                color = lcd.GREEN

            # Draw bars.
            for i in range(1, 7):
                if vbat >= i:
                    lcd.rect(((6 - i) * 20) + 27, 10, 15, 40, color, color)

            lcd.roundrect(45, 58, 70, 20, 8, lcd.BLACK, lcd.BLACK)
            lcd.text(lcd.CENTER, 63, str(pbat) + "%", lcd.WHITE)

    @staticmethod
    def map_value(x, in_min, in_max, out_min, out_max):
        return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min