本文整理汇总了Python中machine.ADC属性的典型用法代码示例。如果您正苦于以下问题:Python machine.ADC属性的具体用法?Python machine.ADC怎么用?Python machine.ADC使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类machine
的用法示例。
在下文中一共展示了machine.ADC属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: adc_read
# 需要导入模块: import machine [as 别名]
# 或者: from machine import ADC [as 别名]
def adc_read(self, pin: int = 0, **kwargs) -> int:
"""
Read an analog value from a PIN. Note that the ESP8266 only has one analog PIN, accessible on
the channel ``0``. If you are interested in the actual voltage that is measured then apply
``V = Vcc * (value/1024)``, where ``Vcc`` is the supply voltage provided to the device (usually 3V if
connected to the Vcc PIN of an ESP8266).
:param pin: GPIO PIN number (default: 0).
:param kwargs: Parameters to pass to :meth:`platypush.plugins.esp.EspPlugin.execute`.
:return: A value between ``0`` and ``1024``.
"""
code = '''
import machine
adc = machine.ADC({pin})
adc.read()
'''.format(pin=pin)
response = self.execute(code, **kwargs)
return int(response.output)
示例2: __init__
# 需要导入模块: import machine [as 别名]
# 或者: from machine import ADC [as 别名]
def __init__(self, settings):
"""
Initialized ADC unit.
"""
super().__init__(settings)
# ADC Pin to sample from.
self.pin = None
# Main resistor value (R1).
self.resistor_r1 = None
# Resistor between input pin and ground (R2).
self.resistor_r2 = None
# Reference to platform ADC object.
self.adc = None
self.setup()
示例3: __init__
# 需要导入模块: import machine [as 别名]
# 或者: from machine import ADC [as 别名]
def __init__(self, sensor_id='adc', min_rd=0, max_rd=1024,
min_val=0, max_val=1):
'''Initialize sensor
min_rd and max_rd are used in sample for sensor calibration
min_val and max_val are the sample limits
'''
self.sensor_id = sensor_id
self.min_rd = min_rd
self.max_rd = max_rd
self.min_val = min_val
self.max_val = max_val
self.coef = (max_val - min_val) / (max_rd - min_rd)
self.adc = ADC(0)
示例4: read
# 需要导入模块: import machine [as 别名]
# 或者: from machine import ADC [as 别名]
def read(self) -> int:
'''Get a sensor reading using Micropython API
Return 0-1024 direct ADC (0~3.3v) reading
'''
return self.adc.read()
示例5: sample
# 需要导入模块: import machine [as 别名]
# 或者: from machine import ADC [as 别名]
def sample(self) -> float:
'''Get an ADC interpolated reading using ThingFlow sensor API
Return min_val~max_val
'''
reading = self.read()
return self.min_val + (reading - self.min_rd) * self.coef
示例6: set_mode_analog_input
# 需要导入模块: import machine [as 别名]
# 或者: from machine import ADC [as 别名]
def set_mode_analog_input(self, payload):
"""
create an adc object
:param payload:
:return:
"""
if 'change_diff' in payload:
self.adc_diff_report = payload['change_diff']
self.adc = ADC(0)
示例7: set_mode_analog_input
# 需要导入模块: import machine [as 别名]
# 或者: from machine import ADC [as 别名]
def set_mode_analog_input(self,payload):
if 'change_diff' in payload:
self.adc_diff_report=payload['change_diff']
self.adc=ADC(0)
示例8: get_battery_voltage
# 需要导入模块: import machine [as 别名]
# 或者: from machine import ADC [as 别名]
def get_battery_voltage():
"""
Returns the current battery voltage. If no battery is connected, returns 3.7V
This is an approximation only, but useful to detect of the charge state of the battery is getting low.
"""
adc = ADC(Pin(BAT_VOLTAGE)) # Assign the ADC pin to read
measuredvbat = adc.read() # Read the value
measuredvbat /= 4095 # divide by 4095 as we are using the default ADC voltage range of 0-1V
measuredvbat *= 3.7 # Multiply by 3.7V, our reference voltage
return measuredvbat
# Return the current charge state of the battery - we need to read the value multiple times
# to eliminate false negatives due to the charge IC not knowing the difference between no battery
# and a full battery not charging - This is why the charge LED flashes
示例9: vcc
# 需要导入模块: import machine [as 别名]
# 或者: from machine import ADC [as 别名]
def vcc():
import machine
mv = machine.ADC(0)
return mv.read() * 1.024
示例10: readRaw
# 需要导入模块: import machine [as 别名]
# 或者: from machine import ADC [as 别名]
def readRaw(self) -> int:
# just loboris fork compatibility although support officialy dropped.
if isinstance(self, machine.ADC):
# Subclass of hardware ADC
return self.read() if platform != "esp32_Lobo" else self.readraw()
return self.read() # on non-hardware ADCs read() always returns raw values
示例11: maxVoltage
# 需要导入模块: import machine [as 别名]
# 或者: from machine import ADC [as 别名]
def maxVoltage() -> float:
return 3.3 # esp standard voltage
# The following methods are overwritten by machineADC, the machine.ADC class, by the proper hardware methods
# In other subclasses they have to be implemented
示例12: width
# 需要导入模块: import machine [as 别名]
# 或者: from machine import ADC [as 别名]
def width(self, *args, **kwargs):
raise NotImplementedError("Width not supported")
# machineADC = type("ADC", (machine.ADC, pyADC), {}) # machine.ADC subclass
示例13: ADC
# 需要导入模块: import machine [as 别名]
# 或者: from machine import ADC [as 别名]
def ADC(pin, atten=None, *args, **kwargs) -> pyADC:
if type(pin) == str:
raise TypeError("ADC pin can't be string")
if isinstance(pin, pyADC):
# must be a completely initialized ADC otherwise it wouldn't be a subclass of pyADC
# could be machineADC, Arduino ADC or even Amux or Amux ADC object
return pin
if type(pin) == machine.ADC:
# using a hacky way to re-instantiate an object derived from machine.ADC by
# reading the used pin from machine.ADC string representation and creating it again.
# This does not retain the set atten value sadly.
# It is however needed so that isinstance(adc, machine.ADC) is always True for hardware ADCs.
astr = str(pin)
if platform == "esp32_Lobo": # ADC(Pin(33): unit=ADC1, chan=5, width=12 bits, atten=0dB (1.1V), Vref=1100 mV)
pin = int(astr[astr.rfind("ADC(Pin(") + 8:astr.find("):")])
elif platform == "esp8266": # esp8266 only has one ADC
pin = 0
elif platform == "esp32": # ADC(Pin(33))
pin = int(astr[astr.rfind("(") + 1:astr.rfind("))")])
else:
raise NotImplementedError("Platform {!s} not implemented".format(platform))
if type(pin) == int:
if platform == "esp32" or platform == "esp32_LoBo":
adc = machineADC(machine.Pin(pin), *args, **kwargs)
adc.atten(adc.ATTN_11DB if atten is None else atten)
return adc
elif platform == "esp8266":
return machineADC(pin, *args, **kwargs) # esp8266 does not require a pin object
else:
raise NotImplementedError(
"Platform {!s} not implemented, please report".format(platform))
raise TypeError("Unknown type {!s} for ADC object".format(type(pin)))
示例14: __init__
# 需要导入模块: import machine [as 别名]
# 或者: from machine import ADC [as 别名]
def __init__(self, s0, s1, s2, s3=None, mux=None, adc=None, return_voltages=False):
""" It is possibile to initialize with:
- pin numbers (or string on esp8266)
- mux object and pin numbers (of mux pins)
- Pin objects (either from machine or mux Pin objects [no mux object needed], or Arduino)
:type return_voltages: bool, True returns voltages on .read() else raw adc value
:type mux: Mux object if a multiplexer is used
:type adc: ADC pin number (esp32) or None (esp8266) or Arduino ADC object or any ADC object
Amux uses default return values of ADC in .read()
--> On esp8266/esp32 raw, on esp32_LoBo voltage
s3 is optional, only needed if 16 pins are used, 8 pins possible with s0-s2.
Amux can be read like a list: value=amux[2]
"""
if mux:
# MUX pin numbers, not pin objects
self._s0 = s0
self._s1 = s1
self._s2 = s2
self._s3 = s3
self._mux = mux
else:
# Pin will take care of returning the correct object
self._s0 = Pin(s0, machine.Pin.OUT)
self._s1 = Pin(s1, machine.Pin.OUT)
self._s2 = Pin(s2, machine.Pin.OUT)
if s3:
self._s3 = Pin(s3, machine.Pin.OUT)
if s3:
self.__size = 16
else:
self.__size = 8
self._return_voltages = return_voltages
self._adc = _ADC(
adc) # no matter what adc is, _ADC will return an object with the unified ADC API
示例15: __init__
# 需要导入模块: import machine [as 别名]
# 或者: from machine import ADC [as 别名]
def __init__(self, name, precision=1, threshold=None,
on_change=None, report_change=True, filter=None):
self.precision = precision
self.threshold = None
if threshold is not None:
self.threshold = max(1, min(threshold, 1023))
self.last_value = None
Device.__init__(self, name, ADC(0), on_change=on_change,
report_change=report_change, filter=filter)