Example #1
0
File: main.c Project: csbaltar/Lab4
int main(void) {
   WDTCTL = WDTPW | WDTHOLD;	// Stop watchdog timer
	
   //initialize LCD
    initSPI();
    LCDinit();
    LCDclear();

   //Write to LCD


    while(1)
    {
    	write(Message1, 8);
    	Line2();
    	write(Message2,8);
    	_delay_cycles(100000);

    	scroll(Message1, MessageSize1);
    	scroll(Message2, MessageSize2);

    	LCDclear();
    }

	return 0;
}
Example #2
0
int main(void) {
    WDTCTL = WDTPW | WDTHOLD;	// Stop watchdog timer

    initSPI();

    LCDinit();

    LCDclear();

    unsigned char player = initPlayer();

    init_timer();
    init_buttons();
    __enable_interrupt();
   	printPlayer(player);
    while(1)
    {

    	player = movementCheck(player);
    	if(LOSE == 1){
    		LCDclear();
    		print("GAME");
    		secondLine();
    		print("OVER");
    		firstLine();
    		GAMEOVER = 1;
    		waitForP1ButtonRelease(BIT1|BIT2|BIT3|BIT4);
    		debounce();
    	}
    	if(didPlayerWin(player)){
    		LCDclear();
    		print("YOU");
    		secondLine();
    		print("WON");
    		firstLine();
    		GAMEOVER = 1;
    		waitForP1ButtonRelease(BIT1|BIT2|BIT3|BIT4);
    		debounce();
    	}
    	if(GAMEOVER){
    		char buttonsToPoll[4] = {BIT1, BIT2, BIT3, BIT4};
    		while(!pollP1Buttons(buttonsToPoll, 4)){
    			//poll until something is pressed
    		}
    		TAR = 0;
    		LOSE = 0;
    		TIMER = 0;
    		GAMEOVER = 0;
    		LCDclear();
    		player = initPlayer();
    		printPlayer(player);
    	}
    }

    return 0;
}
Example #3
0
File: main.c Project: ryanlamo/lab5
int main(void) {
    WDTCTL = WDTPW | WDTHOLD;

    player = initPlayer();

    initSPI();
    LCDinit();
    LCDclear();

    RenewGame();

    printPlayer(player);


    init_timer();
    init_buttons();
   __enable_interrupt();


    while(1)
    {

    	if (player==0xC7)
    	{

    		TACTL &= ~TAIE;
    		LCDclear();
    		cursorToLineOne();
    		writeString("YOU");
    		cursorToLineTwo();
    		writeString("WON!");
    		gamedone = 1;
    		_delay_cycles(1000000);

   	    }


    	if (CountTimer >=4)
    	{
    		TACTL &= ~TAIE;
    		LCDclear();
    		cursorToLineOne();
    		writeString("Game");
    		cursorToLineTwo();
    		writeString("Over");
    		gamedone = 1;
    		_delay_cycles(1000000);

    	}

    }

	
	return 0;
}
Example #4
0
unsigned char movePlayer(unsigned char player, unsigned char direction){
	clearPlayer(player);
	LCDclear();

	if(direction == BIT1){
		if(player != 0x87 && player != 0xc7){
			player++;
		}        }
	else if(direction == BIT2){
		if(player != 0x80 && player != 0xc0){
			player--;
		}
	}
	else if(direction == BIT3){
		if(player < 0x88){
			player+=0x40;
		}
		if(player> 0xdf){
			player+=0xc0;
		}
	}
	else if(direction == BIT4){
		player &= ~ROW_MASK;}
	return player;

}
Example #5
0
void main(void) {
    char buffer[SCI_BUFSIZ+1] = {0};
    
    // Initialize all nessesary modules
    timer_init();
    SCIinit();
    encoder_init();
    motor_init();
    msleep(16); LCDinit();
    //start_heartbeat();    // Not used, TCNT overflow interrupts causing issues
    
    DDRP |= PTP_PTP0_MASK;  // Set DDR for laser GPIO
    
    // Motors off initially
    motor_set_speed(MOTOR1C, 0);
    motor_set_speed(MOTOR2C, 0);
    
    EnableInterrupts;
    
    LCDclear(); LCDputs("Ready.");
    SCIputs("HCS12 ready to go!");
    
    
    for(EVER) {
        SCIdequeue(buffer);
        cmdparser(buffer);
        memset(buffer, 0, SCI_BUFSIZ+1);    // Clear out the command buffer after each command parsed
        //LCDclear(); LCDprintf("\r%7d  %7d\n%7d  %7d", drive_value1, drive_value2, speed_error1, speed_error2);
    }
}
Example #6
0
void lcdDisplaySensors(unsigned int fd,struct sGENERAL patient){

	//while(GPIORead(changeDisplay)!=HIGH){
		char Nokia_Temp[10],Nokia_BPM[10];	
	
		Sensors.BPM = (unsigned int)serialGetchar(fd);
		Sensors.Temp = ((float)serialGetchar(fd)*5/(1023))/0.01;			
		Sensors.BPMState = healthState(patient,Sensors.BPM);
		Sensors.TempState = isNormal(Sensors.Temp);
	
		snprintf(Nokia_Temp,10,"%.1f*C",Sensors.Temp);
		snprintf(Nokia_BPM,10,"%dBPM",Sensors.BPM);	
	
		LCDclear();

		printf("Temp:%.1f\n",Sensors.Temp);
		printf("BPM:%d\n",Sensors.BPM);
		LCDdrawstring(20,0,"SENSORES");
		LCDdrawstring(25,10,Nokia_Temp);
		LCDdrawstring(20,20,Sensors.TempState);
		LCDdrawstring(25,30,Nokia_BPM);
		LCDdrawstring(20,40,Sensors.BPMState);
	
		LCDdisplay();
	//}
}	
Example #7
0
void update_display()
{
    // update display
    LCDclear();
    for(int i=0; i<linebuff.size(); i++)                         
        LCDdrawstring(0, i*8, (char*)linebuff[i].c_str());

     // drawing buttom line    
     for (int i=0; i<83; i+=2)
        LCDdrawline(i, 40, i, 40, BLACK);

     // drawing battery
     LCDdrawrect(70, 42, 12, 5, BLACK);
     LCDdrawrect(81, 43, 3, 3, BLACK);
     // battery charge state    
     battery_level = (battery_level>100) ? 100 : battery_level;
     battery_level = (battery_level<0) ? 0 : battery_level;
     LCDfillrect(70, 43, battery_level/10+1, 4, BLACK); 

    // battery plugged state 
    if(battery_plugged)
    {
        LCDdrawline(66, 43, 68, 43, BLACK);
        LCDdrawline(66, 45, 68, 45, BLACK);
        LCDfillrect(63, 42, 3, 5, BLACK);
        LCDfillrect(61, 43, 2, 3, BLACK);
        LCDdrawline(58, 44, 61, 44, BLACK);
        LCDdrawline(56, 45, 58, 45, BLACK);
    }
}
Example #8
0
static screen_t *
screen_init(const int _din, const int _sclk, const int _dc, const int _rst, const int _cs, const int contrast)
{
	screen_t *scretp;

	if (wiringPiSetup() == -1) {
		syslog(LOG_LOCAL0|LOG_ERR, "wiringPi-Error");
		return (NULL);
	}
	if (pcf8591Setup(200, 0x48) == -1) {
		syslog(LOG_LOCAL0|LOG_ERR, "wiringPi-Error");
		return (NULL);
  	}
  
	/* init and clear lcd */
	LCDInit(_sclk, _din, _dc, _cs, _rst, contrast);
	LCDclear();
  
	/* show logo */
	LCDshowLogo();

	if ((scretp = calloc(1, sizeof(screen_t))) == NULL) {
		return (NULL);
	}
	scretp->sc_contrast = contrast;
	snprintf(scretp->sc_line1_buf, sizeof(scretp->sc_line1_buf), "Pilab");
	cur_time(scretp->sc_line2_buf, sizeof(scretp->sc_line2_buf));
	return (scretp);
}
Example #9
0
File: main.c Project: ryanlamo/lab5
void RenewGame()
{
	gamedone = 0;
	LCDclear();
	player = initPlayer();
	printPlayer(player);

}
Example #10
0
int main(void){
	WDTCTL = WDTPW | WDTHOLD;
			char message[] = "ECE 382 is my favorite class! ";
			char message2[]= "I got 99 problems... but a git ain't one. ";
			initSPI();
			LCDinit();
			LCDclear();
			cursorToLineOne();
			scrollString(message,message2);
			while (1){};
}
Example #11
0
int main(void)
{
        WDTCTL = (WDTPW|WDTHOLD);


        player = initPlayer(); //debugging: had player defined twice, caused * to jump around alot
        initSPI();
        LCDinit();
        LCDclear();
        printPlayer(player);
        init_timer();
        init_buttons();
        __enable_interrupt();


        while(1)
        {
        	 if(player == 0xC7){
        	                    TACTL &= ~TAIE;
        	                    LCDclear();
        	                    cursorToLineOne();
        	                    writeString("YOU");
        	                    cursorToLineTwo();
        	                    writeString("WON!");
        	                    gameover = 1;
        	                    _delay_cycles(100000);
        	            }
        	            if(timerCount >= 4){
        	                    TACTL &= ~TAIE;
        	                    LCDclear();
        	                    cursorToLineOne();
        	                    writeString("Game");
        	                    cursorToLineTwo();
        	                    writeString("Over!");
        	                    gameover = 1;
        	                    _delay_cycles(100000);
        	            }
        }

        return 0;
}
Example #12
0
/*Author: Travis Schriner
 * Date 21 Oct 2013
 * Description: This program interacts with the
 * LCD in the geekbox and allows me to write to it
 *
 */
int main(void) {
    WDTCTL = WDTPW | WDTHOLD;	// Stop watchdog timer
	
    initSPI();
    LCDinit();
    LCDclear();

    char *topMessage = "ECE382 is my Favorite Class!";
    char *prompt = "Message?";
    char *promptKey = "Press123";
    char *message1 = "This is message 1 fool!";
    char *message2 = "This is message 2 fool!";
    char *message3 = "This is message 3 fool!";



    cursorToLineOne();
    writeString(prompt);
    cursorToLineTwo();
    writeString(promptKey);


    configureP1PinAsButton(BIT1|BIT2|BIT3);         // configure pins 1, 2, and 3 as buttons

       P1DIR |= BIT0|BIT6;                             // set launchpad LEDs to output

           char buttons[] = {BIT1, BIT2, BIT3};
           char pressedButton = pollP1Buttons(buttons, 3);

           switch (pressedButton) {
               case BIT1:

            	   waitForP1ButtonRelease(BIT1);
            	   scrollString(topMessage, message1);

                   break;
               case BIT2:

            	   waitForP1ButtonRelease(BIT2);
            	   scrollString(topMessage, message2);
                   break;
               case BIT3:

            	   waitForP1ButtonRelease(BIT3);
            	   scrollString(topMessage, message3);

                   break;
           }
 while(1){

 }
}
Example #13
0
interrupt void monstermash_isr(void){
	EALLOW;
	unsigned int value;
	unsigned long temp;
	value = ADC_get();

	if(SRAMaddress <= 0x29FFFF & a == 0){
		*SRAMaddress = value;
		SRAMaddress++;

	}
	else if(SRAMaddress <= 0x29FFFF && a ==1){
		temp = *SRAMaddress;
		temp = temp+value;
		temp = temp/2;
		value = temp;
		//TODO:this is where attenuation would go
		*SRAMaddress = value;
		SRAMaddress++;
		if(SRAMaddress > 0x29FFFF){

			b = 1;

		}
	}
	else if(b == 1){
		DINT;
		SRAMaddress = 0x260000;
		changeFunctions(samplingRate);
		EINT;
		ERTM;
	}
	else{


		SRAMaddress = 0x260000;
		a = 1;

		string_push("press 4 when second sample is ready");
		int test = keypadScan();
		while(test != 4){
			test = keypadScan();
		}
		LCDclear();
		//changeFunctions();
		//EINT;   // Enable Global interrupt INTM
		//ERTM;   // Enable Global realtime interrupt DBGM

	}
}
Example #14
0
void LCDputs(char string[])
{
    unsigned char i = 0;        //create a counter variable

    LCDclear();                 //Clear the LCD before writing

    while(string[i])            //This will continue until the /0 (end of string) character is reached
    {
        LCDwrite(string[i]);    //Write the string one element at a time
        i++;                    //Move to the next element
        if(i==16)
            LCDgotoLineTwo();   //Go to line two after element 16
                                //otherwise the string will be written off screen
    }
}
Example #15
0
int main(void) {
	WDTCTL = WDTPW | WDTHOLD;	// Stop watchdog timer
	char mainMESSAGE[] = { 'E', 'C', 'E', '3', '8', '2', ' ', 'i', 's', ' ',
			'm', 'y', ' ', 'f', 'a', 'v', 'o', 'r', 'i', 't', 'e', ' ', 'c',
			'l', 'a', 's', 's', '!'};
	int mainMessageLength = 29;
	char * mainMessagePointer = &mainMESSAGE[0];
	initSPI();
	LCDinit();
	LCDclear();
	while (1) {
		scrollString(mainMessagePointer, mainMessagePointer, mainMessageLength);
	}

	return 0;
}
Example #16
0
void button_down(byte my_floor) {
    CANframe txframe;
    LED2 = 1;
#ifdef USE_LCD
    LCDclear();
    LCDprintf("Floor: %d\nDir: %s", my_floor, "down");
#endif
    // Message to controller; down button pressed
    txframe.id = MSCAN_CTL_ID;
    txframe.priority = 0x01;
    txframe.length = 3;
    txframe.payload[0] = CMD_BUTTON_CALL;
    txframe.payload[1] = my_floor;
    txframe.payload[2] = BUTTON_DOWN;
    CANsend(&txframe);
}
Example #17
0
void Reset(char buttonToTest){
        if(buttonToTest &P2IFG){
                if(buttonToTest & P2IES){
                        gameover=0;
                        LCDclear();
                        player=initPlayer();
                        printPlayer(player);
                        clearTimer();
                        TACTL|= TAIE;
                }
                else{
                        debounce();
                }
                P2IES ^= buttonToTest;
                P2IFG &= ~buttonToTest;
        }
}
Example #18
0
int main(void) {
	
	DisableDog();
	CPUinit();
	EALLOW;
	outputEnable();
	LCDinit();
	LCDclear();
	initADC();
	DAC_init();
//	SRAMwrite(0);
//	SRAMaddress = 0x260000; //shouldn't need SRAM here
	fft_init();
	initBuffers();

	timerINIT(ISRvalue, samplingRate);




	while(1){
		if(sampleBufferFull){
			fft.InBuf = &sampleBuffer[0];
			int i;
			for(i = 0;i<FFT_SIZE;i++){
				outBuffer[i] = 0;
			}
			for(i=0;i<FFT_SIZE/2;i++){
				MagBuffer[i] = 0;
			}
			RFFT_f32(&fft);
			//fft.MagBuf = &sampleBuffer[0];
			RFFT_f32_mag(&fft);

			sampleBufferFull = 0;
			EINT;
		}
		else{
			//do nothing
		}

	}
	return 0;
}
Example #19
0
//courtesy of C2C Steinmiller...
void scrollString(char *string1, char *string2) {
        unsigned int i = 0;

        char *count1 = string1, *count2 = string2;

        while (1) {
                cursorToLineOne();
                char *currentChar = count1;

                for (i = 0; i < 8; i++) {
                     writeDataByte(*currentChar);        //send data in the string to be written

                     currentChar++;

                     if (*currentChar == 0)
                           currentChar = string1;
                }
                count1++;

                if (*count1 == 0) {
                     count1 = string1;
                }

                cursorToLineTwo();
                char *currentChar2 = count2;
                for (i = 0; i < 8; i++) {
                      writeDataByte(*currentChar2);        //send data in the string to be written

                      currentChar2++;

                      if (*currentChar2 == 0)
                      currentChar2 = string2;
                }
                count2++;

                if (*count2 == 0) {
                    count2 = string2;
                }

                __delay_cycles(600000);

                LCDclear();
        }
}
Example #20
0
int main(void) {
	
	DisableDog();
	CPUinit();
	EALLOW;
	outputEnable();
	LCDinit();
	LCDclear();
	initADC();
	DAC_init();
	init_buffer();
	timerINIT(ISRvalue, samplingRate);

	while(1){

	}


	return 0;
}
Example #21
0
void lcdDisplayProfile(struct sGENERAL perfil){
	//while(GPIORead(changeDisplay)!=HIGH){		
		char	Nokia_Nome[25],Nokia_Genero[10],Nokia_Idade[10];
		unsigned int idade = perfil.Age;

		snprintf(Nokia_Nome,25,"Nome:%s",perfil.Name);
		snprintf(Nokia_Genero,10,"Genero:%s",perfil.Sex);
		snprintf(Nokia_Idade,10,"Idade:%d",perfil.Age); 
	
		LCDclear();

		LCDdrawstring(25,0,"PERFIL");
		LCDdrawline(0, 9, 83, 9, BLACK);
		LCDdrawstring(0,11,Nokia_Nome);
		LCDdrawstring(0,29,Nokia_Genero);
		LCDdrawstring(0,39,Nokia_Idade);
	
		LCDdisplay();
	//}
}	
Example #22
0
int main(void)
{
	unsigned char buffer[17]; // Where we are reading to
	buffer[16] = '\0'; // Terminating string
	int page = 0xa0;
	int offset = 0;

	init_timer0(); // LCD needs timer0 to function
	LCDinit(); // Initializing LCD
	ini_i2c(); // Initializing i2c

	/******************** MAIN LOOP *********************/
	while(1) {
		offset &= 0xff;
		buffer[0] = offset;
		escreve_i2c(page, buffer, 1); /* tell i2c we want to read from page + offset */
		if(le_i2c(page, buffer, 16)) { /* If we failed to read 16 bytes */
			LCDclear();
			LCDgoto(0, 0);
			LCDputs("Problema de leitura");
			LCDgoto(1, 0);
			LCDputs(buffer);
		} else {
			LCDgoto(0, 0); // Go to first line, first column
			LCDputs("Pg = ");
			LCDputchar(page + '0'); // Prints page number as char

			LCDgoto(0, 8); // Go to the second half of first line
			LCDputs("Of = ");
			LCDputchar(offset + '0'); // Prints offset as char

			LCDgoto(1, 0); // Fisrt column of the second line
			LCDputs(buffer); // Prints buffer
		}
	}

	return 0;
}
Example #23
0
int main(int argc, char **argv)
{
	int i;
   setvbuf(stdout, NULL, _IOLBF, 0);

	setup_sighandlers();

    // check wiringPi setup
    if (wiringPiSetup() == -1)
    {
        printf("wiringPi-Error\n");
        exit(1);
    }

    // init and clear lcd
    pinMode(_bk, OUTPUT);
    digitalWrite(_bk, HIGH);

	unlink(DEVFILE);
	if (mkfifo(DEVFILE, 0666) < 0)
		fatal("dispd: Failed to create %s: %m\n", DEVFILE);
	if (chmod(DEVFILE, 0666) < 0)
		fatal("didpd: Failed to set permissions on %s: %m\n", DEVFILE);

	//if (daemon(0,1) < 0)
	//	fatal("dispd: Failed to daemonize process: %m\n");

    LCDInit(_sclk, _din, _dc, _cs, _rst, contrast);
    LCDclear();
    LCDshowLogo();
    delay(2000);

	go_go_go();

	return 0;
}
Example #24
0
int main (void)
{
  // print infos
  printf("Raspberry Pi PCD8544 sysinfo display\n");
  printf("========================================\n");

  // check wiringPi setup
  if (wiringPiSetup() == -1)
  {
	printf("wiringPi-Error\n");
    exit(1);
  }

  // init and clear lcd
  LCDInit(_sclk, _din, _dc, _cs, _rst, contrast);
  LCDclear();

  // show logo
  LCDshowLogo();

  delay(2000);

  for (;;)
  {
	  // clear lcd
	  LCDclear();

	  // get system usage / info
	  struct sysinfo sys_info;
	  if(sysinfo(&sys_info) != 0)
	  {
		printf("sysinfo-Error\n");
	  }

	  // uptime
	  char uptimeInfo[15];
	  unsigned long uptime = sys_info.uptime / 60;
	  sprintf(uptimeInfo, "Uptime %ld min.", uptime);

	  // cpu info
	  char cpuInfo[10];
	  unsigned long avgCpuLoad = sys_info.loads[0] / 1000;
	  sprintf(cpuInfo, "CPU %ld%%", avgCpuLoad);

	  // ram info
	  char ramInfo[10];
	  unsigned long totalRam = sys_info.freeram / 1024 / 1024;
	  sprintf(ramInfo, "RAM %ld MB", totalRam);

	  // build screen
	  //LCDdrawstring(0, 0, "Raspberry Pi:");
	  LCDdrawstring(0, 0, "Sunfounder.com");
	  LCDdrawline(0, 10, 83, 10, BLACK);
	  LCDdrawstring(0, 12, uptimeInfo);
	  LCDdrawstring(0, 20, cpuInfo);
	  LCDdrawstring(0, 28, ramInfo);
	  LCDdisplay();

	  delay(1000);
  }

    //for (;;){
  //  printf("LED On\n");
  //  digitalWrite(pin, 1);
  //  delay(250);
  //  printf("LED Off\n");
  //  digitalWrite(pin, 0);
  //  delay(250);
  //}

  return 0;
}
Example #25
0
File: main.c Project: Jsrazz/Lab4
/*Author C2C Rasmussen
 * Date Oct 28 13
 *
 *  Implements the program
 * main.c
 */
