Esempio n. 1
0
def error_indicator(n, flashmode=0, onoff=1):
    global led, status_led, pwmled, DEBUG
#    # if DEBUG: print("error_indicator(): n = %d, flashmode = %d, onoff = %d" % (n, flashmode, onoff))
    if n > 0:
        n = n - 1
        led[n].value(1)
        if flashmode == 0:
                if status_led[n] is not None:
#                    # if DEBUG: print("error_indicator(): resetting RMT/PWM for LED %d" % n)
                    status_led[n].loop(False)
                    status_led[n].deinit()
                if pwmled[n] is not None:
                    pwmled[n].deinit()
                    pwmled[n] = None
                pwmled[n] = PWM(led[n], freq=20000, duty=1023-200*onoff)
        else:
            if status_led[n] is not None:
                status_led[n].deinit()
                status_led[n] = None
            if pwmled[n] is not None:
                pwmled[n].deinit()
                pwmled[n] = None
            status_led[n] = esp32.RMT(n, pin=led[n], clock_div=255)
            status_led[n].loop(True)
        if flashmode == 1:
            if status_led[n] is not None:
                status_led[n].write_pulses((32767, 1, 32767, 8192, 1), start=1)
        elif flashmode == 2:
            if status_led[n] is not None:
                status_led[n].write_pulses((16384, 1, 16384, 16384, 1), start=0)
        elif flashmode == 3:
            if status_led[n] is not None:
                status_led[n].write_pulses((32767, 1, 8192, 8192, 1), start=0)
    else:
        if flashmode == 0:
            for i in range(0, NLED):
                if status_led[i] is not None:
                    status_led[i].loop(False)
                    status_led[i].deinit()
                    status_led[i] = None
                #led[i].init(mode=Pin.OUT)
                if pwmled[i] is not None:
                    pwmled[i].deinit()
                    pwmled[i] = None
                if i % 2 == 0:
                    pwmled[i] = PWM(led[i], freq=20000, duty=1023)
                else:
                    pwmled[i] = PWM(led[i], freq=20000, duty=1023-200*onoff)
        else:
            # Exiting
            for i in range(0, NLED):
                if status_led[i] is not None:
                    status_led[i].loop(False)
                    status_led[i].deinit()
                    status_led[i] = None
                if pwmled[i] is not None:
                    pwmled[i].deinit()
                    pwmled[i] = None
                led[i].init(mode=Pin.OUT)
                led[i].value(1)
Esempio n. 2
0
 def __init__(self, pin, pixel_count, rmt_channel=1, pixel_channels=3):
     self.rmt = esp32.RMT(rmt_channel, pin=pin, clock_div=4)
     # pixels stores the data sent out via RMT
     self.channels = pixel_channels
     single_pixel = (0, ) * pixel_channels
     self.pixels = [D_ZERO * (pixel_channels * CHANNEL_WIDTH)] * pixel_count
     # colors is only used for __getitem__
     self.colors = [single_pixel] * pixel_count
     self.n = pixel_count
Esempio n. 3
0
def board_init():
    global LED1, LED2, KEY1, NLED, led, pwmled, key1, status_led
    KEY1.init(mode=Pin.IN, pull=Pin.PULL_UP)
    key1 = KEY1
    LED1.init(mode=Pin.OUT)
    led.append(LED1)
    LED2.init(mode=Pin.OUT)
    led.append(LED2)
    for i in range(0, NLED):
        pwmled.append(None)
        status_led.append(None)
        rmt = esp32.RMT(i, pin=led[i])
        rmt.deinit()
        pwm = PWM(led[i])
        pwm.deinit()
        led[i].init(mode=Pin.OUT)
        led[i].value(1)
Esempio n. 4
0
    def __init__(self,pin,freq=5000,duty=512):
        
        for i in range(len(channel)):
            if channel[i] == None:
                channel[i] = i
                self.pwmchannel = i
                break
            
        self.pin = pin
        self.freq_alt = freq
        self.duty_alt = duty
        
        if self.freq_alt < 35: #防止 write_pulses 的 tuple 內元素值超過
            self.freq_alt = 35
        if self.duty_alt < 2 :
            self.duty_alt = 2

        period = 1000000/self.freq_alt  #pwm 週期時間, 單位 us  ----[請看最下面註解]
        high_t = int(period*(self.duty_alt/1023)) #高電位一周期內持續時間
        low_t = int(period - high_t) #低電位持續時間 = 週期 - 高電位持續時間
        
        self.r = esp32.RMT(self.pwmchannel, pin=self.pin, clock_div=80) #預設clock_div=80, pwm 單為時間為 us
        self.r.write_pulses((high_t,low_t),start=1)
Esempio n. 5
0
import esp32
from machine import Pin

led1 = Pin(33, Pin.OUT)
led2 = Pin(32, Pin.OUT)
led1.value(1)
led2.value(1)

r = esp32.RMT(0, pin=l, clock_div=255)
r.loop(True)
r.write_pulses((16384, 1, 16384, 16384, 1), start=0)
r.write_pulses((32767, 1, 32767, 32767, 1), start=0)
r.write_pulses((32767, 1, 32767, 1, 32767, 32767, 1, 32767, 1), start=0)
# Date: 2020-04-18
#############################################################

