Пример #1
0
void GetEEPRomData(uint8_t start, uint8_t stop, uint8_t * data) {
//	println_W("Getting eeprom page: ");p_int_W(start+DATASTART);print_W(" to ");p_int_W(stop+DATASTART);
	//WORD_VAL raw;
	uint8_t i = 0;
	if (start > stop) {
		//println_W("###ERROR eeprom");
		return;
	}
	int total = 0;
	do {
		total++;
		if (total > 5)
			return;
		LoadCorePacket(&downstreamPacketTemp);
		for (i = 0; i < (stop - start); i++) {
			//Writes over the old packet to avoid mis-reads
			downstreamPacketTemp.use.data[i] = data[i];
		}
		downstreamPacketTemp.use.head.Method = BOWLER_GET;
		downstreamPacketTemp.use.head.RPC = GetRPCValue(eepd);
		downstreamPacketTemp.use.data[0] = start + DATASTART;
		downstreamPacketTemp.use.data[1] = stop + DATASTART;
		downstreamPacketTemp.use.head.DataLegnth = 6;
		SendPacketToCoProc(&downstreamPacketTemp);
		buttonCheck(12);
	} while (downstreamPacketTemp.use.head.RPC != GetRPCValue(eepd));

	for (i = 0; i < (stop - start); i++) {
		data[i] = downstreamPacketTemp.use.data[i + 1];
	}
//	println_W("Rx<<");
//	printBowlerPacketDEBUG(&downstreamPacketTemp,WARN_PRINT);
}
Пример #2
0
bool handleButtons(float x, float y)
{
	if(translateButton.contains(x,y))
		transM = TRANS_TRANSLATION;
	else if(rotateButton.contains(x,y))
		transM = TRANS_ROTATION;
	else if(scaleButton.contains(x,y))
		transM = TRANS_SCALE;
	else if(XButton.contains(x,y))
		axisM = X_AXIS;
	else if(YButton.contains(x,y))
		axisM = Y_AXIS;
	else if(ZButton.contains(x,y))
		axisM = Z_AXIS;
	else if(tetraButton.contains(x,y))
		createShape(SHAPE_TETRAHEDRON);
	else if(cubeButton.contains(x,y))
		createShape(SHAPE_CUBE);
	else if(octButton.contains(x,y))
		createShape(SHAPE_OCTAHEDRON);
	else if(sphereButton.contains(x,y))
		createShape(SHAPE_SPHERE);
	else if(cylinderButton.contains(x,y))
		createShape(SHAPE_CYLINDER);
	else if(planeButton.contains(x,y))
		createShape(SHAPE_PLANE);
	else if(deleteButton.contains(x,y))
		deleteShape();
	else if(RButton.contains(x,y))
		colorM = COLOR_R;
	else if(GButton.contains(x,y))
		colorM = COLOR_G;
	else if(BButton.contains(x,y))
		colorM = COLOR_B;
	else if(loadButton.contains(x,y))
	{	
		idCounter = load("default.mcg", &listOfShapes);
		if(idCounter <= 0)
		{
			idCounter = 1;
			listOfShapes.clear();
		}
	}
	else if(saveButton.contains(x,y))
		save("default", listOfShapes);
	else return false;
	buttonCheck();
	return true;
}
Пример #3
0
void GetAllModes(BowlerPacket * pack) {
//	println_I("GetAllModes");
	int total = 0;
	do {
		total++;
		if (total > 5) {
//			println_I("Failed returning");
			return;
		}
		LoadGACM(pack);
		SendPacketToCoProc(pack);
		buttonCheck(14);
	} while (pack->use.head.RPC != GetRPCValue("gacm"));
	//print_I("..done");
}
Пример #4
0
void seedWithButtonPress() {
// turn on teensy LED
PORTD |= 0b01000000;
// Configure port B for the buttons....
DDRB |= 0b00001100;
// Wait for the button press on SW0 while iterating the seed
unsigned int seed = 0;
while (!((PINB >> PB0) & 1)) {
buttonCheck();
seed++;
}
// Seed the random number generator
srand(seed);
// What does dgbI do? where is it from?
// dbgI((int) seed, 10);
// turn off teensy LED
PORTD = 0b00000000;
}
Пример #5
0
void SetLockCode(char * code) {
	//WORD_VAL raw;
	uint8_t i = 0;
	LoadCorePacket(&downstreamPacketTemp);
	downstreamPacketTemp.use.head.Method = BOWLER_POST;
	downstreamPacketTemp.use.head.RPC = GetRPCValue(eepd);
	downstreamPacketTemp.use.data[0] = LOCKSTART;
	downstreamPacketTemp.use.data[1] = DATASTART;
	while (code[i] != '\0') {
		downstreamPacketTemp.use.data[2 + i] = code[i];
		i++;
		buttonCheck(8);
		if (i == LOCKSIZE)
			break;
	}
	downstreamPacketTemp.use.data[2 + i] = '\0';
	downstreamPacketTemp.use.head.DataLegnth = 6 + LOCKSIZE;
	SendPacketToCoProc(&downstreamPacketTemp);
}
Пример #6
0
boolean GetLockCode(char * code) {
	//WORD_VAL raw;
	uint8_t i = 0;
	LoadCorePacket(&downstreamPacketTemp);
	downstreamPacketTemp.use.head.Method = BOWLER_GET;
	downstreamPacketTemp.use.head.RPC = GetRPCValue(eepd);
	downstreamPacketTemp.use.data[0] = LOCKSTART;
	downstreamPacketTemp.use.data[1] = DATASTART;
	downstreamPacketTemp.use.head.DataLegnth = 6;
	SendPacketToCoProc(&downstreamPacketTemp);
	while (downstreamPacketTemp.use.data[i + 2] != '\0') {
		code[i] = downstreamPacketTemp.use.data[2 + i];
		i++;
		buttonCheck(10);
		if (i == LOCKSIZE)
			break;
	}
	code[i] = '\0';
	return isAscii(code);
}
Пример #7
0
void SetName(char * name) {
	//WORD_VAL raw;
	uint8_t i = 0;
	println_E(name);
	LoadCorePacket(&downstreamPacketTemp);
	downstreamPacketTemp.use.head.Method = BOWLER_POST;
	downstreamPacketTemp.use.head.RPC = GetRPCValue(eepd);
	downstreamPacketTemp.use.data[0] = NAMESTART;
	downstreamPacketTemp.use.data[1] = LOCKSTART;
	while (name[i] != '\0') {
		downstreamPacketTemp.use.data[2 + i] = name[i];
		i++;
		buttonCheck(9);
		if (i == NAMESIZE)
			break;
	}
	downstreamPacketTemp.use.data[2 + i] = '\0';
	downstreamPacketTemp.use.head.DataLegnth = 6 + i + 1;
	printPacket(&downstreamPacketTemp,ERROR_PRINT);
	SendPacketToCoProc(&downstreamPacketTemp);
}
Пример #8
0
void _SetChannelValueCoProc(uint8_t PIN, uint8_t state) {
	uint8_t retry = 0;
	do {
		if (retry > 0) {
			//println_E("#*#*SetChannelValueCoProc did not return RDY pin: ");p_int_E(PIN);print_E(" mode: ");printMode(GetChannelMode(PIN),ERROR_PRINT);
			//printPacket(&downstreamPacketTemp,ERROR_PRINT);
			return;
		}
		LoadCorePacket(&downstreamPacketTemp);
		downstreamPacketTemp.use.head.Method = BOWLER_POST;
		downstreamPacketTemp.use.head.RPC = GetRPCValue("schv");
		downstreamPacketTemp.use.data[0] = PIN;
		set32bit(&downstreamPacketTemp, state, 1);
		set32bit(&downstreamPacketTemp, 0, 5);			// zero ms
		downstreamPacketTemp.use.head.DataLegnth = 4 + 1 + 4 + 4;
		SendPacketToCoProc(&downstreamPacketTemp);
		setDataTableCurrentValue(PIN, state);
		retry++;
		buttonCheck(13);
	} while (downstreamPacketTemp.use.head.RPC != _RDY);

}
Пример #9
0
boolean GetName(char * name) {
	//WORD_VAL raw;
	uint8_t i = 0;
	LoadCorePacket(&downstreamPacketTemp);
	downstreamPacketTemp.use.head.Method = BOWLER_GET;
	downstreamPacketTemp.use.head.RPC = GetRPCValue(eepd);
	downstreamPacketTemp.use.data[0] = NAMESTART;
	downstreamPacketTemp.use.data[1] = LOCKSTART;
	downstreamPacketTemp.use.head.DataLegnth = 6;
	SendPacketToCoProc(&downstreamPacketTemp);

	printPacket(&downstreamPacketTemp,WARN_PRINT);

	while (downstreamPacketTemp.use.data[i] != '\0') {
		name[i] = downstreamPacketTemp.use.data[i];
		i++;
		buttonCheck(11);
		if (i == NAMESIZE)
			break;
	}
	name[i] = '\0';
	println_W(name);
	return isAscii(name);
}
Пример #10
0
// main
// ===============================================================
// ===============================================================
// ===============================================================
// ===============================================================
int main(void)
{
	uint8_t i1, i2, i3, i4;
	
	uint8_t bus_address;
	uint8_t rx_type;
	uint8_t k_row;

	k_row = 0;
	refresh_row = 0;
	scan_keypad = 0;
	key_read = key_write = 0;
	rx_read = rx_write = 0;
	
	intensity_index = 15;

	reply_type = _REPLY_KEYPAD;

	DDRB = 0xfe;	// input pin 1, outputs else
	DDRC = 0xff;	// output
	DDRD = 0xff;	// output

	bus_address = ADDR;	// initialized value, default 100 or from makefile
						// we will overwrite this with the value stored in eeprom

	// led refresh interrupt
	TCCR0A = 0;
	TCCR0B = (1<<CS02);// | (1<<CS00);	// prescale io/256
	TIMSK0 = (1 << OCIE0A) | (1 << OCIE0B);// | (1 << TOIE0);
	OCR0A = LED_REFRESH_RATE;
	OCR0B = ints[intensity_index];
	
	TCCR2A = 0;
	TCCR2B = (1<<CS22) | (1<<CS21);		// prescale io/256
	TIMSK2 = (1 << OCIE2A) | (1 << OCIE2B);// | (1 << OCIE0B);// | (1 << TOIE0);
	OCR2A = d1ints[intensity_index];
	OCR2B = d2ints[intensity_index];
	
	// keypad interrupt
	TCCR1A = 0;
	TCCR1B = (1<<CS12) | (1<<CS10);
	TIMSK1 = (1<<OCIE1A);
	OCR1A = KEYPAD_REFRESH_RATE;

	buttonInit();

	sei();

	twi_init();
	twi_setAddress(bus_address);
	twi_attachSlaveRxEvent(processRx);
	twi_attachSlaveTxEvent(processTx);


	// loop
	// ===============================================================
	// ===============================================================
	while(1) {
		if(scan_keypad) {
			//for(k_row=0;k_row<8;k_row++) {
				button_last[k_row] = button_current[k_row];
				//_delay_us(5);
				PORTB |= B2_KCOL_SH;
				_delay_us(5);

				for(i1=0;i1<8;i1++) {
					i2 = !(PINB & B0_KCOL_Q);
					PORTB |= B1_KCOL_CLK;
					//_delay_us(2);
					PORTB &= ~B1_KCOL_CLK;
					if (i2)
			            button_current[k_row] |= (1 << i1);
			        else
			            button_current[k_row] &= ~(1 << i1);
					buttonCheck(k_row,i1);
					if(button_event[k_row] & (1<<i1)) {
						button_event[k_row] &= ~(1<<i1);
						key_changes[key_write]= ((!i2)<<7) + ((7-i1)*8) + (k_row);
						key_write++;
						key_write %= 128;
					}
				}

			PORTB &= ~B2_KCOL_SH;
			
			k_row++;
			k_row %= 8;
			PORTC = k_row;//(PINC | 0x07) | k_row;

			scan_keypad = 0;
		}

		if(rx_read != rx_write) {
			rx_type = rx[rx_read];

			if(rx_type == _QUERY) {
				i1 = 0;
			} else if(rx_type == _SLEEP) {
				TIMSK0 = 0;
				TIMSK1 = 0;
				TIMSK2 = 0;
				PORTD = 0;
			} else if(rx_type == _WAKE) {
				TIMSK0 = (1 << OCIE0A) | (1 << OCIE0B);
				TIMSK1 = (1<<OCIE1A);
			} else if(rx_type == _KEYPAD_REQUEST) {
				reply_type = _REPLY_KEYPAD;
			} else if(rx_type == _LED_SET0) {
				// _LED_SET0 //////////////////////////////////////////////
				i3 = rx[(rx_read + 1)%128];
				i1 = i3 & 0x0f;
				i2 = i3 >> 4;
				lights[i1] &= ~(1<<i2);
			} else if(rx_type == _LED_SET1) {
Пример #11
0
void fail(){
	while(true) {
		SetColor(1,1,0);
		buttonCheck(1);
	}
}
Пример #12
0
int main(int argc, char *argv[]) {
	int button, buttonCount;
 
#if defined(DEBUG)
    int daemonize = 0;
#else
    int daemonize = 1;
#endif
 
    // Setup signal handling before we start
    signal(SIGHUP, signal_handler);
    signal(SIGTERM, signal_handler);
    signal(SIGINT, signal_handler);
    signal(SIGQUIT, signal_handler);
 
    int c;
    while( (c = getopt(argc, argv, "nh|help")) != -1) {
        switch(c){
            case 'h':
                PrintUsage(argc, argv);
                exit(0);
                break;
            case 'n':
                daemonize = 0;
                break;
            default:
                PrintUsage(argc, argv);
                exit(0);
                break;
        }
    }
 
    syslog(LOG_INFO, "%s daemon starting up", DAEMON_NAME);
 
    // Setup syslog logging - see SETLOGMASK(3)
#if defined(DEBUG)
    setlogmask(LOG_UPTO(LOG_DEBUG));
    openlog(DAEMON_NAME, LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER);
#else
    setlogmask(LOG_UPTO(LOG_INFO));
    openlog(DAEMON_NAME, LOG_CONS, LOG_USER);
#endif
 
    /* Our process ID and Session ID */
    pid_t pid, sid;
 
    if (daemonize) {
        syslog(LOG_INFO, "starting the daemonizing process");
 
        /* Fork off the parent process */
        pid = fork();
        if (pid < 0) {
            exit(EXIT_FAILURE);
        }
        /* If we got a good PID, then
           we can exit the parent process. */
        if (pid > 0) {
            exit(EXIT_SUCCESS);
        }
 
        /* Change the file mode mask */
        umask(0);
 
        /* Create a new SID for the child process */
        sid = setsid();
        if (sid < 0) {
            /* Log the failure */
            exit(EXIT_FAILURE);
        }
 
        /* Change the current working directory */
        if ((chdir("/")) < 0) {
            /* Log the failure */
            exit(EXIT_FAILURE);
        }
 
        /* Close out the standard file descriptors */
        close(STDIN_FILENO);
        close(STDOUT_FILENO);
        close(STDERR_FILENO);
    }
 
    //****************************************************
    // TODO: Insert core of your daemon processing here
    //****************************************************

    while(1){
	button = buttonCheck();
	if(button == 0){
		//Debounce protector
		while(button == 0){ button = buttonCheck();}
		if(buttonCount == 0) buttonCount = 1;
		else buttonCount = 0;


		if(buttonCount == 1){
			//Kill the code
			system("kill -9 'cat /var/run/studentCode.pid'");
			//Print program termination message.
			system("wall /programKillMsg");
			setLED(4);
		}

		//Reset the robot
		fullReset();

		if( buttonCount == 0){
			//Print message
			system("wall /resetMessage");

			//Run the code again.
			system("/RUNCODE/runMe");
		}

		usleep(500000);		
	}
    }


    syslog(LOG_INFO, "%s daemon exiting", DAEMON_NAME);
 
    //****************************************************
    // TODO: Free any allocated resources before exiting
    //****************************************************
 
    exit(0);
}
Пример #13
0
Файл: 40h.c Проект: monome/40h
int main(void)
{
    uint8 i1, i2, i3;
    uint8 rx0, rx1, rx_pos, rx_roll;

    uint8 led_data[8];

    t_message serial_in;
    t_message serial_out;

    io_pin_t ioRXF; ioRXF.port = B; ioRXF.pin = 1;
    io_pin_t ioRD; ioRD.port = B; ioRD.pin = 2;
    io_pin_t ioWR; ioWR.port = B; ioWR.pin = 3;

    io_pin_t ioPWREN; ioPWREN.port = C; ioPWREN.pin = 0;

    io_pin_t ioLOAD; ioLOAD.port = C; ioLOAD.pin = 6;	       // 164 SH/LD
    io_pin_t ioDATA; ioDATA.port = C; ioDATA.pin = 7;	       // 165 QH
    io_pin_t ioCLKSEL; ioCLKSEL.port = A; ioCLKSEL.pin = 6;	   // 164 CLK
    io_pin_t ioCLKIN; ioCLKIN.port = A; ioCLKIN.pin = 7;
    io_pin_t ioSET; ioSET.port = A; ioSET.pin = 5;	           // 164 A

    uint8 firstRun = true;

    DDRB &= ~(1 << ioRXF.pin); PORTB |= (1 << ioRXF.pin);
    DDRB |= (1 << ioRD.pin); PORTB |= (1 << ioRD.pin);
    DDRB |= (1 << ioWR.pin); PORTB |= (1 << ioWR.pin);

    DDRC &= ~(1 << ioPWREN.pin); PORTC |= (1 << ioPWREN.pin);

    DDRC |= (1 << ioLOAD.pin); PORTC |= (1 << ioLOAD.pin);
    DDRC &= ~(1 << ioDATA.pin); PORTC |= (1 << ioDATA.pin);
    DDRA |= (1 << ioCLKSEL.pin); PORTA |= (1 << ioCLKSEL.pin);
    DDRA |= (1 << ioCLKIN.pin); PORTA |= (1 << ioCLKIN.pin);
    DDRA |= (1 << ioSET.pin); PORTA |= (1 << ioSET.pin);

    output_pin(ioSET, 1);	             // clear out row selector
    for (i1 = 0; i1 < 8; i1++) {
        led_data[i1] = 0;
        output_pin(ioCLKSEL, 1);         // clear out row selector
        output_pin(ioCLKSEL, 0);
    }

    buttonInit();

    rx0 = 0;
    rx_pos = 0;
    rx_roll = 0;

    sei();

    // init SPI
    SPCR = (1 << SPE) | (1 << MSTR) | (SPI2X);
    DDRB |= (1 << PB5)|(1 << PB4)|(1 << PB7);
    
    spi_led(11, 7);				       // set scan limit to full range
    spi_led(10, 15);				   // set to max intensity
    for(i1 = 1; i1 < 9; i1++) {
        spi_led(i1, i1);	           // print startup pattern 
    }
    spi_led(12, 1);				       // come out of shutdown mode 
    spi_led(15, 0);				       // test mode off
   
    for(i1=0;i1<64;) {
        spi_led(10, (64-i1)/4);				   // set to max intensity
    	if(!input_pin(ioPWREN)) i1++;         // wait for USB enumeration, to prevent auto-sleep
    	_delay_ms(8);
    }

    for(i1 = 1; i1 < 9; i1++) 
        spi_led(i1, 0);	           // clear led data
 
    spi_led(10, 15);				   // set to max intensity

    // ******** main loop ********
    while (1) {
    	// read incoming serial **********************************************
    	PORTD = 0;			          // setup PORTD for input
        DDRD = 0;			          // input w/ tristate

        while(!(input_pin(ioRXF))) {
            output_pin(ioRD, 0);
            if (rx_pos == 0) {
                rx0 = PIND;
                rx_pos = 1;
            }
            else {
                uint8 msg_type, msg_data0;
                
                rx1 = PIND;
                rx_pos = 0;


                // *********** process packet here 
                serial_in.data0 = rx0;
                serial_in.data1 = rx1;
                
                msg_type = messageGetType(serial_in);

                switch (msg_type) {
                case kMessageTypeLedTest:
                    msg_data0 = messageGetLedTestState(serial_in);
                    spi_led(15, msg_data0);
                    break;

                case kMessageTypeLedIntensity:
                    msg_data0 = messageGetLedIntensity(serial_in);
                    spi_led(10, msg_data0);
                    break;

                case kMessageTypeLedStateChange:

                    i1 = messageGetLedX(serial_in);
                    i2 = messageGetLedY(serial_in);

                    if(messageGetLedState(serial_in) == 0)
                        led_data[i2] &= ~(1 << i1);
                    else
                        led_data[i2] |= (1 << i1);

                    spi_led(i2 + 1, led_data[i2]);
                    break;

                case kMessageTypeAdcEnable:
                    if (messageGetAdcEnableState(serial_in))
                        enableAdc(messageGetAdcEnablePort(serial_in));
                    else
                        disableAdc(messageGetAdcEnablePort(serial_in));
                    break;

                case kMessageTypeShutdown:
                    msg_data0 = messageGetShutdownState(serial_in);
                    spi_led(12, msg_data0);
                    break;

                case kMessageTypeLedSetRow:
                    if (firstRun == true) {
                        for (i1 = 0; i1 < 8; i1++) {
                            led_data[i1] = 0;
                            spi_led(i1 + 1, led_data[i1]);
                        }
                        
                        firstRun = false;
                    }

                    i1 = (messageGetLedRowIndex(serial_in) & 0x7); // mask this value so we don't write to an invalid address.
                                                                   // this will have to change for 100h.
                    i2 = messageGetLedRowState(serial_in);
                    
                    led_data[i1] = i2;
                    spi_led(i1 + 1, led_data[i1]);
                    break;

                case kMessageTypeLedSetColumn:
                    if (firstRun == true) {
                        for (i1 = 0; i1 < 8; i1++) {
                            led_data[i1] = 0;
                            spi_led(i1 + 1, led_data[i1]);
                        }
                        
                        firstRun = false;
                    }

                    i1 = (messageGetLedColumnIndex(serial_in) & 0x7);
                    i2 = messageGetLedColumnState(serial_in);

                    for (i3 = 0; i3 < 8; i3++) {
                        if (i2 & (1 << i3))
                            led_data[i3] |= 1 << i1;
                        else
                            led_data[i3] &= ~(1 << i1);

                        spi_led(i3 + 1, led_data[i3]);
                    }

                    break;
                }
            }

            output_pin(ioRD,1); 
	    
        }

        // check if there's a stray single packet
        if (rx_pos == 1)
            rx_roll++;
        else
            rx_roll = 0; 		// this can be moved to after the packet is processed
	
        // if single packet is "lost" trash it after a chance to get a match
        if(rx_roll > 80) {
            rx_roll = 0;
            rx_pos = 0;
        }

	
        // output serial data **********************************************
        PORTD = 0;			// setup PORTD for output
        DDRD = 0xFF;


        // button check proto

        output_pin(ioSET,0);
        for (i1 = 0; i1 < 8; i1++) {
            output_pin(ioCLKSEL, 1);
            output_pin(ioCLKSEL, 0);
            output_pin(ioSET, 1);
            
	
            button_last[i1] = button_current[i1];

            output_pin(ioLOAD, 0);		// set 165 to load
            output_pin(ioLOAD, 1);		// 165 shift

            for (i2=0; i2 < 8; i2++) {
                i3 = input_pin(ioDATA);
                i3 = (i3 == 0);
                if (i3) 
                    button_current[i1] |= (1 << i2);
                else
                    button_current[i1] &= ~(1 << i2);

                buttonCheck(i1, i2);

                if (button_event[i1] & (1 << i2)) {
                    button_event[i1] &= ~(1 << i2);

                    messagePackButtonPress(&serial_out, (button_state[i1] & (1 << i2)) ? kButtonDownEvent : kButtonUpEvent, i2, i1);
                    
                    output_pin(ioWR, 1);
                    PORTD = serial_out.data0; 
                    output_pin(ioWR, 0);
                    
                    output_pin(ioWR, 1);
                    PORTD = serial_out.data1;
                    output_pin(ioWR, 0); 
                } 
                
                output_pin(ioCLKIN, 1);
                output_pin(ioCLKIN, 0);
            }
        }

        // ---------------------------------- ADC -------------------------


        for (i1 = 0; i1 < kAdcFilterNumAdcs; i1++) { 
            if (gAdcFilters[i1].dirty == true) {
                messagePackAdcVal(&serial_out, i1, gAdcFilters[i1].value);

                output_pin(ioWR, 1);
                PORTD = serial_out.data0; 
                output_pin(ioWR, 0);
                
                output_pin(ioWR, 1);
                PORTD = serial_out.data1;
                output_pin(ioWR, 0); 
                
                gAdcFilters[i1].dirty = false;
            }
        } 
    }
    
    return 0;
}
Пример #14
0
int main(int argc, char *argv[]) {
	int button, buttonCount;
 
#if defined(DEBUG)
    int daemonize = 0;
#else
    int daemonize = 1;
#endif
 
    // Setup signal handling before we start
    signal(SIGHUP, signal_handler);
    signal(SIGTERM, signal_handler);
    signal(SIGINT, signal_handler);
    signal(SIGQUIT, signal_handler);
 
    int c;
    while( (c = getopt(argc, argv, "nh|help")) != -1) {
        switch(c){
            case 'h':
                PrintUsage(argc, argv);
                exit(0);
                break;
            case 'n':
                daemonize = 0;
                break;
            default:
                PrintUsage(argc, argv);
                exit(0);
                break;
        }
    }
 
    syslog(LOG_INFO, "%s daemon starting up", DAEMON_NAME);
 
    // Setup syslog logging - see SETLOGMASK(3)
#if defined(DEBUG)
    setlogmask(LOG_UPTO(LOG_DEBUG));
    openlog(DAEMON_NAME, LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER);
#else
    setlogmask(LOG_UPTO(LOG_INFO));
    openlog(DAEMON_NAME, LOG_CONS, LOG_USER);
#endif
 
    /* Our process ID and Session ID */
    pid_t pid, sid;
 
    if (daemonize) {
        syslog(LOG_INFO, "starting the daemonizing process");
 
        /* Fork off the parent process */
        pid = fork();
        if (pid < 0) {
            exit(EXIT_FAILURE);
        }
        /* If we got a good PID, then
           we can exit the parent process. */
        if (pid > 0) {
            exit(EXIT_SUCCESS);
        }
 
        /* Change the file mode mask */
        umask(0);
 
        /* Create a new SID for the child process */
        sid = setsid();
        if (sid < 0) {
            /* Log the failure */
            exit(EXIT_FAILURE);
        }
 
        /* Change the current working directory */
        if ((chdir("/")) < 0) {
            /* Log the failure */
            exit(EXIT_FAILURE);
        }
 
        /* Close out the standard file descriptors */
        close(STDIN_FILENO);
        close(STDOUT_FILENO);
        close(STDERR_FILENO);
    }
 
    //****************************************************
    // TODO: Insert core of your daemon processing here
    //****************************************************

    while(button != 0){
	button = buttonCheck();
	if(button == 0){
		while(button == 0){ button = buttonCheck(); buttonCount++;}
		//KILL runme process if it doesnt kill itself.
		if(buttonCount < 30000) system("/RUNCODE/runMe");
		else system("shutdown -r now");
		break;
	}
    }


    syslog(LOG_INFO, "%s daemon exiting", DAEMON_NAME);
 
    //****************************************************
    // TODO: Free any allocated resources before exiting
    //****************************************************
 
    exit(0);
}