MikrofonSensor und TemperaturSenor die zwei Python programme funktionieren. mit den jeweiligen 2 json Datein. Beim TemperaturSensor wird im Terminal keine Wertre ausgegeben aber in der json Datei kann man die Temp und Hum sehen.

This commit is contained in:
Chiara 2025-05-28 14:53:44 +02:00
parent 4c654ec969
commit 1751076592
2614 changed files with 349009 additions and 0 deletions

View file

@ -0,0 +1,141 @@
# SPDX-FileCopyrightText: 2024 Brent Rubell for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""Mock PWMOut Wrapper for Generic Agnostic Board"""
class PWMError(IOError):
"""Base class for PWM errors."""
class PWMOut:
"""Pulse Width Modulation Output Class"""
def __init__(self, pin, *, frequency=500, duty_cycle=0, variable_frequency=False):
self._pwmpin = None
self._period = 0
self._open(pin, duty_cycle, frequency, variable_frequency)
def __del__(self):
self.deinit()
def __enter__(self):
return self
def __exit__(self, t, value, traceback):
self.deinit()
def _open(self, pin, duty=0, freq=500, variable_frequency=False):
self._pin = pin
# set frequency
self.frequency = freq
self._variable_frequency = variable_frequency
# set duty
self.duty_cycle = duty
self.enabled = True
def deinit(self):
"""Deinit the PWM."""
if self._pwmpin is not None:
self._pwmpin = None
def _is_deinited(self):
if self._pwmpin is None:
raise ValueError(
"Object has been deinitialize and can no longer "
"be used. Create a new object."
)
@property
def period(self):
"""Get or set the PWM's output period in seconds.
Raises:
PWMError: if an I/O or OS error occurs.
TypeError: if value type is not int or float.
:type: int, float
"""
return 1.0 / self.frequency
@period.setter
def period(self, period):
if not isinstance(period, (int, float)):
raise TypeError("Invalid period type, should be int or float.")
self.frequency = 1.0 / period
@property
def duty_cycle(self):
"""Get or set the PWM's output duty cycle which is the fraction of
each pulse which is high. 16-bit
Raises:
PWMError: if an I/O or OS error occurs.
TypeError: if value type is not int or float.
ValueError: if value is out of bounds of 0.0 to 1.0.
:type: int, float
"""
return int(self._duty_cycle * 65535)
@duty_cycle.setter
def duty_cycle(self, duty_cycle):
if not isinstance(duty_cycle, (int, float)):
raise TypeError("Invalid duty cycle type, should be int or float.")
if not 0 <= duty_cycle <= 65535:
raise ValueError("Invalid duty cycle value, should be between 0 and 65535")
# convert from 16-bit
duty_cycle /= 65535.0
self._duty_cycle = duty_cycle
@property
def frequency(self):
"""Get or set the PWM's output frequency in Hertz.
Raises:
PWMError: if an I/O or OS error occurs.
TypeError: if value type is not int or float.
:type: int, float
"""
return self._frequency
@frequency.setter
def frequency(self, frequency):
if not isinstance(frequency, (int, float)):
raise TypeError("Invalid frequency type, should be int or float.")
self._frequency = frequency
@property
def enabled(self):
"""Get or set the PWM's output enabled state.
Raises:
PWMError: if an I/O or OS error occurs.
TypeError: if value type is not bool.
:type: bool
"""
return self._enabled
@enabled.setter
def enabled(self, value):
if not isinstance(value, bool):
raise TypeError("Invalid enabled type, should be string.")
self._enabled = value
# String representation
def __str__(self):
return "pin %s (freq=%f Hz, duty_cycle=%f%%)" % (
self._pin,
self.frequency,
self.duty_cycle,
)

View file

