/*************************************************************************
* 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 <stdlib.h> // abs()
#include <io.h>
#include "includes.h"
#include "main.h"

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

/* Definition of Task Priorities */

#define TASK1_PRIORITY      1
#define TASK2_PRIORITY      2

/* Prints "Hello World" and sleeps for three seconds */
void task1(void* pdata)
{
  while (1)
  { 
    detectMotion();
    
    // Increment Buffer
    IOWR_32DIRECT(FRAME_ADDRESSING_0_BASE, 0, 1);
    IOWR_32DIRECT(FRAME_ADDRESSING_0_BASE, 0, 0);
    
    // 50 = 8 fps, 70 = 5 fps
    OSTimeDlyHMSM(0, 0, 0, 50); // The program can do much better if removed (limited SDRAM timing)
  }
}

void detectMotion()
{
    /* Flow control variables */
    int row_index = 0, col_index = 0, difference = 0, pixelErrorCount = 0;
    int sub_difference = 0;
    int j = 0,k = 0;
    int boxvalue_row = 0, boxvalue_col = 0;
    
    /* The Box Array for determining failed image segments */
    char boxValues[HORIZONTAL_BOXES][VERTICAL_BOXES];
    
    /* Address values for SDRAM */
    int addr = SDRAM_0_BASE+IORD_32DIRECT(FRAME_ADDRESSING_0_BASE, 0);
    int background_addr = SDRAM_0_BASE+IORD_32DIRECT(FRAME_ADDRESSING_0_BASE, 4);
    int column_main_addr;
    int column_back_addr;
    
    /* Detect Motion */
    while(row_index < ROWS)
    {
        col_index=0;
        
        while(col_index < COLUMNS)
        {
            pixelErrorCount=0;
            
            for(k = col_index; k < BOX_COLUMNS+col_index; k++)
            {
                column_main_addr = addr + k*512;
                column_back_addr = background_addr + k*512;
                
                /* Currently checking double pixels - might change to single */
                for(j = row_index; j < BOX_ROWS+row_index; j+=2) 
                {                    
                    difference = abs(IORD_16DIRECT(column_back_addr+j, 0) - IORD_16DIRECT(column_main_addr+j, 0));
                    
                    //sub_difference = difference / 256;
                    
                    if (difference > PIXEL_DIFFERENCE_THRESHOLD)
                    {
                        pixelErrorCount++;
                    }
                    
                    //sub_difference = difference % 256;
                    
                    //if (sub_difference > PIXEL_DIFFERENCE_THRESHOLD)
                    //{
                    //    pixelErrorCount++;   
                    //}
                }
            }
            
            /* If enough pixels failed, flag the box as failed */
            boxvalue_row = (((j-1) - (j%BOX_ROWS))/BOX_ROWS);
            boxvalue_col = (((k-1) - (k%BOX_COLUMNS))/BOX_COLUMNS);
            
            if(pixelErrorCount > ERROR_PERCENTAGE_THRESHOLD)
            {
                boxValues[boxvalue_row][boxvalue_col] = 1;
            }
            else
            {
                boxValues[boxvalue_row][boxvalue_col] = 0;
            }
            
            col_index+=BOX_COLUMNS;
        }
        
        row_index+=BOX_ROWS;
    }
    
    /* Print box - testing */
    /*for (j = 0; j < VERTICAL_BOXES; j++)
    {
        for (k = 0; k < HORIZONTAL_BOXES; k++)
        {
            printf ("%d ", boxValues[k][j]);
        }
        printf ("\n");
    }
    printf ("<=====================>\n");*/
    
    /* Set up the box(es) */
    int x,y,i;
    
    for(x=0; x<HORIZONTAL_BOXES; x++)
    {
        for(y=0; y<VERTICAL_BOXES; y++)
        {
            if(boxValues[x][y] == (char) 1)
            {
                if((y==0) || (boxValues[x][y-1] != (char) 1))
                {
                    column_main_addr = addr + y*BOX_COLUMNS*512;
                    
                    //outline the top border
                    for(i=x*BOX_ROWS; i<(x*BOX_ROWS+BOX_ROWS); i+=2)
                    {
                        IOWR_16DIRECT(column_main_addr+i, 0, 65535);
                    }
                }
                if(y == VERTICAL_BOXES-1)
                {
                    column_main_addr = addr + (y*BOX_COLUMNS+BOX_COLUMNS-2)*512;
                    
                    //outline the bottom border
                    for(i=x*BOX_ROWS; i<(x*BOX_ROWS+BOX_ROWS); i+=2)
                    {
                        IOWR_16DIRECT(column_main_addr+i, 0, 65535);
                    }                    
                }
                else if(boxValues[x][y+1] != (char) 1)
                {
                    column_main_addr = addr + (y*BOX_COLUMNS+BOX_COLUMNS-1)*512;
                    
                    //outline the bottom border
                    for(i=x*BOX_ROWS; i<(x*BOX_ROWS+BOX_ROWS); i+=2)
                    {
                        IOWR_16DIRECT(column_main_addr+i, 0, 65535);
                    }
                }
                if((x==0) || (boxValues[x-1][y] != (char) 1))
                {
                    column_main_addr = addr + x*BOX_ROWS;
                    
                    //outline the left border
                    for(j=y*BOX_COLUMNS; j<(y*BOX_COLUMNS+BOX_COLUMNS); j++)
                    {
                        IOWR_16DIRECT(column_main_addr+j*512, 0, 65279); // Was 65535
                    }
                }
                if((x==HORIZONTAL_BOXES-1) || (boxValues[x+1][y] != (char) 1))
                {
                    column_main_addr = addr + ((x*BOX_ROWS)+BOX_ROWS-1);
                    
                    //outline the right border
                    for(j=y*BOX_COLUMNS; j<(y*BOX_COLUMNS+BOX_COLUMNS); j++)
                    {
                        IOWR_16DIRECT(column_main_addr+j*512, 0, 65535);
                    }
                }
            }
        }
    }
}

/* The main function creates two task and starts multi-tasking */
int main(void)
{
  
  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);*/
  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.          *
******************************************************************************/