int main(void) {
	WDTCTL = WDTPW | WDTHOLD;	// Stop watchdog timer

	//stores strings in an arrays (idea from C2C Mordan)

	char string1[] = {'E','C','E',' ','3', '8', '2',' ','i','s',' ','m','y',' ','f','a','v','o','r','i','t','e',' ','c','l','a','s','s','!',' ',' '};

	char message[] = {'P','R','E','S','S',' ','K','E','Y',' ',' '}; // 11 in length
	char message1[] = {'w','a','z','z','u','p'}; // 6 in length
	char message2[] = {'h','o','w',' ','d','o',' ','y','o','u', ' ','d','o','?',' ',' '}; // 16 in length
	char message3[] = {'f','a','l','c','o','n',' ','p','u','n','c','h',' ',' '}; // 14 in length

	initSPI();
	LCDinit();
	LCDclear();

		while(1){
	cursorToLineOne();
	createString(message, 11);

	scrollLCD(message, 11);



	configureP1PinAsButton(BIT1|BIT2|BIT3);         // configure pins 1, 2, and 3 as buttons

	P1DIR |= BIT0|BIT6;                             // set launchpad LEDs to output

	while (1) {
		char buttons[] = {BIT1, BIT2, BIT3};
		char pressedButton = pollP1Buttons(buttons, 3);

		switch (pressedButton) {
		case BIT1:							// detects if Bit1 is enabled
			P1OUT ^= BIT0|BIT6;
			waitForP1ButtonRelease(BIT1);

			while(1){
			cursorToLineOne();						//passes new message
			createString(string1, 31);
			cursorToLineTwo();
			createString(message1, 6 );
			scrollLCD(string1, 31);
			scrollLCD(message1, 6);
			}
			// do something in response
			break;


		case BIT2:

			P1OUT ^= BIT0|BIT6;
			waitForP1ButtonRelease(BIT2);
			// do something in response

			while(1){
			cursorToLineOne();
			createString(string1, 31);
			cursorToLineTwo();
			createString(message2, 16 );
			scrollLCD(string1, 31);
			scrollLCD(message2, 16);
			}
			break;


		case BIT3:
			// toggle LEDs and wait for release
			P1OUT ^= BIT0|BIT6;
			waitForP1ButtonRelease(BIT3);


			while(1){
			cursorToLineOne();
			createString(string1, 31);
			cursorToLineTwo();
			createString(message3, 14 );
			scrollLCD(string1, 31);
			scrollLCD(message3, 14);
			}
			break;
		}
	}

		}

}
Example #26
0
static void go_go_go(void)
{
	int fd;
	struct timeval tv;
	static char line[128];
	int nchars = 0;
    int ntimes_out = 0;
    int ntimes_reset = 0;
	if ((fd = open(DEVFILE, O_RDWR|O_NONBLOCK)) == -1)
		fatal("dispd: Failed to open %s: %m\n", DEVFILE);

	for (;;) {
		int n, width, servo;
		fd_set ifds;
		char width_arg[64];

		FD_ZERO(&ifds);
		FD_SET(fd, &ifds);
        tv.tv_sec = 3;
        tv.tv_usec = 0;
		if ((n = select(fd+1, &ifds, NULL, NULL, &tv)) != 1)
        {
            // turn off backlight after 6 sec
            if(++ntimes_out > 2)
            {
                digitalWrite(_bk, LOW);
                delay(10);
            }
            if(++ntimes_reset > 20) // 60 sec
            {
                ntimes_reset = 0;
                LCDInit(_sclk, _din, _dc, _cs, _rst, contrast);
                LCDdisplay();
                delay(10);
            }
            //printf("%s : %d\n", __FILE__, __LINE__);
			continue;
        }   
		while (read(fd, line+nchars, 1) == 1) {
            //printf("%s : %d\n", __FILE__, __LINE__);
			if (line[nchars] == '\n') 
            {
				line[nchars] = '\0';
				nchars = 0;
                std::string str = line;

                // do whatever
                if(strcmp(line,"%clear%") == 0)                
                {
                    linebuff.clear();
                    update_display();
                    digitalWrite(_bk, HIGH);               
                }   
                else if(strcmp(line,"%logo%") == 0)
                {
                    linebuff.clear();
                    LCDclear();
                    LCDshowLogo();
                    digitalWrite(_bk, HIGH);
                }
                else if(str.substr(0,5) == "%bat%")
                {
                    std::string val = str.substr(6, str.npos);
                    battery_level = atoi(val.c_str());
                    update_display();
                }
                else if(str.substr(0,6) == "%plug%")
                {
                    std::string val = str.substr(7, str.npos);
                    battery_plugged = (atoi(val.c_str()) != 0);
                    update_display();
                }

                else
                {
                    int start = 0;  
                    int size = 14;  
                    while (start < str.size()) 
                    {  
                        int end = ((start + size) < str.size()) ? size : str.size() - start;                        
                        if(linebuff.size()>4)
                            linebuff.erase(linebuff.begin());	                  
                        linebuff.push_back(str.substr(start, end));
                        start += size;  
                    } 
                    update_display();
                    digitalWrite(_bk, HIGH);
                    ntimes_out = 0;
                }
                // turn off backlight after 5 sec
                if(++ntimes_out > 5)
                {
                    digitalWrite(_bk, LOW);
                    delay(10);
                }
                LCDdisplay();
                delay(10);
			} 
            else 
            {
				if (++nchars >= 126) {
					fprintf(stderr, "Input too long\n");
					nchars = 0;
				}
			}
		} // end while
	} // end for
}
Example #27
0
void callbox(byte my_floor) {
    byte rxmessage[PAYLOAD_SIZE];   // Received data payload
    static byte floor, direction;
    word distance;
    static byte flag_dist_init = 0;
    CANframe txframe;   // Transmitted CAN frame
    
    floor = 0xFF;   // Start at false floor
    direction = DIRECTION_STATIONARY;   // Assume starting car direction is stationary
    
    if (!flag_dist_init){
        dist_init();
        flag_dist_init = 1;
    }
    
    if(SW1 && !sw1_pressed) {  
        sw1_pressed = 1;
        button_up(my_floor);
    }
    if(SW2 && !sw2_pressed) { 
        sw2_pressed = 1;
        button_down(my_floor);
    }
    if(!SW1) sw1_pressed = 0;
    if(!SW2) sw2_pressed = 0;
    
    runSerialCAN(MSCAN_NODE_ID);
    
    if(data_available()) {
        
        CANget(rxmessage);
        
        switch(rxmessage[0]) {
            case CMD_LOCATION:
                floor = rxmessage[1];
                direction = rxmessage[2];
                
#ifdef USE_LCD
                LCDclear();
                LCDprintf("Floor: %d\nDir: %d", floor, direction);
#endif
#ifdef USE_LED7
                led7_write(led7_table[floor]);
#endif
                break;
            case CMD_BUTTON_CALL:
                rxmessage[1] == DIRECTION_UP ? button_up(my_floor) : button_down(my_floor);
                break;
            case CMD_ERROR:
#ifdef USE_LCD
                LCDclear();
                LCDprintf("Error condition\nreceived!");
#endif
                break;
            default:
#ifdef USE_LCD
                LCDclear();
                LCDputs("Unknown command");
#endif
                break;
        }
        
        // Turn off indicator LED once car has reached local floor
        if(floor == my_floor) {
            LED1 = 0; LED2 = 0;
        }
        
    }
    
    // Sonar sensor currently attached to callbox 1
    // Send off distance message to controller node
    if (my_floor == FLOOR1) {
        distance = dist_read();
        
        txframe.id = MSCAN_CTL_ID;
        txframe.priority = 0x01;
        txframe.length = 3;
        txframe.payload[0] = CMD_DISTANCE;
        txframe.payload[1] = (distance & 0xFF00) >> 8;         
        txframe.payload[2] = (distance & 0x00FF);
        CANsend(&txframe);
    }
Example #28
0
/*
 * Elevator car functionality
 * - 
 */
void car(void) {
    byte ret;
    byte sw1_pressed = 0, sw2_pressed = 0;
    char *command, *floor, *direction;
    byte cur_floor;
    byte elevatorDirection = 0xFF;
    
    CANframe txframe;               // Transmitted CAN frames
    byte rxmessage[PAYLOAD_SIZE];   // Received data payload
    
    cur_floor = 0;
    
    // Message to controller; floor 1
    txframe.id = MSCAN_CTL_ID;
    txframe.priority = 0x01;
    txframe.length = 2;
    txframe.payload[0] = CMD_BUTTON_CAR;
    txframe.payload[2] = DIRECTION_STATIONARY; 
    
    
    if(BTN_FLOOR1) {
        if (BUTTON_FL1 > cur_floor) {
            elevatorDirection = DIRECTION_DOWN;
        } else if (BUTTON_FL1 < cur_floor) {
            elevatorDirection = DIRECTION_UP;
        } else {
            elevatorDirection = DIRECTION_STATIONARY;
        }
        
        txframe.payload[1] = BUTTON_FL1;
        txframe.payload[2] = elevatorDirection;
        
        ret = CANsend(&txframe);
        if(ret) {
            // Message could not be sent!
        }
    }
    if(BTN_FLOOR2) {
        if (BUTTON_FL2 > cur_floor) {
            elevatorDirection = DIRECTION_DOWN;
        } else if (BUTTON_FL2 < cur_floor) {
            elevatorDirection = DIRECTION_UP;
        } else {
            elevatorDirection = DIRECTION_STATIONARY;
        }
        
        txframe.payload[1] = BUTTON_FL2;
        txframe.payload[2] = elevatorDirection;
        
        ret = CANsend(&txframe);
        if(ret) {
            // Message could not be sent!
        }
    }
    if(BTN_FLOOR3) {
        if (BUTTON_FL3 > cur_floor) {
            elevatorDirection = DIRECTION_DOWN;
        } else if (BUTTON_FL3 < cur_floor) {
            elevatorDirection = DIRECTION_UP;
        } else {
            elevatorDirection = DIRECTION_STATIONARY;
        }
        
        txframe.payload[1] = BUTTON_FL3;
        txframe.payload[2] = elevatorDirection;
        
        ret = CANsend(&txframe);
        if(ret) {
            // Message could not be sent!
        }
    }
    if(BTN_DROPEN) {
        txframe.payload[1] = BUTTON_DOOR_OPEN;
        txframe.payload[2] = elevatorDirection;
        
        ret = CANsend(&txframe);
        if(ret) {
            // Message could not be sent!
        }
    }
    if(BTN_DRCLOSE) {
        
        txframe.payload[1] = BUTTON_DOOR_CLOSE;
        txframe.payload[2] = elevatorDirection;
        
        ret = CANsend(&txframe);
        if(ret) {
            // Message could not be sent!
        }
    }
    if(BTN_ESTOP) {
        
        txframe.payload[1] = BUTTON_STOP;
        txframe.payload[2] = elevatorDirection;
        
        ret = CANsend(&txframe);
        if(ret) {
            // Message could not be sent!
        }
    }
    
    // CAN bus <-> serial link
    // Check for new incoming messages and send out received messages via serial
    runSerialCAN(MSCAN_NODE_ID);
    
    if(data_available()) {
        
        CANget(rxmessage);
        
        switch(rxmessage[0]) {
            case CMD_LOCATION:
                command = "Loc";
                break;
            case CMD_BUTTON_CALL:
                command = "Call";
                break;
            case CMD_BUTTON_CAR:
                command = "Car";
                break;
            case CMD_DISP_APPEND:
                command = "Disp";
                break;
            case CMD_ERROR:
                command = "Err";
                break;
            default:
                // Command didn't match known commands!
                goto car_cmd_error;
        }
        
        switch(rxmessage[1]) {
            case FLOOR1:
                floor = "1";
                cur_floor = 1;
                break;
            case FLOOR2:
                floor = "2";  
                cur_floor = 2;
                break;
            case FLOOR3:
                floor = "3"; 
                cur_floor = 3;
                break;
            default:
                // Command didn't match known commands!
                goto car_cmd_error;
        }
        
        
        // Turn off LED when car arrives at requested floor
        if ( rxmessage[0] == CMD_LOCATION ) {           
            if( rxmessage[1] == FLOOR1 ) {
                LED1 = 0;
            }
            if( rxmessage[1] == FLOOR2 ) {
                LED2 = 0;
            }
            /*if( rxmessage[1] == FLOOR3 ) {
                LED3 = 0;
            }*/
        }
        
#ifdef USE_LED7
        led7_write(led7_table[cur_floor]); 
#endif
#ifdef USE_LCD
        LCDhome();
        LCDclear();
        LCDprintf("Command: %s\nFloor%s", command, floor);
#endif
        
        return;
        
car_cmd_error:
        //LCDhome();
        //LCDclear();
        //LCDprintf("Error in\ncommand");
        return;
        
    }
}
Example #29
0
/*
 * Controller functionality
 * - Send elevator location messages to callboxes
 * - Listen for button press messages
 */
void controller() {
    byte sw1_pressed = 0, sw2_pressed = 0;
    byte rxmessage[PAYLOAD_SIZE];   // Received data payload 
    byte button_pressed;
	byte next_floor;
    char *button_floor_str, *button_direction_str;
    
    byte update_lcd = 1;
    byte cycle_count = 0;
    word distance;  // car height in cm, distance measurement in mm
    byte cur_floor;
    byte last_floor = 0;
    //byte b;   // used for debug manual frame sending testing
    
    dist_init();
    mctrl_init();   // Initialize servo motor controller
    
    for(;;) {
        
        mctrl_update();
        
        cycle_count++;
        
        if ( cycle_count == 10 ) {
            update_lcd = 1;
            cycle_count = 0;
        }
        
        if ( update_lcd ) {
            update_lcd = 0;
/*            
#ifdef USE_LCD
            if ( cur_floor == 0 ) { 
                LCDclear();
                LCDputs("No car");
            } else {
                LCDclear();
                LCDprintf("%dmm/F%d", car_height, cur_floor);
            }
#endif
*/
        }
        
        // CAN bus <-> serial link
        // Check for new incoming messages and send out received messages via serial
        runSerialCAN(MSCAN_NODE_ID);
        /*
        while ( sci_bytesAvailable() ) {
            sci_readByte(&b);
            lcd_putc(b);  
        } */
        
        if(data_available()) {
            
            CANget(rxmessage);
            
            switch(rxmessage[0]) {
            case CMD_BUTTON_CALL:
                button_pressed = rxmessage[1];
                
                addToQueue(button_pressed);
                next_floor = peekNextFloor();
                pid_setpoint(FLOOR2SETPOINT(next_floor));
                
                switch(cur_floor) {
                case FLOOR1:
                    button_floor_str = "1";
                    break;
                case FLOOR2:
                    button_floor_str = "2";
                    break;
                case FLOOR3:
                    button_floor_str = "3";
                    break;
                default:
                    break;
                }
                
                if(next_floor == cur_floor) {
                    button_direction_str = "stat";
                } else if(next_floor > cur_floor) {
                    button_direction_str = "up  ";
                } else {
                    button_direction_str = "down";
                }
                
#ifdef USE_LCD
                LCDhome();
                LCDprintf("\nFloor%s Dir %s", button_floor_str, button_direction_str);
#else
#ifdef USE_LCD2
                lcd_goto(0x10); // Start at second line
                lcd_puts("Floor");
                lcd_puts(button_floor_str);
                lcd_puts(" Dir ");
                lcd_puts(button_direction_str);
#endif
#endif
                break;
            case CMD_BUTTON_CAR:
                button_pressed = rxmessage[1];
                if(button_pressed == BUTTON_STOP) {
                    // call emergency stop function
                } else if(button_pressed < BUTTON_DOOR_CLOSE) {
                    addToQueue(button_pressed);
                }
                    
                next_floor = peekNextFloor();
                pid_setpoint(FLOOR2SETPOINT(next_floor));
                
                switch(cur_floor) {
                case FLOOR1:
                    button_floor_str = "1";
                    break;
                case FLOOR2:
                    button_floor_str = "2";
                    break;
                case FLOOR3:
                    button_floor_str = "3";
                    break;
                default:
                    break;
                }
                
                if(next_floor == cur_floor){
                    button_direction_str = "stat";
                }else if(next_floor > cur_floor){
                    button_direction_str = "up  ";
                }else {
                    button_direction_str = "down";
                }
                
#ifdef USE_LCD
                LCDhome();
                LCDprintf("\nFloor%s Dir %s", button_floor_str, button_direction_str);
#endif
#ifdef USE_LCD2
                lcd_goto(0x10); // Start at second line
                lcd_puts("Floor");
                lcd_puts(button_floor_str);
                lcd_puts(" Dir ");
                lcd_puts(button_direction_str);
#endif
                break;
            case CMD_DISTANCE:
                distance = (rxmessage[1] << 8) | rxmessage[2];
                pid_feedback(distance);
                
                if (distance < SETPOINT_F1 + FLOOR_MARGIN) cur_floor = FLOOR1;
                if (distance > SETPOINT_F2 - FLOOR_MARGIN && distance < SETPOINT_F2 + FLOOR_MARGIN) cur_floor = FLOOR2;
                if (distance > SETPOINT_F3 - FLOOR_MARGIN && distance < SETPOINT_F3 + FLOOR_MARGIN) cur_floor = FLOOR3;
                
                if ( distance > 1500 ) {
                    cur_floor = 0;
#ifdef USE_LED7
                    led7_write(led7_bars[1]);
#endif
                } else {
#ifdef USE_LED7
                    led7_write(led7_table[cur_floor]);
#endif
                    if ( cur_floor != last_floor ) {
                        update_floor(cur_floor);
                        last_floor = cur_floor;
                        
                        // if we have reached the target floor, pop off the top of the queue
                        // TODO: change name of getNextFloor() to be more descriptive
                        if(cur_floor == next_floor){
                            getNextFloor();
                            next_floor = peekNextFloor();
                            pid_setpoint(FLOOR2SETPOINT(next_floor));
                        }
                    }
                }
#ifdef USE_LCD
                LCDhome();
                LCDprintf("Dist: %4d", distance);
#endif
                break;
            case CMD_ERROR:
                
                break;
            default:
#ifdef USE_LCD
                LCDclear();
                LCDputs("\nUnknown command");
#endif
#ifdef USE_LCD2
                lcd_goto(0x10); // Start at second line
                lcd_puts("Unknown command");
#endif
                break;
            }
            
        }
        
        delay_ms(100);
    }
}
Example #30
0
/***********************cmdparser*******************************
* 
*   Purpose: Parse the command string to call the correct function. 
*
*   Input: char *cmdtype: input command string.
*
*   Output: int result: Resulting integer value.
*
***************************************************************/
void cmdparser(char *buffer) {
    char cmdtype[CMD_LEN+1] = {0};
    int numchars = 0;
    static int numcmd = 0;  // Count of number of commands parsed
    static byte tog = 0;    // Laser toggle bit
    char motor1_pct, motor2_pct;
    
    
    cmdtype[0] = buffer[numchars];
    cmdtype[1] = buffer[numchars+1]; 
    cmdtype[2] = buffer[numchars+2]; 
    cmdtype[CMD_LEN] = '\0';    // Terminate input command after three bytes, leaving just the command type
    
    switch(cmdconv(cmdtype)) {
    case 0:     // If no command found, go to next character.
        seekcmd(buffer, &numchars);
        break;
    
    case PNG:   // ping
        SCIprintf("png%05d",numcmd);   // echo command confirmation with stamp.
        //LCDclear(); LCDputs("Ping!");
        
        numcmd++;
        numchars += SCI_CMDSIZ;
        break;
    
    case ABT:  // STOP THE PRESS!
        SCIprintf("abt%05d",numcmd);
        LCDclear(); LCDputs("Abort!\nAbort!");
        stop_motion();
        
        numcmd++;
        numchars += SCI_CMDSIZ;
        break;
    
    case RES:  // Resume operation
        SCIprintf("res%05d",numcmd);
        LCDclear(); LCDputs("Resuming...");
        start_motion();
        LCDclear(); LCDputs("Resumed");
        
        numcmd++;
        numchars += SCI_CMDSIZ;
        break;
    
    case MOV:   // Set motor speed (0% - 100%)
        SCIprintf("mov%05d", numcmd);
        if(buffer[numchars+3] == '2') {
            // Both motors selected
            TC_INT_DISABLE(TC_MOTOR);   // Disable motor control law
              motor_set_speed(MOTOR1C, (char)atoi(&buffer[numchars+4]));
              motor_set_speed(MOTOR2C, (char)atoi(&buffer[numchars+4]));
            TC_INT_ENABLE(TC_MOTOR);    // Re-enable motor control law
            //LCDclear(); LCDprintf("\rM%c: %3d  M%c: %3d", MOTOR1C, atoi(&buffer[numchars+4]), MOTOR2C, atoi(&buffer[numchars+4]));
        }
        else {
            motor_set_speed(buffer[numchars+3], (char)atoi(&buffer[numchars+4]));
            //LCDclear(); LCDprintf("\rMotor %c: %3d", buffer[numchars+3], atoi(&buffer[numchars+4]));
        }
        
        numcmd++;
        numchars += SCI_CMDSIZ;
        break;
    
    case DST:   // Set motor distance (+speed)
        SCIprintf("dst%05d", numcmd);
        switch(buffer[numchars+4]) {
        case '0':   // Setting a speed
            
            motor1_pct = motor_convert(MOTOR1C, (int)atoi(&buffer[numchars+5]));
            motor2_pct = motor_convert(MOTOR2C, (int)atoi(&buffer[numchars+5]));
            
            // Set speed to both motors if 4th char is a '2'
            if(buffer[numchars+3] == '2') {
                TC_INT_DISABLE(TC_MOTOR);   // Disable motor control law
                    motor_set_speed(MOTOR1C, motor1_pct);
                    motor_set_speed(MOTOR2C, motor2_pct);
                TC_INT_ENABLE(TC_MOTOR);    // Re-enable motor control law
            } else
                motor_set_speed(buffer[numchars+3],
                  motor_convert(buffer[numchars+3], (int)atoi(&buffer[numchars+5]))
                );
            
            //LCDclear(); LCDprintf("\rM1: %3d M2: %3d", motor1_pct, motor2_pct);
            //LCDprintf("\nS1: %3d S2: %3d", atoi(&buffer[numchars+5]), atoi(&buffer[numchars+5]));
            
            break;
        case '1':   // Setting a distance
            
            // Set speed to both motors if 4th char is a '2'
            if(buffer[numchars+3] == '2') {
                motor_set_distance(MOTOR1C, (word)atoi(&buffer[numchars+5]));
                motor_set_distance(MOTOR2C, (word)atoi(&buffer[numchars+5]));
                //LCDclear(); LCDprintf("\nD%c: %3d  D%c: %3d", MOTOR1C, atoi(&buffer[numchars+5]), MOTOR2C, atoi(&buffer[numchars+5]));
            }
            else {
                motor_set_distance(buffer[numchars+3], (word)atoi(&buffer[numchars+5]));
                //LCDclear(); LCDprintf("\rDist %c: %3d", buffer[numchars+3], atoi(&buffer[numchars+5]));
            }
            
            break;
        }
        
        numcmd++;
        numchars += SCI_CMDSIZ;
        break;
    
    case SPN:   // Spin in place
        SCIprintf("spn%05d", numcmd);
        DisableInterrupts;
        motor_set_speed(MOTOR1C, -50);
        motor_set_speed(MOTOR2C, 50);
        motor_set_distance(MOTOR1C, (word)atoi(&buffer[numchars+3]));
        motor_set_distance(MOTOR2C, (word)atoi(&buffer[numchars+3]));
        EnableInterrupts;
        SCIprintf("Dist: %3d\n", atoi(&buffer[numchars+3]));
        
        numcmd++;
        numchars += SCI_CMDSIZ;
        break;
    
    case AIM:   // Toggle laser pointer
        SCIprintf("aim%05d",numcmd);
        tog = (tog) ? 0 : 1;
        PTP_PTP0 = (tog) ? 1 : 0;
        
        numcmd++;
        numchars += SCI_CMDSIZ;
        break;
    
    case STP:   // Force stop; set motor PWM to zero to stop the high frequency ringing!
        SCIprintf("stp%05d",numcmd);
        TC_INT_DISABLE(TC_MOTOR);   // Disable motor control law
          motor_set_duty(MOTOR1C, 0);
          motor_set_duty(MOTOR2C, 0);
        TC_INT_ENABLE(TC_MOTOR);    // Re-enable motor control law
        
        numcmd++;
        numchars += SCI_CMDSIZ;
        break;
    
    }
}