Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Combining several py files
#1
Hi,

I’m building an alarm clock based on the Raspberry Zero board, a LED display (https://learn.adafruit.com/adafruit-led-...t-backpack) and a vibration motion (as a switch) - no buttons used.

The local time comes from the board’s OS and the alarm time is set via a local web page. The latter stores 4 variables in a text file: hour, minutes, alarm ON/OFF and if activated on weekends (YES/NO).

My mainfile.py starts at power up and trough a ‘While(True)’ loop displays the actual time.

I’m new to Python so I’m looking for some directions and not necessary a code.

Having said that, my question is how do I interface with the mainfile.py the following files?

beep.py – generates a beep in a loop for two minutes unless the vibration sensor is activated.
alarm.py – reads the text file and compares the actual time with the one set from the web page if there’s a match, then runs beep.py.

I imagine that all the files need to go in the While loop or do they go through a different thread?

TIA
Reply
#2
you can add a a program named zero.py to run all the other modules.
this is an example of how this is done (replacing with your modules, of course)
zero.py (Note: import order is not important here, nor is order of execution:
import program3
import program1
import program2


def zero():
    print('Starting zero\n')
    program3.p3()

    program1.p1()

    program2.function2()

if __name__ == '__main__':
    zero()
program1.py:
def p1 ():
    print("Hello, I am program 1")

if __name__ == '__main__':
    p1()
program2.py:
# program2 has functions

def function1():
    print('I am function 1')

def function2():
    print('I am function 2')

if __name__ == '__main__':
    function1()
    function2()
program3.py:
def p3 ():
    print("Hello, I am program 3")

if __name__ == '__main__':
    p3()
running from command line:
python zero.py
output:
Output:
Starting zero Hello, I am program 3 Hello, I am program 1 I am function 2
Reply
#3
@Larz60+ Thank you for your time and for getting back to me.

I understood your approach and will try it soon but still puzzled about the looping part of the program. Somehow the zero.py needs to have a loop so that the display can be updated and the time alarm can be checked or should I tell cron to run zero.py every x seconds?
Reply
#4
I can't really help without knowing the intricate details of all of the hardware and software.
But I do know that if there is a timer involved, it creates an event (interrupt) when the 'alarm' triggers. You need to intercept this internet, not with a loop, but with a software routine that is triggered by the event. This is usually done by binding the software entry point with the device creating the event. A bit of searching turns up the following:
Pointing to stack overload: https://stackoverflow.com/questions/1614...-in-python
Reply
#5
RaspberryPi Zero is just a minimal computer (super minimal, fits in the palm of your hand). I think most tutorials recommend installing Raspbian on it, which is based on Debian. https://www.raspberrypi.org/downloads/raspbian/

You could certainly use a while True loop, and sleep(1) or something. Does the clock display seconds? How accurate do you want it to be? It'd probably be easier if you had it just run through cron, maybe as several different scripts (one which read the config and started an alert if needed, and a separate one which did nothing but update the display).

I'm not a huge fan of long running tasks, but if you'd rather use a while loop and avoid cron, that's totally fine. Just make sure you close your files after opening them, and call sleep() now and then so you don't update the display a few hundred times every second.
Reply
#6
The clock does not show seconds but they're used by the routine to blink the colon. The code below works but uses a While loop wich difficults the implementation of other routines.

#!/usr/bin/python

import time
import datetime

from Adafruit_LED_Backpack import SevenSegment

# ===========================================================================
# Clock Example
# ===========================================================================
segment = SevenSegment.SevenSegment(address=0x70)

# Initialize the display. Must be called once before using the display.
segment.begin()

# print "Press CTRL+Z to exit"

# Continually update the time on a 4 char, 7-segment display
while(True):
  now = datetime.datetime.now()
  hour = now.hour
  minute = now.minute
  second = now.second
  al_hour, al_minutes, al, we

  segment.clear()
  # Set hours
  segment.set_digit(0, int(hour / 10))     # Tens
  segment.set_digit(1, hour % 10)          # Ones
  # Set minutes
  segment.set_digit(2, int(minute / 10))   # Tens
  segment.set_digit(3, minute % 10)        # Ones
  # Toggle colon
  segment.set_colon(second % 2)            # Toggle colon at 1Hz

  # Write the display buffer to the hardware.  This must be called to
  # update the actual display LEDs.
  segment.write_display()
  
  #get alarm settings 

  # Wait a quarter second (less than 1 second to prevent colon blinking getting$
  time.sleep(0.25)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Missing graph after combining xls files Kishore01 0 1,337 Apr-15-2020, 10:51 AM
Last Post: Kishore01

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020