Skip to content

Instantly share code, notes, and snippets.

@loveandsheep
Last active August 29, 2015 14:17
Show Gist options
  • Save loveandsheep/1db4e29e45be55f3056b to your computer and use it in GitHub Desktop.
Save loveandsheep/1db4e29e45be55f3056b to your computer and use it in GitHub Desktop.
raspberry pi -> mac shutdown
#include "main.h"
using namespace std;
int main(){
atexit(onExit);
gpio_init();
phase = PHASE_OFF;
gpio_configure(PIN_PWR, GPIO_OUTPUT);
gpio_configure(PIN_STATUS, GPIO_OUTPUT);
gpio_configure(PIN_BUTTON_ON, GPIO_INPUT);
gpio_configure(PIN_BUTTON_OFF, GPIO_INPUT);
gpio_configure_pull(PIN_BUTTON_ON, GPIO_PULLUP);
gpio_configure_pull(PIN_BUTTON_OFF, GPIO_PULLUP);
gpio_set(PIN_PWR);
gpio_clear(PIN_AC_SSR);
while (1){mainloop();}
return 0;
}
void mainloop(){
time_t currentTime;
if (phase == PHASE_OFF)
{
if (gpio_read(PIN_BUTTON_ON) == 0){
//Turn on Mac.
cout << "Start turning on Mac..." << endl;
phase = PHASE_BOOTING;
gpio_set(PIN_AC_SSR);
time(&stockTime);
}
}
else if (phase == PHASE_BOOTING)
{
//wait for Mac boot finished.
time(&currentTime);
if (difftime(currentTime, stockTime) > BOOT_WAIT_TIME){
cout << "Boot standby mode finished." << endl;
phase = PHASE_ON;
}
}
else if (phase == PHASE_ON)
{
if (gpio_read(PIN_BUTTON_OFF) == 0){
cout << "call Shutdown reqest..." << endl;
//call Shell script
system(". powerOff.sh");
system("autossh 192.168.124.10 usrID usrPass");
phase = PHASE_COOLING;
time(&stockTime);
}
}
else if (phase == PHASE_COOLING)
{
//wait for Mac shutdown finished.
time(&currentTime);
if (difftime(currentTime, stockTime) > COOL_WAIT_TIME){
cout << "Cooling finished." << endl;
phase = PHASE_OFF;
gpio_clear(PIN_AC_SSR);
}
}
}
void onExit(){
gpio_clear(PIN_PWR);
gpio_clear(PIN_STATUS);
cout << "exit powerController." << endl;
}
#include <iostream>
#include <stdio.h>
#include <time.h>
#include "raspGPIO.h"
#define PHASE_BOOTING 0
#define PHASE_ON 1
#define PHASE_COOLING 2
#define PHASE_OFF 3
#define PIN_PWR 3
#define PIN_STATUS 17
#define PIN_BUTTON_OFF 27
#define PIN_BUTTON_ON 27
#define PIN_AC_SSR 2
#define BOOT_WAIT_TIME 300.00
#define COOL_WAIT_TIME 120.00
void mainloop();
void onExit();
int main();
int phase;
time_t stockTime;
#!/bin/sh
auto_ssh(){
host=$1
id=$2
pass=$3
expect -c "
set timeout 10
spawn ssh -t ${id}@${host} sudo shutdown -h -u now
expect \"Are you sure you want to continue connecting (yes/no)?\" {
send \"yes\n\"
expect \"Password:\"
send \"${pass}\n\"
expect \"Password:\"
send \"${pass}\n\"
expect \"Password:\"
send \"${pass}\n\"
} \"Password:\" {
send \"${pass}\n\"
expect \"Password:\"
send \"${pass}\n\"
expect \"Password:\"
send \"${pass}\n\"
}
"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment