top of page

/*

 * main.c

 *

 *  Created on: 10/09/2017

 *      Author: Attard

 */

 

/*                         Includes

 * Included header files Must be added to the eclipse includes path

 * This creates the -I/directory/file line in makefile

 * necessary to tell the linker where to look for a header file.

 *

 * Alternatively, the full directory path of the header file

 * (relative to the file Calling the header) can be used

 */

//#include <stdlib.h>

//#include <syst/alt_flash.h>

#include <string.h>

#include <stdio.h>

#include <time.h>

#include <sys/alt_stdio.h>

#include <unistd.h>

#include "altera_avalon_timer_regs.h"

#include "altera_avalon_pio_regs.h"

#include "system.h"

#include "Sketcher.h"

 

// Eclipse Hot-key to Collapse ALL:

//                                [control + shift + Numpad_divide]

// Collapses ALL code blocks and Comment Blocks in CURRENT document.

 

//---------- Ways to initialized memory spaces... ------------------

// char strang[2][10] = {'\0'};

// memset(strang, '\0', sizeof(strang));

 

int main(void)

{

 

//================ Board Initialization ====================================

    printf("Initializing Hardware...\t...\t");

        if(init_board())    // Resets hardware (and VGA_RAM) and Sets control Registers

        {

            perror("Error! Failed To Initialize Board.\n");

        }

 

//================== Memory Initialization ================================

// Initializes Variables in the scope of 'main'

 

    //------------------- Pixel MAP --------------------------------

    screen_t Screen = {'\0'};    // Screen

 

    cursor_t Cursor = {0};            // Cursor

    pen_t Pen = {0};                    // Pen

    //-------------------- 7Seg ------------------------------------

    seg7display_t seg7Display = {0};

    seg7Display.dispMODE = 1;    // Defaults to up/down display

 

    //-------------------- Command Arguments -------------------------

    __cmd_t Command[maxN_cmds];

    alt_u32 ArgX[maxN_cmds] = {0};

    alt_u32 ArgY[maxN_cmds] = {0};

    alt_u32 ArgZ[maxN_cmds] = {0};

    char String[maxN_cmds][MAX_STRLEN] = {'\0'};    // User defined Output string used in Output Text Command

 

    //char DRAW_AGAIN = '\0';

 

//===================== User Prompt ===================================

//================= Get Input Arguments ===============================

while(1)

{

    // Read input string and extract command and argument values

    alt_u8 CMD_n = (getCommands(Command, ArgX, ArgY, ArgZ, String, &Screen, &Pen, &Cursor, &seg7Display));

 

    printf("C:%i  X:%lu  Y:%lu  Z:%lu  S:%s\n", Command[1], ArgX[1], ArgY[1], ArgZ[1], String[1]);

 

    /* Execute Commands [C can have up to 127 Arguments passed into a function]

    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! NOTE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    //    C typedef structs MUST be initialized.

    //    'typedef struct' is passed using 'pass by reference'.

    //    Using 'type *inst_name' in prototype and definition, and passing with '&inst_name'

    //    before passing into function as arguments */

    executeCommands(CMD_n, Command, ArgX, ArgY, ArgZ, String, &Screen, &Pen, &Cursor, &seg7Display);

 

    // Prompt User to Begin new Sketch?

    printf("\n==============================================================================================================================================\n");

    printf("Sketch Complete!\nDraw Again?\n");

    usleep(250000);

    //printf("Sketch Complete!\nDraw Again? [y/n] : ");

    //scanf("%c",&DRAW_AGAIN);

    //if(DRAW_AGAIN == 'n')

    //{

    //    break;

    //}

 

} //end_while

 

    // Exit Program

    printf("\n\nThank you for choosing Sketcher!\n\tGOODBYE!!!");

    return 0;

}

/*

 * sketcher.h

 *

 *  Created on: 09/09/2017

 *      Author: Attard

 */

 

#ifndef SKETCHER_H_

#define SKETCHER_H_

 

//================== Debugging ============================

#define DEBUG_MODE     0    // set to '1' for Debug Mode Messages

#define VERBOSE        1    // set to '1' for Verbose Mode Messages

 

//============= Parameter Definitions =====================

// Colors [B,G,R]

#define BLU    0x00F

