/*************************************************************************
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA.      *
* All rights reserved. All use of this software and documentation is     *
* subject to the License Agreement located at the end of this file below.*
**************************************************************************
* Description:                                                           *
* The following is a simple hello world program running MicroC/OS-II.The * 
* purpose of the design is to be a very simple application that just     *
* demonstrates MicroC/OS-II running on NIOS II.The design doesn't account*
* for issues such as checking system call return codes. etc.             *
*                                                                        *
* Requirements:                                                          *
*   -Supported Example Hardware Platforms                                *
*     Standard                                                           *
*     Full Featured                                                      *
*     Low Cost                                                           *
*   -Supported Development Boards                                        *
*     Nios II Development Board, Stratix II Edition                      *
*     Nios Development Board, Stratix Professional Edition               *
*     Nios Development Board, Stratix Edition                            *
*     Nios Development Board, Cyclone Edition                            *
*   -System Library Settings                                             *
*     RTOS Type - MicroC/OS-II                                           *
*     Periodic System Timer                                              *
*   -Know Issues                                                         *
*     If this design is run on the ISS, terminal output will take several*
*     minutes per iteration.                                             *
**************************************************************************/


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "includes.h"

/* Definition of Task Stacks */
#define   TASK_STACKSIZE       2048
OS_STK    task1_stk[TASK_STACKSIZE];
OS_STK    task2_stk[TASK_STACKSIZE];
OS_STK    task3_stk[TASK_STACKSIZE];
OS_STK    task4_stk[TASK_STACKSIZE];
OS_STK    task5_stk[TASK_STACKSIZE];

/* Definition of Task Priorities */

#define TASK1_PRIORITY      1
#define TASK2_PRIORITY      2
#define TASK3_PRIORITY      3
#define TASK4_PRIORITY      4
#define TASK5_PRIORITY      5

#define LEDS (char * ) GREEN_LEDS_BASE
#define RS232 (char *) "/dev/RS_232_UART"
#define BUFFER_SIZE 2


/*Car Steering Variables*/
#define STEERING_BASE (char*)MAX_PWM_SERVO_BASE

#define CAR_STEERING_COUNTER_DUTY_CYCLE 13
#define CAR_STEERING_HALF_DUTY_CYCLE 18
#define CAR_STEERING_CLOCKWISE_DUTY_CYCLE 23

/* Motor controls*/
#define MOTOR_BASE (char*)MAX_PWM_MOTOR_BASE

#define MOTOR_COUNTER_DUTY_CYCLE 13
#define MOTOR_HALF_DUTY_CYCLE 18
#define MOTOR_CLOCKWISE_DUTY_CYCLE 24

/*Semaphore Instantiation*/
OS_EVENT *Motor_sem;
OS_EVENT *Control_sem;


FILE* RS232_fp;


/**/
void task1(void* pdata)
{
    char flip = 0;
    
  while (1)
  {
    if(flip){
        *LEDS = 0xff;
    } else {
        *LEDS = 0x00;
    }
    flip = ~flip;
    
    //printf("Hello from task1\n");
    OSTimeDlyHMSM(0, 0, 1, 0);
  }
}
/**/


/* Prints "Hello World" and sleeps for three seconds */
/**/
void task2(void* pdata)
{
    char rx_msg = 'z';
    char* rx_msg_str = malloc(sizeof(char) * BUFFER_SIZE + 1);
    
  while (1)
  { 
    if(RS232_fp != NULL)
    {    
        //rx_msg = fgetc(RS232_fp);
        memset(rx_msg_str, 0, BUFFER_SIZE + 1);
        fgets(rx_msg_str, BUFFER_SIZE + 1, RS232_fp);
    }

/*
    if( ((rx_msg >= 'a') && (rx_msg <= 'i')) || ((rx_msg >= 'r') && (rx_msg <= 'z')) )
    {
        printf("%c", rx_msg);
    }
*/    
    
/*
    if( ((rx_msg_str[0] >= 'a') && (rx_msg_str[0] <= 'i')) || ((rx_msg_str[0] >= 'r') && (rx_msg_str[0] <= 'z')) )
    {
        if( ((rx_msg_str[1] >= 'a') && (rx_msg_str[1] <= 'i')) || ((rx_msg_str[1] >= 'r') && (rx_msg_str[1] <= 'z')) )
        {
            printf("%s", rx_msg_str);
        }
    }
*/
    
    if((rx_msg_str[0] == 'a') || (rx_msg_str[0] == 'c'))
    {
        if((rx_msg_str[1] == 'b') || (rx_msg_str[1] == 'd'))
        {
            printf("Steering: %c -- Motor: %c\n", rx_msg_str[0], rx_msg_str[1]);
        }
        
        else
            printf("\n%s: 2nd char invalid.\n", rx_msg_str);
    }
    
    else if((rx_msg_str[0] == 'b') || (rx_msg_str[0] == 'd'))
    {
        if((rx_msg_str[1] == 'a') || (rx_msg_str[1] == 'c'))
        {
            printf("Motor: %c -- Steering: %c\n", rx_msg_str[0], rx_msg_str[1]);
        }
        
        else
            printf("\n%s: 2nd char invalid.\n", rx_msg_str);
    }
    
    else
        printf("\n%s: 1st char invalid.\n", rx_msg_str);
    
    
        
    OSTimeDlyHMSM(0, 0, 0.1, 0);
  }
}
/**/


