Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 161f864fe3
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 221 lines (188 sloc) 4.989 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
/*
* Copyright (C) 2112 Garrett Brown <gbruin@ucla.edu>
*
* This Program is free software; you can redistribute it and/or modify it
* under the terms of the Modified BSD License.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the organization nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* This Program is distributed AS IS in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

#include "Upstart.h"
#include "GPIO.h"
#include "BeagleBoardAddressBook.h"
#include "ParamServer.h"

#include <unistd.h> // for usleep
#include <string>

#include <iostream>

#define SHUTDOWN_COMMAND "./system_shutdown" // in current bin directory
#define BUTTON_TIMEOUT 5000000UL // 5.0s
//#define POWER_TIMEOUT 5000000UL // 5.0s

#define RED_FADE 1000

using namespace std;

int main(int argc, char **argv)
{
Upstart justdoit;
justdoit.Main();
}
void Upstart::Main()
{
// Wait 10 seconds
usleep(10000000L);

GPIO arduino1(ARDUINO_BRIDGE1);
arduino1.Open();
arduino1.SetDirection(GPIO::OUT, 0);

// Wait 20 seconds
usleep(20000000L);

// Connect to the Arduino
arduino.Open(ARDUINO_PORT);

// Wait 2 seconds
usleep(2000000L);

GPIO arduino2(ARDUINO_BRIDGE2);
arduino2.Open();
arduino2.SetDirection(GPIO::OUT, 0);

// Rev up the threads
m_bRunning = true;

boost::thread tempGreen(boost::bind(&Upstart::GreenThreadRun, this));
m_greenThread.swap(tempGreen);

//boost::thread tempYellow(boost::bind(&Upstart::YellowThreadRun, this));
//m_yellowThread.swap(tempYellow);

boost::thread tempRed(boost::bind(&Upstart::RedThreadRun, this));
m_redThread.swap(tempRed);

//boost::thread tempThumbwheel(boost::bind(&Upstart::ThumbwheelThreadRun, this));
//m_thumbwheelThread.swap(tempThumbwheel);

//boost::thread tempIMU(boost::bind(&Upstart::IMUThreadRun, this));
//m_IMUThread.swap(tempIMU);

// Run until all threads have completed
m_greenThread.join();
m_redThread.join();

GPIO arduino3(ARDUINO_BRIDGE3);
arduino3.Open();
arduino3.SetDirection(GPIO::OUT, 0);
}

void Upstart::GreenThreadRun()
{
GPIO gpio(BUTTON_GREEN);
gpio.Open();

gpio.SetDirection(GPIO::IN);
gpio.SetEdge(GPIO::BOTH);

/*
ParamServer::BatteryMonitor bm;
string fsm = bm.GetString();
arduino.DestroyFSM(fsm); // Make sure FSM isn't running before we start
*/

GPIO arduino4(ARDUINO_BRIDGE4);
arduino4.Open();
arduino4.SetDirection(GPIO::OUT, 0);

enum STATE
{
ENABLED = 0,
DISABLED = 1
} state = DISABLED;

unsigned long duration = 0;
unsigned int post_value = 0;

while (m_bRunning)
{
try
{
if (gpio.Poll(BUTTON_TIMEOUT, duration, true, post_value))
{
// No timeout
if (post_value == 0)
{
// Pressed
if (state == DISABLED)
{
//arduino.CreateFSM(fsm);
arduino4.SetValue(1);
state = ENABLED;
}
else
{
//arduino.DestroyFSM(fsm);
arduino4.SetValue(0);
state = DISABLED;
}
}
else
{
// Depressed
}
}
else
{
// Timeout
}
}
catch (const GPIO::Exception &e)
{
m_bRunning = false;
}
}
//arduino.DestroyFSM(fsm);
arduino4.SetValue(1);
}

void Upstart::RedThreadRun()
{
GPIO gpio(BUTTON_RED);
gpio.Open();

gpio.SetDirection(GPIO::IN);
gpio.SetEdge(GPIO::BOTH);

ParamServer::ChristmasTree xmastree;
string strXmastree = xmastree.GetString();
ParamServer::Fade fade;
fade.SetPin(LED_EMERGENCY);
fade.SetPeriod(RED_FADE);
fade.SetDelay(50);
fade.SetCurve(1);
string strFade = fade.GetString();
arduino.DestroyFSM(strFade); // Make sure FSM isn't running before we start

unsigned long duration = 0;
unsigned int post_value = 0;

while (m_bRunning)
{
try
{
if (gpio.Poll(BUTTON_TIMEOUT, duration, true, post_value))
{
// No timeout
if (post_value == 0)
arduino.CreateFSM(strFade);
else
arduino.DestroyFSM(strFade);
}
else
{
// Timed out
if (gpio.GetValue() == 0)
{
// Timed out on press waiting for depress
m_bRunning = false;
arduino.DestroyFSM(strFade);
int result = system(SHUTDOWN_COMMAND);
(void)result;
}
else
{
// Timed out on depress waiting for press
}
}
}
catch (const GPIO::Exception &e)
{
m_bRunning = false;
}
}
arduino.DestroyFSM(strFade);
}
Something went wrong with that request. Please try again.