Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial commit #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
340 changes: 0 additions & 340 deletions COPYING

This file was deleted.

File renamed without changes.
17 changes: 0 additions & 17 deletions __init__.py

This file was deleted.

Empty file removed activity.py
Empty file.
8 changes: 0 additions & 8 deletions activity/activity.info

This file was deleted.

4 changes: 0 additions & 4 deletions activity/sensor.svg

This file was deleted.

7 changes: 7 additions & 0 deletions req.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
RPi.GPIO
adafruit-blinka
adafruit-circuitpython-displayio-ssd1306
adafruit-circuitpython-ssd1306
adafruit-circuitpython-display-text
adafruit-circuitpython-hcsr04
adafruit-circuitpython-dht
1 change: 1 addition & 0 deletions rpi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# import all fns with usage description here
188 changes: 188 additions & 0 deletions sensors/general.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
# Functions for:
# - Digital output
# - Digital input
# - Delay
# - Button
# - HC-SR04 Ultrasonic distance sensor
# - DHT11 Temperature and Humidity sensor
# - OLED display


import os
import time
import pwmio
import board
import digitalio
import subprocess
# from adafruit_motor import servo
import adafruit_dht
import adafruit_motor.motor as motor
import adafruit_hcsr04

cwd = os.getcwd()


# ------------- Digital output ------------- #
def digitalWrite(pin_no: str, val: float):
if not hasattr(board, pin_no):
return # + send a message
pin = digitalio.DigitalInOut(getattr(board, pin_no))
pin.direction = digitalio.Direction.OUTPUT
pin.value = int(val)


# ------------- Digital input ------------- #
def digitalRead(pin_no: str):
if not hasattr(board, pin_no):
return
pin = digitalio.DigitalInOut(getattr(board, pin_no))
pin.direction = digitalio.Direction.INPUT
return bool(pin.value)


# ------------- Delay ------------- #
def delay(ms: int):
time.sleep(ms / 1000)


# ------------- Button ------------- #
def button(pin: str):
button = 0
try:
button = digitalio.DigitalInOut(getattr(board, pin))
button.direction = digitalio.Direction.INPUT
except Exception:
None
button.pull = digitalio.Pull.UP
return not button.value


# ------------- HC-SR04 Ultrasonic distance sensor ------------- #
class _dist:
def def_dist(self, trigger: str, echo: str):
try:
self.sonar = adafruit_hcsr04.HCSR04(
trigger_pin=getattr(board, trigger),
echo_pin=getattr(board, echo))
except Exception:
return

def dst(self):
try:
s = 0.0
for i in range(20):
if self.sonar.distance < 1:
i = i - 1
continue
s += self.sonar.distance
return round(s / 20, 1)
except Exception:
return 0.0


dist = _dist()


# ------------- DHT Temperature and Humidity sensor ------------- #
class _dht:
def def_dht(self, pin: str):
try:
self.dhtDevice = adafruit_dht.DHT11(getattr(board, pin))
except Exception:
return

def temp(self):
try:
return self.dhtDevice.temperature
except Exception:
return 0.0

def hum(self):
try:
return self.dhtDevice.humidity
except Exception:
return 0.0


dht = _dht()


# ------------- OLED display ------------- #
class _oled:
def define(self, _height, _width, _text_color):
self.width = int(_width)
self.height = int(_height)
self.text_color = int(_text_color)

def print(self, text):
subprocess.Popen(
"python3 "
+ cwd
+ "/plugins/rpi/sensors/oled_display.py "
+ str(self.height)
+ " "
+ str(self.width)
+ " "
+ str(self.text_color)
+ " '"
+ str(text)
+ "'",
shell=True,
)


oled = _oled()


# ------------- PIR sensor ------------- #
def pir_motion(pin: str) -> bool:
try:
pir = digitalio.DigitalInOut(getattr(board, pin))
pir.direction = digitalio.Direction.INPUT
return bool(pir.value)
except Exception:
return False


# ------------- SERVO motor ------------- #
# *need to make usability easy and more controlable
# def servo_motor(pin: str, times, pulse_ms: int, frequency=50):
# pwm = pwmio.PWMOut(getattr(board, pin), duty_cycle=2**15, frequency=50)
# servo_m = servo.Servo(pwm)
# duty_cycle = int(pulse_ms / (period_ms / 65535.0))

# for i in range(0, times):
# for angle in range(0, 180, 5): # 0 - 180 degrees, 5 Degree steps
# servo.angle = angle
# time.sleep(0.05)
# for angle in range(180, 0, -5): # 180 - 0 degrees, 5 Degree steps
# servo.angle = angle
# time.sleep(0.05)


# ------------- PWM led ------------- #
def pwm_led(pin: str, duty_cycle: int):
led = pwmio.PWMOut(getattr(board, pin), frequency=5000, duty_cycle=0)
led.duty_cycle = int(duty_cycle / 100 * 65535)
if duty_cycle == 0:
led.deinit()

# # Example usage
# pwm_led('D14', 50) # Set to 50% brightness or duty cycle
# sleep(1) # Keep the LED on for 1 second


# ------------- DC Motor (not tested yet) ------------- #
def run_dc_motor(pin1, pin2, duration: float):
pwm_a = pwmio.PWMOut(getattr(board, pin1), frequency=5000)
pwm_b = pwmio.PWMOut(getattr(board, pin2), frequency=5000)

m = motor.DCMotor(pwm_a, pwm_b)
m.decay_mode = 0
m.throttle = 1.0

time.sleep(duration)
m.throttle = 0

# Example usage of the function
# run_dc_motor('D14', 'D15', 2) # Run the motor for 2 seconds
59 changes: 59 additions & 0 deletions sensors/oled_display.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import sys
import time
import board
import displayio
from i2cdisplaybus import I2CDisplayBus
from fourwire import FourWire
import terminalio
from adafruit_display_text import label
import adafruit_displayio_ssd1306


def main(width, height, text_color, text):
height = int(height)
width = int(width)
text_color = int(text_color)
displayio.release_displays()
oled_reset = board.D27

# Use for I2C
i2c = board.I2C()
display_bus = I2CDisplayBus(i2c, device_address=0x3C, reset=oled_reset)

# Use for SPI
# spi = board.SPI()
# oled_cs = board.D5
# oled_dc = board.D6
# display_bus = FourWire(spi, command=oled_dc, chip_select=oled_cs,
# reset=oled_reset, baudrate=1000000)
display = adafruit_displayio_ssd1306.SSD1306(
display_bus, width=width, height=height
)
splash = displayio.Group()
display.root_group = splash

# clear oled
color_bitmap = displayio.Bitmap(width, height, 1)
color_palette = displayio.Palette(1)
color_palette[0] = 0xFFFFFF if text_color == 0 else 0x000000
bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette,
x=0, y=0)
splash.append(bg_sprite)

# print on oled
if text != "":
if len(text) > 20:
text = text[:20]
text_area = label.Label(
terminalio.FONT,
text=text,
color=0x000000 if text_color == 0 else 0xFFFFFF,
x=5,
y=5,
)
splash.append(text_area)
time.sleep(0.2)


if __name__ == "__main__":
main(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])
4 changes: 0 additions & 4 deletions setup.py

This file was deleted.