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:
Mon Oct 17 13:09:10 2016 +0000
Revision:
12:878c6e9d9e60
Parent:
8:6c92789d5f87
Added Psiswarm class; changed Dances to Animations

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