/*
//USE THIS LATER!

if((rx_msg_str[0] >= 'a') && (rx_msg_str[0] <= 'i'))
    {
        if((rx_msg_str[1] >= 'r') && (rx_msg_str[1] <= 'z'))
        {
            printf("Steering: %c -- Motor: %c\n", rx_msg_str[0], rx_msg_str[1]);
        }
        
        else
            printf("\n%s: 2nd char invalid.\n", rx_msg_str);
    }
    
    else if((rx_msg_str[0] >= 'r') && (rx_msg_str[0] <= 'z'))
    {
        if((rx_msg_str[1] >= 'a') && (rx_msg_str[1] <= 'i'))
        {
            printf("Motor: %c -- Steering: %c\n", rx_msg_str[0], rx_msg_str[1]);
        }
        
        else
            printf("\n%s: 2nd char invalid.\n", rx_msg_str);
    }
    
    else
        printf("\n%s: 1st char invalid.\n", rx_msg_str);

*/



void task3(void* pdata)//Deal with the Motor
{
static int wave=1;
static int middle=1;
int k=0;

INT8U err;

while(1){

wave+=1;
if(wave >1)
wave=0;

//OSSemPend(Motor_sem,0,&err);

/*
printf("Value of the Motor switch is: %d \n", *MOTOR_SWITCH);


if(*CONTROL_SWITCH>=0)
{
    //Neutral
    
    if(*MOTOR_SWITCH==0 || *MOTOR_SWITCH==1)
    {
    *MOTOR_BASE=MOTOR_HALF_DUTY_CYCLE;
    }
*/    
    *MOTOR_BASE=MOTOR_HALF_DUTY_CYCLE;
    
    //Turning Clockwise
    
    /*
    if(*MOTOR_SWITCH==2)
    {
    *MOTOR_BASE=MOTOR_HALF_DUTY_CYCLE +1;
    }
    if(*MOTOR_SWITCH==6)
    {
    *MOTOR_BASE=MOTOR_HALF_DUTY_CYCLE +3;
    }
    if(*MOTOR_SWITCH==14)
    {
    *MOTOR_BASE=MOTOR_HALF_DUTY_CYCLE +4;
    }
     if(*MOTOR_SWITCH==30)
    {
    *MOTOR_BASE=MOTOR_CLOCKWISE_DUTY_CYCLE;
    }

    
    //Turning CounterClockwise
    
    if(*MOTOR_SWITCH==3)
    {
    *MOTOR_BASE=MOTOR_HALF_DUTY_CYCLE -1;
    }
    if(*MOTOR_SWITCH==7)
    {
    *MOTOR_BASE=MOTOR_HALF_DUTY_CYCLE -3;
    }
     if(*MOTOR_SWITCH==15)
    {
    *MOTOR_BASE=MOTOR_HALF_DUTY_CYCLE -4;
    }
      if(*MOTOR_SWITCH==31)
    {
    *MOTOR_BASE=MOTOR_COUNTER_DUTY_CYCLE;
    }

}
*/
   OSTimeDlyHMSM(0, 0, 0, 20);
      
}//end while loop
        
}//end task 3

