ELEC2645 (2015/16) / Mbed 2 deprecated 2ndYearProject-DistanceSensor

Dependencies:   N5110 SRF02-JEB mbed

introScreen/intro.h

Committer:
ll13j7b
Date:
2016-05-04
Revision:
1:f82359c58eda
Child:
2:01f697b856de

File content as of revision 1:f82359c58eda:


#ifndef INTRO_H
#define INTRO_H


#include "mbed.h"
#include "N5110.h"
#include <bitset>

//Ticker
Ticker update_screen, update_screen2;

volatile bool g_update_screen = 0;
volatile bool g_update_screen_2 = 0;

//Screen buffer
int introBuffer[168][96];

void init_K64F();
void PrintStringToBuffer(const char * str, int x, int y);
void SendBufferToLCD();
void init_serial();
void update_screen_isr();
void printFirstText();
void animateToMenu();
void update_screen2_isr();
void performAnimatedIntro();


void performAnimatedIntro()
{
    update_screen.attach(&update_screen_isr, 0.05);
    update_screen2.attach(&update_screen2_isr, 0.05);

    printFirstText();
    animateToMenu();
}

void update_screen_isr()
{
    g_update_screen = 1;
}

void update_screen2_isr()
{
    g_update_screen_2 = 1;
}

void init_serial()
{
    // set to highest baud - ensure terminal software matches
    pc.baud(115200);
}

void PrintStringToBuffer(const char * str, int x, int y)
{
    //Counts the number of characters in the string
    int char_no = 0;

    ///Only runs while in the bounds of the C-string
    while (*str) {
        //Dereferences the pointer to grab the ASCII value of the current char, offset it by 32 to match the font map and multiply it by 5 as each letter is 5 pixels large
        int font_pos = (*str - 32)*5;
        //Loops through the font map for the selected letter, each letter is 5 pixels large
        for (int i = 0; i < 5; ++i) {
            int x_pixel = x + i + (char_no*6); //Multiply the char no by 6 as each letter has a pixel gap in between

            //Stores bits of the font in the bitset template. Char is 8 bits large
            std::bitset<8> bit_representation (font5x7[font_pos + i]);
            //Loops through the bitset and sets the screen buffer if they're active
            for (int bit = 0; bit < 8; ++bit) {
                if (bit_representation.test(bit)) {
                    introBuffer[x_pixel][y + bit] = 1;
                }
            }
        }

        //Increments the character no and string pointer
        char_no++;
        str++;
    }
}

void SendBufferToLCD()
{
    //Loops through the bitmap that can fit on the screen (84x48) and sets the pixels
    for (int col = 0; col < 84; ++col) {
        for (int row = 0; row < 48; ++row) {
            if (introBuffer[col][row]) {
                lcd.setPixel(col, row);
            }
        }
    }
}

void printFirstText()
{
    char text[] = "Sensor";
    char text1[] = "Project By";
    char text2[] = "James Bruce";
    int y_pos = 50; //Starting y position
    int y_pos1 = 58; //Starting y position
    int y_pos2 = 66; //Starting y position

    while (y_pos > 5) { //Stops when y = 5
        if (g_update_screen) {
            g_update_screen = 0;

            //Clears the screen buffer and the lcd
            memset(introBuffer, 0, sizeof(introBuffer));
            lcd.clear();

            PrintStringToBuffer(text, 3, y_pos);
            PrintStringToBuffer(text1, 3, y_pos1);
            PrintStringToBuffer(text2, 3, y_pos2);
            SendBufferToLCD();

            lcd.refresh();

            y_pos--; //Decrements the y coordinate
            y_pos1--;
            y_pos2--;
        }
    }
    wait(1);
}

void animateToMenu()
{
    char text[] = "MENU";
    char text1[] = "SETTINGS";
    char text2[] = "GRAPH PLOT";
    char text3[] = "NUMERICAL";
    char text4[] = "RADAR MODE";
    
    char oldText[] = "Sensor";
    char oldText1[] = "Project By";
    char oldText2[] = "James Bruce";
    
    int x_pos = 83+30; //Starting x position
    int x_pos1 = 83+17; //Starting x position
    int x_pos2 = 83+12; //Starting x position
    int x_pos3 = 83+16; //Starting x position
    int x_pos4 = 83+12; //Starting x position
    int x = 3; // Old x position 

    while (x_pos1 > 17) { //Stops when x = 0
        if (g_update_screen_2) {
            g_update_screen_2 = 0;

            //Clears the screen buffer and the lcd
            memset(introBuffer, 0, sizeof(introBuffer));
            lcd.clear();

            PrintStringToBuffer(oldText, 3, x+2);
            PrintStringToBuffer(oldText1, 3, x+10);
            PrintStringToBuffer(oldText2, 3, x+18);
            
            PrintStringToBuffer(text, x_pos, 0);
            PrintStringToBuffer(text1, x_pos1, 16);
            PrintStringToBuffer(text2, x_pos2, 24);
            PrintStringToBuffer(text3, x_pos3, 32);
            PrintStringToBuffer(text4, x_pos4, 40);
            SendBufferToLCD();

            lcd.refresh();

            x_pos--; //Decrements the y coordinate
            x_pos1--;
            x_pos2--;
            x_pos3--;
            x_pos4--;
            x++;
            
            if (x > 60) {
                x = 60;   
            }
        }
    }
}

#endif