#define GRN       0x0F0

#define RED    0xF00

#define BLK    0x0

#define WHT    0xFFF

 

#define PINK    0xF0F

#define YEL       0x0FF

#define LGRN   0xFF0

 

// Limits

#define maxN_cmds 255

#define MAX_STRLEN 20

 

// Screen Parameters

#define Xwidth 256

#define Ywidth 256

#define startX 0    // Implemented in Hardware

#define startY 0

#define MIDDLEofSCREEN (Xwidth*((Ywidth/2)-1)+((Ywidth/2)-1))

#define BL_CORNER (Xwidth*(Ywidth-1))

#define BR_CORNER (Xwidth*(Ywidth-1)) + (Xwidth-1)

#define TR_CORNER (Xwidth-1) // Screen Address Offset by 3 to account for add_tr HW error

#define TL_CORNER 0

 

// Conversion Factors

#define asciiZero 48

#define asciiNine 57

#define asciiSpace 38

 

// Software Version

#define V_major 1

#define V_minor 0

 

typedef struct

{

    alt_u8 one;

    alt_u8 two;

}test_t;

 

void Ftest(test_t *test);

 

//=========== Pixel Map ====================

typedef struct {

    //alt_u16 Pixel[Xwidth][Ywidth];

    alt_u16 X[Xwidth];

    alt_u16 Y[Ywidth];

    alt_u16 Pixel[((Xwidth*Ywidth)-1)];

}screen_t;

 

//========= Data Structures ================

// Pen INFO for seg7

typedef struct {

    alt_u8 penUpDown[2];    // 2 character display for 'up' || 'dn'

    alt_u8 BYTE[6];        // 6 character display for pen position

    alt_u8 dispMODE;    // Display mode (up_down[0] or position[1])

}seg7display_t;

 

// User Commands

typedef enum{

    BI_cmd = 0,

    CS_cmd,

    PU_cmd,

    PD_cmd,

    PC_cmd,

    PA_cmd,

    EI_cmd,

    MV_cmd,

    OT_cmd,

}__cmd_t;

 

// Cursor/Pen Direction

typedef enum{

    right = 0,

    right_up = 45,

    up = 90,

    up_left = 135,

    left = 180,

    down_left = 225,

    down = 270,

    down_right = 315,

}angle_t;

 

 

//============ CURSOR ================

typedef struct{

    angle_t Angle;

    alt_u16 Coulor;

    alt_u16 Image;

    alt_u32 Position;

}cursor_t;

 

//=========== Pen ====================

typedef struct{

    alt_u8 is_down;

    alt_u16 Colour;

    alt_u16 Weight;

    angle_t Angle;

    alt_u32 Position;

    alt_u8 X;    // X,Y start at zero (top left) of drawing area

    alt_u8 Y;

}pen_t;

 

//TODO WriteChar

//TODO WriteString

//TODO TEST Display Cursor

 

//========================== cmd_Functions ==================================

//    All Project Defined _cmd Functions

//

//    ***NOTE**** typedef structs are implicitly turned into pointers

//    (like arrays) when passed into a function.

//

//    Can be passed using this feature

//            ie; proto: function(data_t Data);          and         Call: function(Data);    and        to_access_from_function: Data.element

//

//    If typedef struct is passed to a function via an extra pass by reference,

//            ie; proto: function(data_t *Data);    and        call: function(&data);    and        to_access_from_function: Data->element

//===========================================================================

//alt_u8 getCommands(__cmd_t *Command, alt_u32 ArgX[maxN_cmds], alt_u32 ArgY[maxN_cmds], alt_u32 ArgZ[maxN_cmds], char string[maxN_cmds][MAX_STRLEN]);                                // Read input string and extract command and argument values

alt_u8 getCommands(__cmd_t *Command, alt_u32 *ArgX, alt_u32 *ArgY, alt_u32 *ArgZ, char string[maxN_cmds][MAX_STRLEN], screen_t *Screen, pen_t *Pen, cursor_t *Cursor, seg7display_t *seg7Display);

alt_u8 executeCommands(alt_u8 CMD_n, __cmd_t *Command, alt_u32 *ArgX, alt_u32 *ArgY, alt_u32 *ArgZ, char String[maxN_cmds][MAX_STRLEN], screen_t *Screen, pen_t *Pen, cursor_t *Cursor, seg7display_t *seg7Display);

 

