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

Dependencies:   N5110 SRF02-JEB mbed

Committer:
ll13j7b
Date:
Wed May 04 21:20:21 2016 +0000
Revision:
1:f82359c58eda
Child:
2:01f697b856de
working version before adding classes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ll13j7b 1:f82359c58eda 1
ll13j7b 1:f82359c58eda 2 #ifndef INTRO_H
ll13j7b 1:f82359c58eda 3 #define INTRO_H
ll13j7b 1:f82359c58eda 4
ll13j7b 1:f82359c58eda 5
ll13j7b 1:f82359c58eda 6 #include "mbed.h"
ll13j7b 1:f82359c58eda 7 #include "N5110.h"
ll13j7b 1:f82359c58eda 8 #include <bitset>
ll13j7b 1:f82359c58eda 9
ll13j7b 1:f82359c58eda 10 //Ticker
ll13j7b 1:f82359c58eda 11 Ticker update_screen, update_screen2;
ll13j7b 1:f82359c58eda 12
ll13j7b 1:f82359c58eda 13 volatile bool g_update_screen = 0;
ll13j7b 1:f82359c58eda 14 volatile bool g_update_screen_2 = 0;
ll13j7b 1:f82359c58eda 15
ll13j7b 1:f82359c58eda 16 //Screen buffer
ll13j7b 1:f82359c58eda 17 int introBuffer[168][96];
ll13j7b 1:f82359c58eda 18
ll13j7b 1:f82359c58eda 19 void init_K64F();
ll13j7b 1:f82359c58eda 20 void PrintStringToBuffer(const char * str, int x, int y);
ll13j7b 1:f82359c58eda 21 void SendBufferToLCD();
ll13j7b 1:f82359c58eda 22 void init_serial();
ll13j7b 1:f82359c58eda 23 void update_screen_isr();
ll13j7b 1:f82359c58eda 24 void printFirstText();
ll13j7b 1:f82359c58eda 25 void animateToMenu();
ll13j7b 1:f82359c58eda 26 void update_screen2_isr();
ll13j7b 1:f82359c58eda 27 void performAnimatedIntro();
ll13j7b 1:f82359c58eda 28
ll13j7b 1:f82359c58eda 29
ll13j7b 1:f82359c58eda 30 void performAnimatedIntro()
ll13j7b 1:f82359c58eda 31 {
ll13j7b 1:f82359c58eda 32 update_screen.attach(&update_screen_isr, 0.05);
ll13j7b 1:f82359c58eda 33 update_screen2.attach(&update_screen2_isr, 0.05);
ll13j7b 1:f82359c58eda 34
ll13j7b 1:f82359c58eda 35 printFirstText();
ll13j7b 1:f82359c58eda 36 animateToMenu();
ll13j7b 1:f82359c58eda 37 }
ll13j7b 1:f82359c58eda 38
ll13j7b 1:f82359c58eda 39 void update_screen_isr()
ll13j7b 1:f82359c58eda 40 {
ll13j7b 1:f82359c58eda 41 g_update_screen = 1;
ll13j7b 1:f82359c58eda 42 }
ll13j7b 1:f82359c58eda 43
ll13j7b 1:f82359c58eda 44 void update_screen2_isr()
ll13j7b 1:f82359c58eda 45 {
ll13j7b 1:f82359c58eda 46 g_update_screen_2 = 1;
ll13j7b 1:f82359c58eda 47 }
ll13j7b 1:f82359c58eda 48
ll13j7b 1:f82359c58eda 49 void init_serial()
ll13j7b 1:f82359c58eda 50 {
ll13j7b 1:f82359c58eda 51 // set to highest baud - ensure terminal software matches
ll13j7b 1:f82359c58eda 52 pc.baud(115200);
ll13j7b 1:f82359c58eda 53 }
ll13j7b 1:f82359c58eda 54
ll13j7b 1:f82359c58eda 55 void PrintStringToBuffer(const char * str, int x, int y)
ll13j7b 1:f82359c58eda 56 {
ll13j7b 1:f82359c58eda 57 //Counts the number of characters in the string
ll13j7b 1:f82359c58eda 58 int char_no = 0;
ll13j7b 1:f82359c58eda 59
ll13j7b 1:f82359c58eda 60 ///Only runs while in the bounds of the C-string
ll13j7b 1:f82359c58eda 61 while (*str) {
ll13j7b 1:f82359c58eda 62 //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
ll13j7b 1:f82359c58eda 63 int font_pos = (*str - 32)*5;
ll13j7b 1:f82359c58eda 64 //Loops through the font map for the selected letter, each letter is 5 pixels large
ll13j7b 1:f82359c58eda 65 for (int i = 0; i < 5; ++i) {
ll13j7b 1:f82359c58eda 66 int x_pixel = x + i + (char_no*6); //Multiply the char no by 6 as each letter has a pixel gap in between
ll13j7b 1:f82359c58eda 67
ll13j7b 1:f82359c58eda 68 //Stores bits of the font in the bitset template. Char is 8 bits large
ll13j7b 1:f82359c58eda 69 std::bitset<8> bit_representation (font5x7[font_pos + i]);
ll13j7b 1:f82359c58eda 70 //Loops through the bitset and sets the screen buffer if they're active
ll13j7b 1:f82359c58eda 71 for (int bit = 0; bit < 8; ++bit) {
ll13j7b 1:f82359c58eda 72 if (bit_representation.test(bit)) {
ll13j7b 1:f82359c58eda 73 introBuffer[x_pixel][y + bit] = 1;
ll13j7b 1:f82359c58eda 74 }
ll13j7b 1:f82359c58eda 75 }
ll13j7b 1:f82359c58eda 76 }
ll13j7b 1:f82359c58eda 77
ll13j7b 1:f82359c58eda 78 //Increments the character no and string pointer
ll13j7b 1:f82359c58eda 79 char_no++;
ll13j7b 1:f82359c58eda 80 str++;
ll13j7b 1:f82359c58eda 81 }
ll13j7b 1:f82359c58eda 82 }
ll13j7b 1:f82359c58eda 83
ll13j7b 1:f82359c58eda 84 void SendBufferToLCD()
ll13j7b 1:f82359c58eda 85 {
ll13j7b 1:f82359c58eda 86 //Loops through the bitmap that can fit on the screen (84x48) and sets the pixels
ll13j7b 1:f82359c58eda 87 for (int col = 0; col < 84; ++col) {
ll13j7b 1:f82359c58eda 88 for (int row = 0; row < 48; ++row) {
ll13j7b 1:f82359c58eda 89 if (introBuffer[col][row]) {
ll13j7b 1:f82359c58eda 90 lcd.setPixel(col, row);
ll13j7b 1:f82359c58eda 91 }
ll13j7b 1:f82359c58eda 92 }
ll13j7b 1:f82359c58eda 93 }
ll13j7b 1:f82359c58eda 94 }
ll13j7b 1:f82359c58eda 95
ll13j7b 1:f82359c58eda 96 void printFirstText()
ll13j7b 1:f82359c58eda 97 {
ll13j7b 1:f82359c58eda 98 char text[] = "Sensor";
ll13j7b 1:f82359c58eda 99 char text1[] = "Project By";
ll13j7b 1:f82359c58eda 100 char text2[] = "James Bruce";
ll13j7b 1:f82359c58eda 101 int y_pos = 50; //Starting y position
ll13j7b 1:f82359c58eda 102 int y_pos1 = 58; //Starting y position
ll13j7b 1:f82359c58eda 103 int y_pos2 = 66; //Starting y position
ll13j7b 1:f82359c58eda 104
ll13j7b 1:f82359c58eda 105 while (y_pos > 5) { //Stops when y = 5
ll13j7b 1:f82359c58eda 106 if (g_update_screen) {
ll13j7b 1:f82359c58eda 107 g_update_screen = 0;
ll13j7b 1:f82359c58eda 108
ll13j7b 1:f82359c58eda 109 //Clears the screen buffer and the lcd
ll13j7b 1:f82359c58eda 110 memset(introBuffer, 0, sizeof(introBuffer));
ll13j7b 1:f82359c58eda 111 lcd.clear();
ll13j7b 1:f82359c58eda 112
ll13j7b 1:f82359c58eda 113 PrintStringToBuffer(text, 3, y_pos);
ll13j7b 1:f82359c58eda 114 PrintStringToBuffer(text1, 3, y_pos1);
ll13j7b 1:f82359c58eda 115 PrintStringToBuffer(text2, 3, y_pos2);
ll13j7b 1:f82359c58eda 116 SendBufferToLCD();
ll13j7b 1:f82359c58eda 117
ll13j7b 1:f82359c58eda 118 lcd.refresh();
ll13j7b 1:f82359c58eda 119
ll13j7b 1:f82359c58eda 120 y_pos--; //Decrements the y coordinate
ll13j7b 1:f82359c58eda 121 y_pos1--;
ll13j7b 1:f82359c58eda 122 y_pos2--;
ll13j7b 1:f82359c58eda 123 }
ll13j7b 1:f82359c58eda 124 }
ll13j7b 1:f82359c58eda 125 wait(1);
ll13j7b 1:f82359c58eda 126 }
ll13j7b 1:f82359c58eda 127
ll13j7b 1:f82359c58eda 128 void animateToMenu()
ll13j7b 1:f82359c58eda 129 {
ll13j7b 1:f82359c58eda 130 char text[] = "MENU";
ll13j7b 1:f82359c58eda 131 char text1[] = "SETTINGS";
ll13j7b 1:f82359c58eda 132 char text2[] = "GRAPH PLOT";
ll13j7b 1:f82359c58eda 133 char text3[] = "NUMERICAL";
ll13j7b 1:f82359c58eda 134 char text4[] = "RADAR MODE";
ll13j7b 1:f82359c58eda 135
ll13j7b 1:f82359c58eda 136 char oldText[] = "Sensor";
ll13j7b 1:f82359c58eda 137 char oldText1[] = "Project By";
ll13j7b 1:f82359c58eda 138 char oldText2[] = "James Bruce";
ll13j7b 1:f82359c58eda 139
ll13j7b 1:f82359c58eda 140 int x_pos = 83+30; //Starting x position
ll13j7b 1:f82359c58eda 141 int x_pos1 = 83+17; //Starting x position
ll13j7b 1:f82359c58eda 142 int x_pos2 = 83+12; //Starting x position
ll13j7b 1:f82359c58eda 143 int x_pos3 = 83+16; //Starting x position
ll13j7b 1:f82359c58eda 144 int x_pos4 = 83+12; //Starting x position
ll13j7b 1:f82359c58eda 145 int x = 3; // Old x position
ll13j7b 1:f82359c58eda 146
ll13j7b 1:f82359c58eda 147 while (x_pos1 > 17) { //Stops when x = 0
ll13j7b 1:f82359c58eda 148 if (g_update_screen_2) {
ll13j7b 1:f82359c58eda 149 g_update_screen_2 = 0;
ll13j7b 1:f82359c58eda 150
ll13j7b 1:f82359c58eda 151 //Clears the screen buffer and the lcd
ll13j7b 1:f82359c58eda 152 memset(introBuffer, 0, sizeof(introBuffer));
ll13j7b 1:f82359c58eda 153 lcd.clear();
ll13j7b 1:f82359c58eda 154
ll13j7b 1:f82359c58eda 155 PrintStringToBuffer(oldText, 3, x+2);
ll13j7b 1:f82359c58eda 156 PrintStringToBuffer(oldText1, 3, x+10);
ll13j7b 1:f82359c58eda 157 PrintStringToBuffer(oldText2, 3, x+18);
ll13j7b 1:f82359c58eda 158
ll13j7b 1:f82359c58eda 159 PrintStringToBuffer(text, x_pos, 0);
ll13j7b 1:f82359c58eda 160 PrintStringToBuffer(text1, x_pos1, 16);
ll13j7b 1:f82359c58eda 161 PrintStringToBuffer(text2, x_pos2, 24);
ll13j7b 1:f82359c58eda 162 PrintStringToBuffer(text3, x_pos3, 32);
ll13j7b 1:f82359c58eda 163 PrintStringToBuffer(text4, x_pos4, 40);
ll13j7b 1:f82359c58eda 164 SendBufferToLCD();
ll13j7b 1:f82359c58eda 165
ll13j7b 1:f82359c58eda 166 lcd.refresh();
ll13j7b 1:f82359c58eda 167
ll13j7b 1:f82359c58eda 168 x_pos--; //Decrements the y coordinate
ll13j7b 1:f82359c58eda 169 x_pos1--;
ll13j7b 1:f82359c58eda 170 x_pos2--;
ll13j7b 1:f82359c58eda 171 x_pos3--;
ll13j7b 1:f82359c58eda 172 x_pos4--;
ll13j7b 1:f82359c58eda 173 x++;
ll13j7b 1:f82359c58eda 174
ll13j7b 1:f82359c58eda 175 if (x > 60) {
ll13j7b 1:f82359c58eda 176 x = 60;
ll13j7b 1:f82359c58eda 177 }
ll13j7b 1:f82359c58eda 178 }
ll13j7b 1:f82359c58eda 179 }
ll13j7b 1:f82359c58eda 180 }
ll13j7b 1:f82359c58eda 181
ll13j7b 1:f82359c58eda 182 #endif