Noah H
Published

PCL Raspi Task Manager

A task reminder/Manager system running on a Raspi 3b with a 240x320 TFT SPI display.

IntermediateFull instructions provided2 hours83
PCL Raspi Task Manager

Things used in this project

Story

Read more

Schematics

Wiring Diagram

Code

code.py

Python
Run just anywhere on your RasPI using Python3
import time
import busio
import digitalio
import board
import pymongo
from datetime import datetime
from PIL import Image, ImageDraw, ImageFont
import adafruit_rgb_display.ili9341 as ili9341
import sys

# Configuration for CS and DC pins:
CS_PIN = board.D14
DC_PIN = board.D23
RST_PIN = board.D24

# Setup SPI bus using hardware SPI:
spi = busio.SPI(clock=board.D11, MOSI=board.D10, MISO=board.D9)

# Create the ILI9341 display with rotation adjusted:
display = ili9341.ILI9341(spi, cs=digitalio.DigitalInOut(CS_PIN),
                          dc=digitalio.DigitalInOut(DC_PIN), rst=digitalio.DigitalInOut(RST_PIN), rotation=90)

# MongoDB URI (replace with your actual MongoDB URI)
uri = ""

# Connect to MongoDB using the URI
client = pymongo.MongoClient(uri)
db = client['taskdb']
tasks_collection = db['tasks']

# Create an image for drawing on the display
image = Image.new("RGB", (display.height, display.width))
draw = ImageDraw.Draw(image)

# Font setup (ensure you have a .ttf font file available, or use a basic font)
font = ImageFont.load_default()

# Function to display feedback or instructions on the screen
def display_feedback(message):
    # Clear the screen
    draw.rectangle((0, 0, display.height, display.width), outline=0, fill=(0, 0, 0))
    # Display the feedback message
    draw.text((10, 10), message, font=font, fill=(255, 255, 255))
    display.image(image)

# Function to display tasks on the upper half of the screen
def display_tasks():
    tasks = get_tasks()
    draw.rectangle((0, 0, display.height, display.width), outline=0, fill=(0, 0, 0))  # Clear the upper half
    y_offset = 10  # Starting position for task display

    # Convert the cursor to a list and check the length
    task_list = list(tasks)  # Convert Cursor to list
    if len(task_list) == 0:
        draw.text((10, y_offset), "No tasks found.", font=font, fill=(255, 255, 255))
    else:
        for task in task_list:
            task_name = task["name"]
            due_date = task["pDate"]
            is_past = task.get("isPast", False)

            # Set color based on isPast
            color = (255, 255, 255)  # Default white
            if is_past:
                color = (255, 0, 0)  # Red for past tasks

            draw.text((10, y_offset), f"{task_name} - {due_date}", font=font, fill=color)
            y_offset += 20  # Smaller spacing between tasks
            if y_offset > display.height // 2 - 30:  # Prevents tasks from overflowing
                break

    display.image(image)

# Function to get tasks from MongoDB
def get_tasks():
    current_time = datetime.now()
    tasks = tasks_collection.find({"isCompleted": False})  # Adjust filter based on "isPast"
    return tasks

# Function to create a task
def create_task(name, due_date):
    task = {
        "name": name,
        "date": datetime.strptime(due_date, "%m/%d/%y@%I:%M%p"),  # Convert string to datetime
        "isPast": False,
        "isCompleted": False,
        "timeLeft": "0d 18h 41m left",  # Static for now, can be updated later
        "pDate": f"Due: {datetime.strptime(due_date, '%m/%d/%y@%I:%M%p').strftime('%m/%d/%y @ %I:%M %p')}"
    }
    tasks_collection.insert_one(task)
    display_feedback(f"Task '{name}' created!")

# Function to complete a task in the database
def complete_task(task_name):
    tasks_collection.delete_one({"name": task_name})
    display_feedback(f"Task '{task_name}' marked as complete!")
    time.sleep(3)
    display_tasks()

# Function to handle input commands
def handle_input(command):
    parts = command.split()
    if len(parts) < 2:
        display_feedback("Invalid command!")
        return

    if parts[0] == "createTask":
        if len(parts) < 3:
            display_feedback("Invalid task creation command!")
            return
        task_name = parts[1]
        due_date = parts[2]
        create_task(task_name, due_date)
    elif parts[0] == "completeTask":
        task_name = parts[1]
        complete_task(task_name)
    else:
        display_feedback("Unknown command!")

# Main loop for user input via console
def main():
    print("Enter command: createTask [name] [due_date] or completeTask [task_name]")

    while True:
        display_tasks()
        # Get input from console
        command = input("Enter command: ")
        handle_input(command)
        time.sleep(0.1)  

if __name__ == "__main__":
    main()

Credits

Noah H
1 project • 0 followers
Contact

Comments

Please log in or sign up to comment.