0

I saw many guides online to run a script at startup but all I found so far runs after logged in to the pi. In my case, I would like to run a script right after the splash screen. I saw some guides online on how to customise the splash screen but it's not exactly what I need. I'm not sure how to adapt my code and I'm afraid that if there's error in the code, my pi won't boot. Here's the script that I want to run. It plays sound through an active buzzer:

import sys
import RPi.GPIO as GPIO
import time
# Set trigger PIN according with your cabling
signalPIN = 26

# Set PIN to output
GPIO.setmode(GPIO.BCM)
GPIO.setup(signalPIN,GPIO.OUT)
class soundGen:
    
    def soundFunc(self,function):
        default = "Not found"
        return getattr(self, 'case_' + str(function), lambda: default)()
    
    def case_1(self):#Startup boot      
        return [1,0,1,0],[0.2,0.1,0.5,0]
        
    def soundPLayer(self, select):
        pin, gap = self.soundFunc(select)
        i=0
        for n in pin:
            GPIO.output(signalPIN,pin[i])
            time.sleep(gap[i])
            i=i+1
ss = soundGen()
function = 4
# cleanup will free PINS and exit will terminate code execution
ss.soundPLayer(function)

GPIO.cleanup()
sys.exit()
4
  • 1
    I would like to run a script at the rainbow splash screen - impossible - I don't think the CPU is even initialised yet at that stage, let alone enough of the operating system to run python – Jaromanda X Mar 17 at 0:17
  • @JaromandaX This one here shows how to customise the splash screen: link. I figured it would be possible to just adapt the code above? – zaraku27 Mar 17 at 0:28
  • you said "rainbow splash screen" - I assumed you meant the "rainbow" that appears when you first turn on the pi, not some splash screen that is added some time later by playing a video using omxplayer - just add audio to the video you play – Jaromanda X Mar 17 at 0:35
  • @JaromandaX I was under the impression that they replaced that rainbow splash screen. So I thought i would be possible to play sound instead at that point in time. I'll modify my question. – zaraku27 Mar 17 at 0:46
2

Realistically, the earliest moment for the boot sound is right after the SystemD sound target:

[Unit]
Description=Boot Sound
Requires=sound.target
After=sound.target

[Service]
Type=oneshot
RemainAfterExit=no
ExecStart=/usr/bin/aplay /boot/sound.wav

[Install]
WantedBy=default.target

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.