C++ Library for the PsiSwarm Robot - Version 0.8

Dependents:   PsiSwarm_V8_Blank_CPP Autonomia_RndmWlk

Fork of PsiSwarmV7_CPP by Psi Swarm Robot

Committer:
jah128
Date:
Fri Mar 11 13:51:24 2016 +0000
Revision:
2:c6986ee3c7c5
Parent:
0:d6269d17c8cf
Child:
5:3cdd1a37cdd7
Added Alex' IR functions

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jah128 0:d6269d17c8cf 1 /* University of York Robotics Laboratory PsiSwarm Library: Display Driver Source File
jah128 0:d6269d17c8cf 2 *
jah128 0:d6269d17c8cf 3 * File: display.cpp
jah128 0:d6269d17c8cf 4 *
jah128 0:d6269d17c8cf 5 * (C) Dept. Electronics & Computer Science, University of York
jah128 0:d6269d17c8cf 6 *
jah128 0:d6269d17c8cf 7 * James Hilder, Alan Millard, Alexander Horsfield, Homero Elizondo, Jon Timmis
jah128 0:d6269d17c8cf 8 *
jah128 2:c6986ee3c7c5 9 * PsiSwarm Library Version: 0.41
jah128 0:d6269d17c8cf 10 *
jah128 2:c6986ee3c7c5 11 * March 2016
jah128 0:d6269d17c8cf 12 *
jah128 0:d6269d17c8cf 13 * Driver for the Midas 16x2 I2C LCD Display (MCCOG21605x6W) LCD
jah128 0:d6269d17c8cf 14 * [Farnell part 2218942 or 2063206]
jah128 0:d6269d17c8cf 15 *
jah128 0:d6269d17c8cf 16 */
jah128 0:d6269d17c8cf 17
jah128 0:d6269d17c8cf 18 #include "psiswarm.h"
jah128 0:d6269d17c8cf 19
jah128 0:d6269d17c8cf 20 Timeout init_timeout;
jah128 0:d6269d17c8cf 21 Timeout backlight_timeout;
jah128 0:d6269d17c8cf 22 Timeout debug_timeout;
jah128 0:d6269d17c8cf 23 int backlight_on_time;
jah128 0:d6269d17c8cf 24 int backlight_off_time;
jah128 0:d6269d17c8cf 25 char backlight_step;
jah128 0:d6269d17c8cf 26 char multipage[200];
jah128 0:d6269d17c8cf 27 char multipage_length = 0;
jah128 0:d6269d17c8cf 28 char preserve_line_1 [17];
jah128 0:d6269d17c8cf 29 char preserve_line_2 [17];
jah128 0:d6269d17c8cf 30 char c_row=0;
jah128 0:d6269d17c8cf 31 char c_column=0;
jah128 0:d6269d17c8cf 32 char p_row;
jah128 0:d6269d17c8cf 33 char p_column;
jah128 0:d6269d17c8cf 34
jah128 0:d6269d17c8cf 35
jah128 0:d6269d17c8cf 36 Display::Display(PinName sda, PinName scl, PinName reset, PinName backlight) : Stream("display"), _i2c(sda,scl), _reset(reset), _backlight(backlight) {
jah128 0:d6269d17c8cf 37 }
jah128 0:d6269d17c8cf 38
jah128 0:d6269d17c8cf 39 Display::Display() : Stream("display"), _i2c(p28,p27), _reset(p29), _backlight(p30) {
jah128 0:d6269d17c8cf 40 }
jah128 0:d6269d17c8cf 41
jah128 0:d6269d17c8cf 42 int Display::i2c_message(char byte){
jah128 0:d6269d17c8cf 43 char bytes [2];
jah128 0:d6269d17c8cf 44 bytes[0]=0x80;
jah128 0:d6269d17c8cf 45 bytes[1]=byte;
jah128 0:d6269d17c8cf 46 int ret=_i2c.write(LCD_ADDRESS,bytes,2);
jah128 0:d6269d17c8cf 47 wait(0.01);
jah128 0:d6269d17c8cf 48 return ret;
jah128 0:d6269d17c8cf 49 }
jah128 0:d6269d17c8cf 50
jah128 0:d6269d17c8cf 51 int Display::disp_putc(int c){
jah128 0:d6269d17c8cf 52 char message [2];
jah128 0:d6269d17c8cf 53 message[0]=0x40;
jah128 0:d6269d17c8cf 54 message[1]=c;
jah128 0:d6269d17c8cf 55 _i2c.write(LCD_ADDRESS,message,2);
jah128 0:d6269d17c8cf 56 wait(0.01);
jah128 0:d6269d17c8cf 57 return c;
jah128 0:d6269d17c8cf 58 }
jah128 0:d6269d17c8cf 59
jah128 0:d6269d17c8cf 60 void Display::init_display(char mode){
jah128 0:d6269d17c8cf 61 //Set initial states: display on, cursor off
jah128 0:d6269d17c8cf 62 display_on = 1;
jah128 0:d6269d17c8cf 63 set_backlight_brightness(1);
jah128 0:d6269d17c8cf 64 cursor_on = 0;
jah128 0:d6269d17c8cf 65 blink_on = 0;
jah128 0:d6269d17c8cf 66
jah128 0:d6269d17c8cf 67 _reset=1;
jah128 0:d6269d17c8cf 68 wait(0.02);
jah128 0:d6269d17c8cf 69 //Set reset low
jah128 0:d6269d17c8cf 70 _reset=0;
jah128 0:d6269d17c8cf 71 wait(0.001);
jah128 0:d6269d17c8cf 72 _reset=1;
jah128 0:d6269d17c8cf 73 wait(0.03);
jah128 0:d6269d17c8cf 74 i2c_message(0x38);
jah128 0:d6269d17c8cf 75 i2c_message(0x39);
jah128 0:d6269d17c8cf 76 i2c_message(0x14);
jah128 0:d6269d17c8cf 77 i2c_message(0x74);
jah128 0:d6269d17c8cf 78 i2c_message(0x54);
jah128 0:d6269d17c8cf 79 i2c_message(0x6F);
jah128 0:d6269d17c8cf 80 _set_display();
jah128 0:d6269d17c8cf 81 clear_display();
jah128 0:d6269d17c8cf 82 char psis[17];
jah128 0:d6269d17c8cf 83 for(int i=0;i<16;i++){
jah128 0:d6269d17c8cf 84 psis[i]=0x1D;
jah128 0:d6269d17c8cf 85 }
jah128 0:d6269d17c8cf 86 set_position(0,0);
jah128 0:d6269d17c8cf 87 write_string(psis,16);
jah128 0:d6269d17c8cf 88 set_position(1,0);
jah128 0:d6269d17c8cf 89 write_string(psis,16);
jah128 0:d6269d17c8cf 90 wait(0.25);
jah128 0:d6269d17c8cf 91 clear_display();
jah128 0:d6269d17c8cf 92 if(mode == 0){
jah128 0:d6269d17c8cf 93 set_position(0,0);
jah128 0:d6269d17c8cf 94 write_string(" YORK ROBOTICS");
jah128 0:d6269d17c8cf 95 set_position(1,0);
jah128 0:d6269d17c8cf 96 write_string(" LABORATORY");
jah128 0:d6269d17c8cf 97 init_timeout.attach(this,&Display::post_init,0.3);}
jah128 0:d6269d17c8cf 98 else {
jah128 0:d6269d17c8cf 99 set_position(0,0);
jah128 0:d6269d17c8cf 100 write_string("Hold button to");
jah128 0:d6269d17c8cf 101 set_position(1,0);
jah128 0:d6269d17c8cf 102 write_string("launch demo code");
jah128 0:d6269d17c8cf 103 }
jah128 0:d6269d17c8cf 104 }
jah128 0:d6269d17c8cf 105
jah128 0:d6269d17c8cf 106 void Display::post_init(){
jah128 0:d6269d17c8cf 107 clear_display();
jah128 0:d6269d17c8cf 108 home();
jah128 0:d6269d17c8cf 109 write_string("PSI SWARM ROBOT");
jah128 0:d6269d17c8cf 110 set_position(1,0);
jah128 0:d6269d17c8cf 111 char line [17];
jah128 0:d6269d17c8cf 112 sprintf(line,"VERSION %1.2f", SOFTWARE_VERSION_CODE );
jah128 0:d6269d17c8cf 113 set_position(1,0);
jah128 0:d6269d17c8cf 114 write_string(line);
jah128 0:d6269d17c8cf 115 init_timeout.attach(this,&Display::post_post_init,0.3);
jah128 0:d6269d17c8cf 116 }
jah128 0:d6269d17c8cf 117
jah128 0:d6269d17c8cf 118 void Display::post_post_init(){
jah128 0:d6269d17c8cf 119 clear_display();
jah128 0:d6269d17c8cf 120 home();
jah128 0:d6269d17c8cf 121 }
jah128 0:d6269d17c8cf 122
jah128 0:d6269d17c8cf 123 void Display::write_string(char * message){
jah128 0:d6269d17c8cf 124 size_t length = strlen(message);
jah128 0:d6269d17c8cf 125 if (length > 16) length = 16;
jah128 0:d6269d17c8cf 126 char to_send [length+1];
jah128 0:d6269d17c8cf 127 to_send[0]=0x40;
jah128 0:d6269d17c8cf 128 for(int i=0;i<length;i++){
jah128 0:d6269d17c8cf 129 to_send[i+1] = message[i];
jah128 0:d6269d17c8cf 130 }
jah128 0:d6269d17c8cf 131 _i2c.write(LCD_ADDRESS,to_send,length+1);
jah128 0:d6269d17c8cf 132 // Add to saved buffer
jah128 0:d6269d17c8cf 133 int count = 0;
jah128 0:d6269d17c8cf 134 for(int i=c_column;i<16;i++){
jah128 0:d6269d17c8cf 135 if(count < length){
jah128 0:d6269d17c8cf 136 if(c_row == 0) preserve_line_1[i] = message[count];
jah128 0:d6269d17c8cf 137 else preserve_line_2[i] = message[count];
jah128 0:d6269d17c8cf 138 }
jah128 0:d6269d17c8cf 139 count++;
jah128 0:d6269d17c8cf 140 }
jah128 0:d6269d17c8cf 141 c_column+=length;
jah128 0:d6269d17c8cf 142 if(c_column>15) c_column=15;
jah128 0:d6269d17c8cf 143 }
jah128 0:d6269d17c8cf 144
jah128 0:d6269d17c8cf 145
jah128 0:d6269d17c8cf 146 void Display::write_string(char * message, char length){
jah128 0:d6269d17c8cf 147 char to_send [length+1];
jah128 0:d6269d17c8cf 148 to_send[0]=0x40;
jah128 0:d6269d17c8cf 149 for(int i=0;i<length;i++){
jah128 0:d6269d17c8cf 150 to_send[i+1] = message[i];
jah128 0:d6269d17c8cf 151 }
jah128 0:d6269d17c8cf 152 _i2c.write(LCD_ADDRESS,to_send,length+1);
jah128 0:d6269d17c8cf 153 // Add to saved buffer
jah128 0:d6269d17c8cf 154 int count = 0;
jah128 0:d6269d17c8cf 155 for(int i=c_column;i<16;i++){
jah128 0:d6269d17c8cf 156 if(count < length){
jah128 0:d6269d17c8cf 157 if(c_row == 0) preserve_line_1[i] = message[count];
jah128 0:d6269d17c8cf 158 else preserve_line_2[i] = message[count];
jah128 0:d6269d17c8cf 159 }
jah128 0:d6269d17c8cf 160 count++;
jah128 0:d6269d17c8cf 161 }
jah128 0:d6269d17c8cf 162 c_column+=length;
jah128 0:d6269d17c8cf 163 if(c_column>15) c_column=15;
jah128 0:d6269d17c8cf 164 }
jah128 0:d6269d17c8cf 165
jah128 0:d6269d17c8cf 166 void Display::set_position(char row, char column){
jah128 0:d6269d17c8cf 167 if(row < 2 && column < 16){
jah128 0:d6269d17c8cf 168 char pos = 128 +((row * 64)+column);
jah128 0:d6269d17c8cf 169 i2c_message(pos);
jah128 0:d6269d17c8cf 170 c_row= row;
jah128 0:d6269d17c8cf 171 c_column = column;
jah128 0:d6269d17c8cf 172 }
jah128 0:d6269d17c8cf 173 }
jah128 0:d6269d17c8cf 174
jah128 0:d6269d17c8cf 175 void Display::set_cursor(char enable){
jah128 0:d6269d17c8cf 176 cursor_on=enable;
jah128 0:d6269d17c8cf 177 _set_display();
jah128 0:d6269d17c8cf 178 }
jah128 0:d6269d17c8cf 179
jah128 0:d6269d17c8cf 180 void Display::set_blink(char enable){
jah128 0:d6269d17c8cf 181 blink_on=enable;
jah128 0:d6269d17c8cf 182 _set_display();
jah128 0:d6269d17c8cf 183 }
jah128 0:d6269d17c8cf 184
jah128 0:d6269d17c8cf 185 void Display::set_display(char enable){
jah128 0:d6269d17c8cf 186 display_on=enable;
jah128 0:d6269d17c8cf 187 _set_display();
jah128 0:d6269d17c8cf 188 }
jah128 0:d6269d17c8cf 189
jah128 0:d6269d17c8cf 190 void Display::set_backlight_brightness(float brightness){
jah128 0:d6269d17c8cf 191 if(brightness > 1) brightness = 0;
jah128 0:d6269d17c8cf 192 if(brightness < 0) brightness = 0;
jah128 0:d6269d17c8cf 193 backlight_brightness = brightness;
jah128 0:d6269d17c8cf 194 if(backlight_brightness == 1) {
jah128 0:d6269d17c8cf 195 backlight_timeout.detach();
jah128 0:d6269d17c8cf 196 _backlight = 1;
jah128 0:d6269d17c8cf 197 }else{
jah128 0:d6269d17c8cf 198 if(backlight_brightness == 0){
jah128 0:d6269d17c8cf 199 backlight_timeout.detach();
jah128 0:d6269d17c8cf 200 _backlight = 0;
jah128 0:d6269d17c8cf 201 } else {
jah128 0:d6269d17c8cf 202 backlight_on_time = (int) (10000.0f * backlight_brightness);
jah128 0:d6269d17c8cf 203 backlight_off_time = 10000 - backlight_on_time;
jah128 0:d6269d17c8cf 204 backlight_step = 0;
jah128 0:d6269d17c8cf 205 _backlight = 0;
jah128 0:d6269d17c8cf 206 backlight_timeout.attach_us(this,&Display::IF_backlight_toggle,backlight_off_time);
jah128 0:d6269d17c8cf 207 }
jah128 0:d6269d17c8cf 208 }
jah128 0:d6269d17c8cf 209 }
jah128 0:d6269d17c8cf 210
jah128 0:d6269d17c8cf 211 void Display::IF_backlight_toggle(){
jah128 0:d6269d17c8cf 212 if(backlight_step == 0){
jah128 0:d6269d17c8cf 213 _backlight = 1;
jah128 0:d6269d17c8cf 214 backlight_step = 1;
jah128 0:d6269d17c8cf 215 backlight_timeout.attach_us(this,&Display::IF_backlight_toggle,backlight_on_time);
jah128 0:d6269d17c8cf 216 } else {
jah128 0:d6269d17c8cf 217 _backlight = 0;
jah128 0:d6269d17c8cf 218 backlight_step = 0;
jah128 0:d6269d17c8cf 219 backlight_timeout.attach_us(this,&Display::IF_backlight_toggle,backlight_off_time);
jah128 0:d6269d17c8cf 220 }
jah128 0:d6269d17c8cf 221 }
jah128 0:d6269d17c8cf 222 void Display::clear_display(){
jah128 0:d6269d17c8cf 223 for(int i=0;i<16;i++){
jah128 0:d6269d17c8cf 224 preserve_line_1[i] = 0x20;
jah128 0:d6269d17c8cf 225 preserve_line_2[i] = 0x20;
jah128 0:d6269d17c8cf 226 }
jah128 0:d6269d17c8cf 227 i2c_message(0x01);
jah128 0:d6269d17c8cf 228 }
jah128 0:d6269d17c8cf 229
jah128 0:d6269d17c8cf 230 void Display::home(){
jah128 0:d6269d17c8cf 231 c_row = 0;
jah128 0:d6269d17c8cf 232 c_column = 0;
jah128 0:d6269d17c8cf 233 i2c_message(0x02);
jah128 0:d6269d17c8cf 234 }
jah128 0:d6269d17c8cf 235
jah128 0:d6269d17c8cf 236 void Display::debug_page(char * message, char length){
jah128 0:d6269d17c8cf 237 p_row=c_row;
jah128 0:d6269d17c8cf 238 p_column=c_column;
jah128 0:d6269d17c8cf 239 i2c_message(0x01);
jah128 0:d6269d17c8cf 240 home();
jah128 0:d6269d17c8cf 241 char multipage_mode = 0;
jah128 0:d6269d17c8cf 242 char line_1[18];
jah128 0:d6269d17c8cf 243 char line_2[18];
jah128 0:d6269d17c8cf 244 line_1[0]=0x40;
jah128 0:d6269d17c8cf 245 line_2[0]=0x40;
jah128 0:d6269d17c8cf 246 if(length > 16){
jah128 0:d6269d17c8cf 247 strncpy(line_1+1, message, 16);
jah128 0:d6269d17c8cf 248 char f_length = length - 16;
jah128 0:d6269d17c8cf 249 if(f_length > 16) {
jah128 0:d6269d17c8cf 250 f_length = 16;
jah128 0:d6269d17c8cf 251 multipage_mode = 1;
jah128 0:d6269d17c8cf 252 }
jah128 0:d6269d17c8cf 253 strncpy(line_2+1, message+16, f_length);
jah128 0:d6269d17c8cf 254 line_1[17]=0;
jah128 0:d6269d17c8cf 255 line_2[f_length+1]=0;
jah128 0:d6269d17c8cf 256 _i2c.write(LCD_ADDRESS,line_1,17);
jah128 0:d6269d17c8cf 257 set_position(1,0);
jah128 0:d6269d17c8cf 258 _i2c.write(LCD_ADDRESS,line_2,f_length+1);
jah128 0:d6269d17c8cf 259 } else {
jah128 0:d6269d17c8cf 260 strncpy(line_1+1, message, length);
jah128 0:d6269d17c8cf 261 _i2c.write(LCD_ADDRESS,line_1,length+1);
jah128 0:d6269d17c8cf 262 }
jah128 0:d6269d17c8cf 263 if(multipage_mode == 1){
jah128 0:d6269d17c8cf 264 strncpy(multipage, message + 32, length - 32);
jah128 0:d6269d17c8cf 265 multipage_length = length - 32;
jah128 0:d6269d17c8cf 266 debug_timeout.attach(this,&Display::IF_debug_multipage,PAGE_TIME);
jah128 0:d6269d17c8cf 267 } else debug_timeout.attach(this,&Display::IF_restore_page,CLEAR_TIME);
jah128 0:d6269d17c8cf 268 }
jah128 0:d6269d17c8cf 269
jah128 0:d6269d17c8cf 270 void Display::IF_restore_page(){
jah128 0:d6269d17c8cf 271 i2c_message(0x01);
jah128 0:d6269d17c8cf 272 home();
jah128 0:d6269d17c8cf 273 char line_1[17];
jah128 0:d6269d17c8cf 274 char line_2[17];
jah128 0:d6269d17c8cf 275 line_1[0]=0x40;
jah128 0:d6269d17c8cf 276 line_2[0]=0x40;
jah128 0:d6269d17c8cf 277 strncpy(line_1+1, preserve_line_1, 16);
jah128 0:d6269d17c8cf 278 strncpy(line_2+1, preserve_line_2, 16);
jah128 0:d6269d17c8cf 279 _i2c.write(LCD_ADDRESS,line_1,17);
jah128 0:d6269d17c8cf 280 set_position(1,0);
jah128 0:d6269d17c8cf 281 _i2c.write(LCD_ADDRESS,line_2,17);
jah128 0:d6269d17c8cf 282 set_position(p_row,p_column);
jah128 0:d6269d17c8cf 283 }
jah128 0:d6269d17c8cf 284
jah128 0:d6269d17c8cf 285 void Display::IF_debug_multipage(){
jah128 0:d6269d17c8cf 286 i2c_message(0x01);
jah128 0:d6269d17c8cf 287 home();
jah128 0:d6269d17c8cf 288 char multipage_mode = 0;
jah128 0:d6269d17c8cf 289 char line_1[18];
jah128 0:d6269d17c8cf 290 char line_2[18];
jah128 0:d6269d17c8cf 291 line_1[0]=0x40;
jah128 0:d6269d17c8cf 292 line_2[0]=0x40;
jah128 0:d6269d17c8cf 293 if(multipage_length > 16){
jah128 0:d6269d17c8cf 294 strncpy(line_1+1, multipage, 16);
jah128 0:d6269d17c8cf 295 char f_length = multipage_length - 16;
jah128 0:d6269d17c8cf 296 if(f_length > 16) {
jah128 0:d6269d17c8cf 297 f_length = 16;
jah128 0:d6269d17c8cf 298 multipage_mode = 1;
jah128 0:d6269d17c8cf 299 }
jah128 0:d6269d17c8cf 300 strncpy(line_2+1, multipage+16, f_length);
jah128 0:d6269d17c8cf 301 line_1[17]=0;
jah128 0:d6269d17c8cf 302 line_2[f_length+1]=0;
jah128 0:d6269d17c8cf 303 _i2c.write(LCD_ADDRESS,line_1,17);
jah128 0:d6269d17c8cf 304 set_position(1,0);
jah128 0:d6269d17c8cf 305 _i2c.write(LCD_ADDRESS,line_2,f_length+1);
jah128 0:d6269d17c8cf 306 } else {
jah128 0:d6269d17c8cf 307 strncpy(line_1+1, multipage, multipage_length);
jah128 0:d6269d17c8cf 308 _i2c.write(LCD_ADDRESS,line_1,multipage_length+1);
jah128 0:d6269d17c8cf 309 }
jah128 0:d6269d17c8cf 310 if(multipage_mode == 1){
jah128 0:d6269d17c8cf 311 char temp[200];
jah128 0:d6269d17c8cf 312 strncpy(temp, multipage + 32, multipage_length - 32);
jah128 0:d6269d17c8cf 313 multipage_length -= 32;
jah128 0:d6269d17c8cf 314 strncpy(multipage, temp, multipage_length);
jah128 0:d6269d17c8cf 315 debug_timeout.attach(this,&Display::IF_debug_multipage,PAGE_TIME);
jah128 0:d6269d17c8cf 316 }else debug_timeout.attach(this,&Display::IF_restore_page,CLEAR_TIME);
jah128 0:d6269d17c8cf 317 }
jah128 0:d6269d17c8cf 318
jah128 0:d6269d17c8cf 319 void Display::_set_display(){
jah128 0:d6269d17c8cf 320 char mode = 8;
jah128 0:d6269d17c8cf 321 if(display_on>0) mode += 4;
jah128 0:d6269d17c8cf 322 if(cursor_on>0) mode += 2;
jah128 0:d6269d17c8cf 323 if(blink_on>0) mode ++;
jah128 0:d6269d17c8cf 324 i2c_message(mode);
jah128 0:d6269d17c8cf 325 }
jah128 0:d6269d17c8cf 326
jah128 0:d6269d17c8cf 327
jah128 0:d6269d17c8cf 328 int Display::_putc (int c) {
jah128 0:d6269d17c8cf 329 putc(c);
jah128 0:d6269d17c8cf 330 return(c);
jah128 0:d6269d17c8cf 331 }
jah128 0:d6269d17c8cf 332
jah128 0:d6269d17c8cf 333 int Display::_getc (void) {
jah128 0:d6269d17c8cf 334 char r = 0;
jah128 0:d6269d17c8cf 335 return(r);
jah128 0:d6269d17c8cf 336 }