@ -0,0 +1,56 @@
# SPDX-FileCopyrightText: 2024 Brent Rubell for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
`analogio` - Analog input and output control
=================================================
See `CircuitPython:analogio` in CircuitPython for more details.
* Author(s): Carter Nelson
"""
from adafruit_blinka.microcontroller.generic_agnostic_board.pin import Pin
from adafruit_blinka import ContextManaged
class AnalogIn(ContextManaged):
"""Analog Input Class"""
def __init__(self, pin):
self._pin = Pin(pin.id)
self._pin.init(mode=Pin.ADC)
@property
def value(self):
"""Read the ADC and return the value"""
return self._pin.value()
# pylint: disable=no-self-use
@value.setter
def value(self, value):
# emulate what CircuitPython does
raise AttributeError("'AnalogIn' object has no attribute 'value'")
# pylint: enable=no-self-use
def deinit(self):
del self._pin
class AnalogOut(ContextManaged):
"""Analog Output Class"""
def __init__(self, pin):
self._pin = Pin(pin.id)
self._pin.init(mode=Pin.DAC)
@property
def value(self):
"""Fake the output."""
return self._pin.value()
@value.setter
def value(self, value):
self._pin.value(value)
def deinit(self):
del self._pin

View file

@ -0,0 +1,20 @@
# SPDX-FileCopyrightText: 2024 Brent Rubell for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""Chip Definition for a generic, os-agnostic, board."""
class GENERIC_AGNOSTIC_BOARD:
"""Generic Agnostic Board Device Class Definition"""
def __init__(self):
pass # This implementation is for a generic board, no initialization is required
def __del__(self):
# try to close the device before destroying the instance
return
# pylint: enable=unused-argument
generic_agnostic_board = GENERIC_AGNOSTIC_BOARD()

View file

@ -0,0 +1,26 @@
# SPDX-FileCopyrightText: 2024 Brent Rubell for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""I2C Class for Generic Agnostic Board"""
from random import randint
# from .generic_agnostic_board.pin import generic_agnostic_board
class I2C:
"""Custom I2C Class for a Generic Agnostic Board"""
def __init__(self, *, frequency=100000):
# self._generic_agnostic_board = generic_agnostic_board
self.freq = frequency
@staticmethod
def scan():
"""Mocks an I2C scan and returns a list of 3 randomly generated
I2C addresses from 0x0 to 0x79.
"""
# Generate a list of 3 randomly generated addresses from 0x0 to 0x79
address_list = []
for _ in range(3):
address_list.append(randint(0x0, 0x79))
return address_list

View file

@ -0,0 +1,18 @@
# SPDX-FileCopyrightText: 2024 Brent Rubell for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""NeoPixel write mocks for a generic board."""
# pylint: disable=unused-argument
def neopixel_write(gpio, buf):
"""Mocks a neopixel_write function"""
# pad output buffer from 3 bpp to 4 bpp
buffer = []
for i in range(0, len(buf), 3):
buffer.append(0)
buffer.append(buf[i + 2])
buffer.append(buf[i + 1])
buffer.append(buf[i])
# then, do nothing

View file

