Understanding Grove PIR Motion Sensor Detection to Use in Other Code

Hi, I’m trying to understand how to use the Grove PIR Motion Sensor. I’m fairly new to hardware & sensor programming, so apologies if some of these questions are pretty basic.

I was able to set up my Raspberry Pi to use the Grove PIR Motion sensor using the Grove Base HAT for RasPi and following this guide:
https://wiki.seeedstudio.com/Grove-PIR_Motion_Sensor/

It’s able to detect motion fairly well, but I’m unable to understand the following script to see where the code is actually getting the input from the GPIO port/sensor, and determining whether that input represents motion being detected or not:

import time
from grove.gpio import GPIO

class GroveMiniPIRMotionSensor(GPIO):
def init(self, pin):
super(GroveMiniPIRMotionSensor, self).init(pin, GPIO.IN)
self._on_detect = None

@property
def on_detect(self):
    return self._on_detect

@on_detect.setter
def on_detect(self, callback):
    if not callable(callback):
        return

    if self.on_event is None:
        self.on_event = self._handle_event

    self._on_detect = callback

def _handle_event(self, pin, value):
    if value:
        if callable(self._on_detect):
            self._on_detect()

Grove = GroveMiniPIRMotionSensor

def main():
import sys

if len(sys.argv) < 2:
    print('Usage: {} pin'.format(sys.argv[0]))
    sys.exit(1)

pir = GroveMiniPIRMotionSensor(int(sys.argv[1]))

def callback():
    print('Motion detected.')

pir.on_detect = callback

while True:
    time.sleep(1)

if name == ‘main’:
main()

So, I was wondering if one of you smart people could help understand exactly what’s going on in this script, and where the sensor is actually detecting motion or not.

The reason I’d like to do so is so that I may access such input/motion detection and use it to trigger part of another program I’ve created. It’s a computer vision program with a video feed that I want to restart whenever motion is detected, so any help figuring out how to do that as well would be much appreciated. Let me know what you guys think!

Thanks!

Sam

This is a piece of Python code, our sample code generally does not have specific principles and processes, just like here, it is the definition of some inherited class methods and functions. But its basic principle is to obtain the high and low level of the sensor to determine whether someone is moving.