alt_u8 BI(screen_t *screen, cursor_t *cursor, pen_t *pen);                        // BEGIN INITIALIAZATION Sets; screen to black, cursor position and angle, 'Initialized message'

alt_u8 CS(screen_t *screen, alt_u16 colour, pen_t *pen, cursor_t *cursor);        // CLEAR SCREEN Sets; reset screen color, pen is_up, pen = white

alt_u8 PU(pen_t *Pen);                                                        // PEN UP

alt_u8 PD(pen_t *pen, screen_t *screen);                                        // PEN DOWN

alt_u8 PC(pen_t *pen, alt_u16 colour, screen_t *screen);                        // PEN COLOR

alt_u8 PA(pen_t *pen, cursor_t *cursor, alt_u16 angle);                        // PEN ANGLE right (=>0, up =>90)

alt_u8 EI(void);                                                            // END INSTRUCTIONS

alt_u8 MV(pen_t *pen, cursor_t *cursor, alt_u16 distance, screen_t *screen);    // MOVE PEN

alt_u8 OT(alt_u32 address, char string[MAX_STRLEN][MAX_STRLEN], screen_t *Screen);        // OUTPUT TEXT Write 'usr_String' at x,y

 

 

//========================== Other Functions =================================

//    Misc Software Functions

//===================================================================================

alt_u8 echo_char(void);

alt_u8 echo_str(void);

alt_u8 init_board(void);

alt_u16 RGB_ColourConcatonation(alt_u32 blue, alt_u32 green, alt_u32 red);

alt_u32 getIntArg(char *usr_String, alt_u8 *character_n);

alt_u8 getCharArg(char *usr_String, alt_u8 *character_n, char * string);

alt_u32 XY_2Address(alt_u32 x, alt_u32 y);

alt_u8 Range_0to15(alt_u32 *Arg);

 

 

//======================= Cursor Functions ==================================

//    Cursor Writing Functions.

//    These Functions write a 'colour' Cursor image of 'Cursor.Angle'

//    at 'Cursor.Position'

//

//============================================================================

alt_u8 DisplayCursor(cursor_t *cursor, alt_u16 colour);

alt_u8 cursor0(alt_u32 CursorPos, alt_u16 colour);

alt_u8 cursor45(alt_u32 CursorPos, alt_u16 colour);

alt_u8 cursor90(alt_u32 CursorPos, alt_u16 colour);

alt_u8 cursor135(alt_u32 CursorPos, alt_u16 colour);

alt_u8 cursor180(alt_u32 CursorPos, alt_u16 colour);

alt_u8 cursor225(alt_u32 CursorPos, alt_u16 colour);

alt_u8 cursor270(alt_u32 CursorPos, alt_u16 colour);

alt_u8 cursor315(alt_u32 CursorPos, alt_u16 colour);

 

 

 

 

//======================= Screen Functions ==================================

//     All Functions That write to the alias buffer 'Screen'

// 'Screen' is of data type 'screen_t'

//    and contains a single dimensional Array 'Pixel[0->65532]'

//===========================================================================

alt_u8 Write_Pixel_toScreen(alt_u32 address, alt_u16 colour, screen_t *screen);

alt_u8 ClearScreen(screen_t *Screen, alt_u16 colour);

 

 

 

 

 

//=================== VGA FUNCTIONS =========================================

// All functions that write to the VGA RAM directly

//===========================================================================

alt_u8 Write_Pixel_toVGA(alt_u8 x, alt_u8 y, alt_u16 colour);

alt_u8 VGA_TEST(void);

alt_u8 DisplayScreen(screen_t *Screen);

alt_u8 ClearVGA(alt_u16 colour);

alt_u8 VGA_Write(alt_u32 address, alt_u16 colour);

 

 

 

 

 

//============================ 7 Segment Display =============================

//    7 Segment Display Functions for DE0 CV DEV board.

//============================================================================

alt_u8 Update7Seg(seg7display_t *seg7Display, pen_t *Pen);

alt_u8 HelloFrom7seg(void);

 

 

#endif /* SKETCHER_H_ */

bottom of page