void task4(void* pdata)//Deal with the Steering
{
static int stumble=1;

INT8U err;

while(1){

stumble+=1;
if(stumble >1)
stumble=0;

//OSSemPend(Control_sem,0,&err);

//printf("Value of the Control switch is: %d \n", *CONTROL_SWITCH);

*STEERING_BASE=CAR_STEERING_COUNTER_DUTY_CYCLE;

/*
if(*CONTROL_SWITCH>=0)
{
    //Neutral

    if(*CONTROL_SWITCH==0 || *CONTROL_SWITCH==1)
    {
    *STEERING_BASE=CAR_STEERING_HALF_DUTY_CYCLE;
    }
    
    //Turning Clockwise
//    
    if(*CONTROL_SWITCH==2)
    {
    *STEERING_BASE=CAR_STEERING_HALF_DUTY_CYCLE +1;
    }
    if(*CONTROL_SWITCH==6)
    {
    *STEERING_BASE=CAR_STEERING_HALF_DUTY_CYCLE +3;
    }
    if(*CONTROL_SWITCH==14)
    {
    *STEERING_BASE=CAR_STEERING_HALF_DUTY_CYCLE +4;
    }
    if(*CONTROL_SWITCH==30)
    {
    *STEERING_BASE=CAR_STEERING_CLOCKWISE_DUTY_CYCLE;
    }

    //Turning CounterClockwise
    
    if(*CONTROL_SWITCH==3)
    {
    *STEERING_BASE=CAR_STEERING_HALF_DUTY_CYCLE -1;
    }
    if(*CONTROL_SWITCH==7)
    {
    *STEERING_BASE=CAR_STEERING_HALF_DUTY_CYCLE -3;
    }
     if(*CONTROL_SWITCH==15)
    {
    *STEERING_BASE=CAR_STEERING_HALF_DUTY_CYCLE -4;
    }
    if(*CONTROL_SWITCH==31)
    {
    *STEERING_BASE=CAR_STEERING_COUNTER_DUTY_CYCLE;
    }
}
*/
   OSTimeDlyHMSM(0, 0, 0, 20);
      
}//end while loop
        
}//end task 3





/* The main function creates two task and starts multi-tasking */
int main(void)
{
  
Motor_sem = OSSemCreate(1);
Control_sem = OSSemCreate(1);
  
  OSTaskCreateExt(task1,
                  NULL,
                  (void *)&task1_stk[TASK_STACKSIZE-1],
                  TASK1_PRIORITY,
                  TASK1_PRIORITY,
                  task1_stk,
                  TASK_STACKSIZE,
                  NULL,
                  0);
              
  /**/
  OSTaskCreateExt(task2,
                  NULL,
                  (void *)&task2_stk[TASK_STACKSIZE-1],
                  TASK2_PRIORITY,
                  TASK2_PRIORITY,
                  task2_stk,
                  TASK_STACKSIZE,
                  NULL,
                  0);
  /**/
  
  /**/
  OSTaskCreateExt(task3,
                  NULL,
                  (void *)&task3_stk[TASK_STACKSIZE-1],
                  TASK3_PRIORITY,
                  TASK3_PRIORITY,
                  task3_stk,
                  TASK_STACKSIZE,
                  NULL,
                  0); 
                  
 /**/
 
 /**/
 OSTaskCreateExt(task4,
                  NULL,
                  (void *)&task4_stk[TASK_STACKSIZE-1],
                  TASK4_PRIORITY,
                  TASK4_PRIORITY,
                  task4_stk,
                  TASK_STACKSIZE,
                  NULL,
                  0); 
 /**/
  
 /*
 OSTaskCreateExt(task5,
                  NULL,
                  (void *)&task5_stk[TASK_STACKSIZE-1],
                  TASK5_PRIORITY,
                  TASK5_PRIORITY,
                  task5_stk,
                  TASK_STACKSIZE,
                  NULL,
                  0); 
  */
  
  
  RS232_fp = fopen(RS232, "r");
  
  OSStart();
  return 0;
}

/******************************************************************************
*                                                                             *
* License Agreement                                                           *
*                                                                             *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA.           *
* All rights reserved.                                                        *
*                                                                             *
* Permission is hereby granted, free of charge, to any person obtaining a     *
* copy of this software and associated documentation files (the "Software"),  *
* to deal in the Software without restriction, including without limitation   *
* the rights to use, copy, modify, merge, publish, distribute, sublicense,    *
* and/or sell copies of the Software, and to permit persons to whom the       *
* Software is furnished to do so, subject to the following conditions:        *
*                                                                             *
* The above copyright notice and this permission notice shall be included in  *
* all copies or substantial portions of the Software.                         *
*                                                                             *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,    *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING     *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER         *
* DEALINGS IN THE SOFTWARE.                                                   *
*                                                                             *
* This agreement shall be governed in all respects by the laws of the State   *
* of California and by the laws of the United States of America.              *
* Altera does not recommend, suggest or require that this reference design    *
* file be used in conjunction or combination with any other product.          *
******************************************************************************/