@ -0,0 +1,228 @@
# SPDX-FileCopyrightText: 2024 Melissa LeBlanc-Williams for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""generic_agnostic_board pin interface"""
import random
# Values for sine wave
# (data points = 20, amplitude=100, frequency=1)
sine_wave = [
0,
31,
59,
81,
95,
100,
95,
81,
59,
31,
0,
-31,
-59,
-81,
-95,
-100,
-95,
-81,
-59,
-31,
]
# Values for a sawtooth wave
# (data points = 20, amplitude=100)
sawtooth_wave = [
-100,
-80,
-60,
-40,
-20,
0,
20,
40,
60,
80,
-100,
-80,
-60,
-40,
-20,
0,
20,
40,
60,
80,
]
class Pin:
"""A basic Pin class for use with generic_agnostic_board"""
# pin modes
OUT = 0
IN = 1
ADC = 2
DAC = 3
# pin values
LOW = 0
HIGH = 1
# pin pulls
PULL_NONE = 0
PULL_UP = 1
PULL_DOWN = 2
# pylint: disable=no-self-use
def return_toggle(self):
"""Returns the pin's expected value, toggling between True and False"""
toggle_state = not self.previous_value
return toggle_state
def return_false(self):
"""Returns the pin's expected value, False"""
return False
def return_true(self):
"""Returns the pin's expected value, True"""
return True
def return_random_int(self):
"""Returns a random integer"""
return random.randint(0, 65535)
def return_fixed_int_pi(self):
"""Returns the first five digits of Pi, 31415"""
return 31415
def return_sine_wave(self):
"""Returns the next value in the sine wave"""
if self._wave_idx is None:
self._wave_idx = 0
else:
self._wave_idx = (self._wave_idx + 1) % len(sine_wave)
return sine_wave[self._wave_idx]
def return_sawtooth_wave(self):
"""Returns the next value in the sawtooth wave"""
if self._wave_idx is None:
self._wave_idx = 0
else:
self._wave_idx = (self._wave_idx + 1) % len(sawtooth_wave)
return sawtooth_wave[self._wave_idx]
def __init__(self, pin_id=None):
self.id = pin_id
self._mode = None
self._pull = None
self.previous_value = False
self.current_value = None
self._wave_idx = None
# mapping of pin definition names to expected behavior
self.pin_behavior = {
0: self.return_true, # Dx_INPUT_TRUE
1: self.return_false, # Dx_INPUT_FALSE
2: self.return_true, # Dx_INPUT_TRUE_PULL_UP
3: self.return_true, # Dx_INPUT_TRUE_PULL_DOWN
4: self.return_true, # Dx_OUTPUT
7: self.return_random_int, # Ax_INPUT_RAND_INT
8: self.return_fixed_int_pi, # Ax_INPUT_FIXED_INT_PI
9: self.return_sine_wave, # Ax_INPUT_WAVE_SINE
10: self.return_sawtooth_wave, # Ax_INPUT_WAVE_SAW
11: self.return_toggle, # Dx_INPUT_TOGGLE
}
def init(self, mode=IN, pull=None):
"""Initialize the Pin"""
if self.id is None:
raise RuntimeError("Can not init a None type pin.")
pull = Pin.PULL_NONE if pull is None else pull
self._pull = pull
self._mode = mode
def write(self, new_value):
"""Saves the new_value to the pin for subsequent calls to .value"""
self.previous_value = self.current_value
self.current_value = new_value
def read(self):
"""Returns the pin's expected value."""
self.previous_value = self.current_value
# perform a lookup on the pin_behavior dict to get the value
self.current_value = self.pin_behavior.get(self.id)()
# is pin a pull up and pin is LOW?
if self._pull == Pin.PULL_UP and self.current_value is False:
self.current_value = False
# is pin a pull down and pin is HIGH?
if self._pull == Pin.PULL_DOWN and self.current_value is True:
self.current_value = False
return self.current_value
def value(self, val=None):
"""Set or return the Pin Value"""
# Digital In / Out
if self._mode in (Pin.IN, Pin.OUT):
# digital read
if val is None:
return self.read()
# digital write
if val in (Pin.LOW, Pin.HIGH):
return self.write(val)
# nope
raise ValueError("Invalid value for pin.")
# Analog In
if self._mode == Pin.ADC:
if val is None:
return self.read()
# read only
raise AttributeError("'AnalogIn' object has no attribute 'value'")
# Analog Out
if self._mode == Pin.DAC:
if val is None:
self.previous_value = self.current_value
return self.current_value
self.write(val)
return None
raise RuntimeError(
"No action for mode {} with value {}".format(self._mode, val)
)
# create pin instances for each pin
D0 = Pin(0)
D1 = Pin(1)
D2 = Pin(2)
D3 = Pin(3)
D4 = Pin(4)
# Special "digital" pins
D6 = Pin(6)
# Analog pins
A0 = Pin(7)
A1 = Pin(8)
A2 = Pin(9)
A3 = Pin(10)
A4 = Pin(12)
# Special digital pins for pixels
D7 = Pin(11)
D8 = Pin(13)
D9 = Pin(14)
# I2C pins
SDA = Pin()
SCL = Pin()
# SPI pins
SCLK = Pin()
SCK = Pin()
MOSI = Pin()
MISO = Pin()
CS = Pin()
spiPorts = ((0, SCK, MOSI, MISO),)
# UART pins
UART_TX = Pin()
UART_RX = Pin()

View file

@ -0,0 +1,57 @@
# SPDX-FileCopyrightText: 2024 Brent Rubell for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""SPI class for a generic agnostic board."""
# from .rp2040_u2if import rp2040_u2if
# pylint: disable=protected-access, no-self-use
class SPI:
"""SPI Base Class for a generic agnostic board."""
MSB = 0
def __init__(self, index, *, baudrate=100000):
self._index = index
self._frequency = baudrate
# pylint: disable=too-many-arguments,unused-argument
def init(
self,
baudrate=1000000,
polarity=0,
phase=0,
bits=8,
firstbit=MSB,
sck=None,
mosi=None,
miso=None,
):
"""Initialize the Port"""
self._frequency = baudrate
# pylint: enable=too-many-arguments
@property
def frequency(self):
"""Return the current frequency"""
return self._frequency
# pylint: disable=unnecessary-pass
def write(self, buf, start=0, end=None):
"""Write data from the buffer to SPI"""
pass
# pylint: disable=unnecessary-pass
def readinto(self, buf, start=0, end=None, write_value=0):
"""Read data from SPI and into the buffer"""
pass
# pylint: disable=too-many-arguments, unnecessary-pass
def write_readinto(
self, buffer_out, buffer_in, out_start=0, out_end=None, in_start=0, in_end=None
):
"""Perform a half-duplex write from buffer_out and then
read data into buffer_in
"""
pass