Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of BeautifulMemeProject by
display.cpp
00001 /* University of York Robotics Laboratory PsiSwarm Library: Display Driver Source File 00002 * 00003 * File: display.cpp 00004 * 00005 * (C) Dr James Hilder, Dept. Electronics & Computer Science, University of York 00006 * 00007 * PsiSwarm Library Version: 0.3 00008 * Display Driver Version: 0.2 00009 * 00010 * October 2015 00011 * 00012 * Driver for the Midas 16x2 I2C LCD Display (MCCOG21605x6W) LCD 00013 * [Farnell part 2218942 or 2063206] 00014 * 00015 */ 00016 00017 #include "psiswarm.h" 00018 00019 Timeout init_timeout; 00020 Timeout backlight_timeout; 00021 Timeout debug_timeout; 00022 int backlight_on_time; 00023 int backlight_off_time; 00024 char backlight_step; 00025 char multipage[200]; 00026 char multipage_length = 0; 00027 char preserve_line_1 [17]; 00028 char preserve_line_2 [17]; 00029 char c_row=0; 00030 char c_column=0; 00031 char p_row; 00032 char p_column; 00033 00034 00035 Display::Display(PinName sda, PinName scl, PinName reset, PinName backlight) : Stream("display"), _i2c(sda,scl), _reset(reset), _backlight(backlight) { 00036 } 00037 00038 Display::Display() : Stream("display"), _i2c(p28,p27), _reset(p29), _backlight(p30) { 00039 } 00040 00041 int Display::i2c_message(char byte){ 00042 char bytes [2]; 00043 bytes[0]=0x80; 00044 bytes[1]=byte; 00045 int ret=_i2c.write(LCD_ADDRESS,bytes,2); 00046 wait(0.01); 00047 return ret; 00048 } 00049 00050 int Display::disp_putc(int c){ 00051 char message [2]; 00052 message[0]=0x40; 00053 message[1]=c; 00054 _i2c.write(LCD_ADDRESS,message,2); 00055 wait(0.01); 00056 return c; 00057 } 00058 00059 void Display::init_display(char mode){ 00060 //Set initial states: display on, cursor off 00061 display_on = 1; 00062 set_backlight_brightness(1); 00063 cursor_on = 0; 00064 blink_on = 0; 00065 00066 _reset=1; 00067 wait(0.02); 00068 //Set reset low 00069 _reset=0; 00070 wait(0.001); 00071 _reset=1; 00072 wait(0.03); 00073 i2c_message(0x38); 00074 i2c_message(0x39); 00075 i2c_message(0x14); 00076 i2c_message(0x74); 00077 i2c_message(0x54); 00078 i2c_message(0x6F); 00079 _set_display(); 00080 clear_display(); 00081 char psis[17]; 00082 for(int i=0;i<16;i++){ 00083 psis[i]=0x1D; 00084 } 00085 set_position(0,0); 00086 write_string(psis,16); 00087 set_position(1,0); 00088 write_string(psis,16); 00089 wait(0.25); 00090 clear_display(); 00091 if(mode == 0){ 00092 set_position(0,0); 00093 write_string(" YORK ROBOTICS"); 00094 set_position(1,0); 00095 write_string(" LABORATORY"); 00096 init_timeout.attach(this,&Display::post_init,0.3);} 00097 else { 00098 set_position(0,0); 00099 write_string("Hold button to"); 00100 set_position(1,0); 00101 write_string("launch demo code"); 00102 } 00103 } 00104 00105 void Display::post_init(){ 00106 clear_display(); 00107 home(); 00108 write_string("PSI SWARM ROBOT"); 00109 set_position(1,0); 00110 char line [17]; 00111 sprintf(line,"VERSION %1.2f", SOFTWARE_VERSION_CODE ); 00112 set_position(1,0); 00113 write_string(line); 00114 init_timeout.attach(this,&Display::post_post_init,0.3); 00115 } 00116 00117 void Display::post_post_init(){ 00118 clear_display(); 00119 home(); 00120 } 00121 00122 void Display::write_string(char * message){ 00123 size_t length = strlen(message); 00124 if (length > 16) length = 16; 00125 char to_send [length+1]; 00126 to_send[0]=0x40; 00127 for(int i=0;i<length;i++){ 00128 to_send[i+1] = message[i]; 00129 } 00130 _i2c.write(LCD_ADDRESS,to_send,length+1); 00131 // Add to saved buffer 00132 int count = 0; 00133 for(int i=c_column;i<16;i++){ 00134 if(count < length){ 00135 if(c_row == 0) preserve_line_1[i] = message[count]; 00136 else preserve_line_2[i] = message[count]; 00137 } 00138 count++; 00139 } 00140 c_column+=length; 00141 if(c_column>15) c_column=15; 00142 } 00143 00144 00145 void Display::write_string(char * message, char length){ 00146 char to_send [length+1]; 00147 to_send[0]=0x40; 00148 for(int i=0;i<length;i++){ 00149 to_send[i+1] = message[i]; 00150 } 00151 _i2c.write(LCD_ADDRESS,to_send,length+1); 00152 // Add to saved buffer 00153 int count = 0; 00154 for(int i=c_column;i<16;i++){ 00155 if(count < length){ 00156 if(c_row == 0) preserve_line_1[i] = message[count]; 00157 else preserve_line_2[i] = message[count]; 00158 } 00159 count++; 00160 } 00161 c_column+=length; 00162 if(c_column>15) c_column=15; 00163 } 00164 00165 void Display::set_position(char row, char column){ 00166 if(row < 2 && column < 16){ 00167 char pos = 128 +((row * 64)+column); 00168 i2c_message(pos); 00169 c_row= row; 00170 c_column = column; 00171 } 00172 } 00173 00174 void Display::set_cursor(char enable){ 00175 cursor_on=enable; 00176 _set_display(); 00177 } 00178 00179 void Display::set_blink(char enable){ 00180 blink_on=enable; 00181 _set_display(); 00182 } 00183 00184 void Display::set_display(char enable){ 00185 display_on=enable; 00186 _set_display(); 00187 } 00188 00189 void Display::set_backlight_brightness(float brightness){ 00190 if(brightness > 1) brightness = 0; 00191 if(brightness < 0) brightness = 0; 00192 backlight_brightness = brightness; 00193 if(backlight_brightness == 1) { 00194 backlight_timeout.detach(); 00195 _backlight = 1; 00196 }else{ 00197 if(backlight_brightness == 0){ 00198 backlight_timeout.detach(); 00199 _backlight = 0; 00200 } else { 00201 backlight_on_time = (int) (10000.0f * backlight_brightness); 00202 backlight_off_time = 10000 - backlight_on_time; 00203 backlight_step = 0; 00204 _backlight = 0; 00205 backlight_timeout.attach_us(this,&Display::IF_backlight_toggle,backlight_off_time); 00206 } 00207 } 00208 } 00209 00210 void Display::IF_backlight_toggle(){ 00211 if(backlight_step == 0){ 00212 _backlight = 1; 00213 backlight_step = 1; 00214 backlight_timeout.attach_us(this,&Display::IF_backlight_toggle,backlight_on_time); 00215 } else { 00216 _backlight = 0; 00217 backlight_step = 0; 00218 backlight_timeout.attach_us(this,&Display::IF_backlight_toggle,backlight_off_time); 00219 } 00220 } 00221 void Display::clear_display(){ 00222 for(int i=0;i<16;i++){ 00223 preserve_line_1[i] = 0x20; 00224 preserve_line_2[i] = 0x20; 00225 } 00226 i2c_message(0x01); 00227 } 00228 00229 void Display::home(){ 00230 c_row = 0; 00231 c_column = 0; 00232 i2c_message(0x02); 00233 } 00234 00235 void Display::debug_page(char * message, char length){ 00236 p_row=c_row; 00237 p_column=c_column; 00238 i2c_message(0x01); 00239 home(); 00240 char multipage_mode = 0; 00241 char line_1[18]; 00242 char line_2[18]; 00243 line_1[0]=0x40; 00244 line_2[0]=0x40; 00245 if(length > 16){ 00246 strncpy(line_1+1, message, 16); 00247 char f_length = length - 16; 00248 if(f_length > 16) { 00249 f_length = 16; 00250 multipage_mode = 1; 00251 } 00252 strncpy(line_2+1, message+16, f_length); 00253 line_1[17]=0; 00254 line_2[f_length+1]=0; 00255 _i2c.write(LCD_ADDRESS,line_1,17); 00256 set_position(1,0); 00257 _i2c.write(LCD_ADDRESS,line_2,f_length+1); 00258 } else { 00259 strncpy(line_1+1, message, length); 00260 _i2c.write(LCD_ADDRESS,line_1,length+1); 00261 } 00262 if(multipage_mode == 1){ 00263 strncpy(multipage, message + 32, length - 32); 00264 multipage_length = length - 32; 00265 debug_timeout.attach(this,&Display::IF_debug_multipage,PAGE_TIME); 00266 } else debug_timeout.attach(this,&Display::IF_restore_page,CLEAR_TIME); 00267 } 00268 00269 void Display::IF_restore_page(){ 00270 i2c_message(0x01); 00271 home(); 00272 char line_1[17]; 00273 char line_2[17]; 00274 line_1[0]=0x40; 00275 line_2[0]=0x40; 00276 strncpy(line_1+1, preserve_line_1, 16); 00277 strncpy(line_2+1, preserve_line_2, 16); 00278 _i2c.write(LCD_ADDRESS,line_1,17); 00279 set_position(1,0); 00280 _i2c.write(LCD_ADDRESS,line_2,17); 00281 set_position(p_row,p_column); 00282 } 00283 00284 void Display::IF_debug_multipage(){ 00285 i2c_message(0x01); 00286 home(); 00287 char multipage_mode = 0; 00288 char line_1[18]; 00289 char line_2[18]; 00290 line_1[0]=0x40; 00291 line_2[0]=0x40; 00292 if(multipage_length > 16){ 00293 strncpy(line_1+1, multipage, 16); 00294 char f_length = multipage_length - 16; 00295 if(f_length > 16) { 00296 f_length = 16; 00297 multipage_mode = 1; 00298 } 00299 strncpy(line_2+1, multipage+16, f_length); 00300 line_1[17]=0; 00301 line_2[f_length+1]=0; 00302 _i2c.write(LCD_ADDRESS,line_1,17); 00303 set_position(1,0); 00304 _i2c.write(LCD_ADDRESS,line_2,f_length+1); 00305 } else { 00306 strncpy(line_1+1, multipage, multipage_length); 00307 _i2c.write(LCD_ADDRESS,line_1,multipage_length+1); 00308 } 00309 if(multipage_mode == 1){ 00310 char temp[200]; 00311 strncpy(temp, multipage + 32, multipage_length - 32); 00312 multipage_length -= 32; 00313 strncpy(multipage, temp, multipage_length); 00314 debug_timeout.attach(this,&Display::IF_debug_multipage,PAGE_TIME); 00315 }else debug_timeout.attach(this,&Display::IF_restore_page,CLEAR_TIME); 00316 } 00317 00318 void Display::_set_display(){ 00319 char mode = 8; 00320 if(display_on>0) mode += 4; 00321 if(cursor_on>0) mode += 2; 00322 if(blink_on>0) mode ++; 00323 i2c_message(mode); 00324 } 00325 00326 00327 int Display::_putc (int c) { 00328 putc(c); 00329 return(c); 00330 } 00331 00332 int Display::_getc (void) { 00333 char r = 0; 00334 return(r); 00335 }
Generated on Fri Jul 15 2022 08:26:10 by
1.7.2