from machine import Pin
import utime as time
import esp32

LED_OFF = 1  # 1=OFF, 0=ON
LED_GPIO = 5  # use GPIO5 for LED output

led = Pin(LED_GPIO, Pin.OUT)

# Use the RMT module to generate pulses
# The freq. of input clock to RMT is always 80MHz.
# 80MHz/250/32000 = 10Hz
rmt = esp32.RMT(id=0, pin=led, clock_div=250)
# Generate pulses repeatedly
rmt.loop(True)
# Send 0 first, followed by 1
# The pulse width for 1 and 0 is 32000 (15-bit value).
rmt.write_pulses([32000, 32000], start=0)

try:
    while True:
        time.sleep(-1)
except KeyboardInterrupt:
    rmt.loop(False)
finally:
    rmt.deinit()
    led.init(mode=Pin.IN, value=1)
Esempio n. 7
0
 def __init__(self):
     self.rmt = esp32.RMT(
         0, pin=Pin(27), clock_div=8
     )  # RMT(channel=0, pin=18, source_freq=80000000, clock_div=8)
# File: ws2812b_rmt_demo-1.py
# Date: 2020-05-27

from machine import Pin
import esp32
import utime as time

WS2812B_PIN = Pin(27)

# create an RMT object, use clock divider = 8
# => The resolution is 100ns or 0.1us
rmt = esp32.RMT(id=0, pin=WS2812B_PIN, clock_div=8)

# define bit timings: pulse widths
BIT0 = (4, 8)  # T0H = 0.4us, T0L = 0.8us
BIT1 = (8, 4)  # T1H = 0.8us, T1L = 0.4us

# test colors
RED_COLOR = 8 * BIT0 + 8 * BIT1 + 8 * BIT0
GREEN_COLOR = 8 * BIT1 + 8 * BIT0 + 8 * BIT0
BLUE_COLOR = 8 * BIT0 + 8 * BIT0 + 8 * BIT1
COLORS = [RED_COLOR, GREEN_COLOR, BLUE_COLOR]

try:
    # press Ctrl+C to terminate
    while True:  # main loop
        for bits in COLORS:
            # send data to RMT
            rmt.write_pulses(bits, start=1)
            time.sleep(1.0)
except KeyboardInterrupt:
Esempio n. 9
0
esp.flash_read(byte_offset, buffer)

# The esp32 module:
import esp32

esp32.hall_sensor()  # read the internal hall sensor
esp32.raw_temperature(
)  # read the internal temperature of the MCU, in Fahrenheit
esp32.ULP()  # access to the Ultra-Low-Power Co-processor

# RMT

import esp32
from machine import Pin

r = esp32.RMT(0, pin=Pin(18), clock_div=8)
r  # RMT(channel=0, pin=18, source_freq=80000000, clock_div=8)
# The channel resolution is 100ns (1/(source_freq/clock_div)).
r.write_pulses((1, 20, 2, 40),
               0)  # Send 0 for 100ns, 1 for 2000ns, 0 for 200ns, 1 for 4000ns

# For low-level driving of a NeoPixel:
# BUG: Why is esp.neopixel_write not stubbed ?
pin = Pin(18)
grb_buf = (1, 20, 2, 40)
is800khz = False

import esp

esp.neopixel_write(pin, grb_buf, is800khz)  # type: ignore # FIXME:
 def __init__(self, pin, n=1):
     self._n = n
     self._data = n * [(0, 0, 0)]
     self._rmt = esp32.RMT(0, pin=pin, clock_div=8)
Esempio n. 11
0
    utime.sleep_us(100)
    pin.value(0)
    utime.sleep_us(300)

# PWM
# Osciloscopio 400us Y -T - 2v DC x1
from machine import Pin, PWM
pin_pwm = PWM(Pin(33, Pin.OUT), freq=1000)
pin_pwm.duty(1023)  # 100%
pin_pwm.duty(511)  # 50%
pin_pwm.duty(768)  # 75%
pin_pwm.duty(255)  # 25%


# RMT pulsos microsegundos
# Osciloscopio 10us Y -T - 2v DC x1
from machine import Pin
import esp32
np_rmt = esp32.RMT(0, pin=Pin(33), clock_div=8)  # RMT(channel=0, pin=x, source_freq=80000000, clock_div=8)
# La resolucion del canal es 100ns (1/(source_freq/clock_div)).
np_rmt.write_pulses((7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6), start=1)
np_rmt.write_pulses((7, 18, 7, 18, 7, 18, 7, 18, 7, 18, 7, 18, 7, 18, 7, 18), start=1)



# UltrasonicSensor
# Osciloscopio 400us Y -T - 2v DC x1 (para ver pulso inicial) pasar a 10 ms para ver respuesta
from ultrasonic_sensor import UltrasonicSensor
sensor_distancia = UltrasonicSensor(32)
sensor_distancia.get_distancia()