For YRL Robot Arm
display.cpp@0:b14dfd8816da, 2017-03-03 (annotated)
- Committer:
- jah128
- Date:
- Fri Mar 03 13:28:54 2017 +0000
- Revision:
- 0:b14dfd8816da
Updated
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jah128 | 0:b14dfd8816da | 1 | /* University of York Robotics Laboratory Robot Arm Controller Board |
jah128 | 0:b14dfd8816da | 2 | * |
jah128 | 0:b14dfd8816da | 3 | * Copyright 2017 University of York |
jah128 | 0:b14dfd8816da | 4 | * |
jah128 | 0:b14dfd8816da | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. |
jah128 | 0:b14dfd8816da | 6 | * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
jah128 | 0:b14dfd8816da | 7 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS |
jah128 | 0:b14dfd8816da | 8 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
jah128 | 0:b14dfd8816da | 9 | * See the License for the specific language governing permissions and limitations under the License. |
jah128 | 0:b14dfd8816da | 10 | * |
jah128 | 0:b14dfd8816da | 11 | * File: display.cpp |
jah128 | 0:b14dfd8816da | 12 | * |
jah128 | 0:b14dfd8816da | 13 | * (C) Dept. Electronics & Computer Science, University of York |
jah128 | 0:b14dfd8816da | 14 | * |
jah128 | 0:b14dfd8816da | 15 | * James Hilder, Alan Millard, Shuhei Miyashita, Homero Elizondo, Jon Timmis |
jah128 | 0:b14dfd8816da | 16 | * |
jah128 | 0:b14dfd8816da | 17 | * |
jah128 | 0:b14dfd8816da | 18 | * January 2017 |
jah128 | 0:b14dfd8816da | 19 | * |
jah128 | 0:b14dfd8816da | 20 | * Driver for the Midas 16x2 I2C LCD Display (MCCOG21605x6W) LCD |
jah128 | 0:b14dfd8816da | 21 | * [Farnell part 2218942 or 2063206] |
jah128 | 0:b14dfd8816da | 22 | * |
jah128 | 0:b14dfd8816da | 23 | */ |
jah128 | 0:b14dfd8816da | 24 | |
jah128 | 0:b14dfd8816da | 25 | #include "robotarm.h" |
jah128 | 0:b14dfd8816da | 26 | |
jah128 | 0:b14dfd8816da | 27 | |
jah128 | 0:b14dfd8816da | 28 | Timeout init_timeout; |
jah128 | 0:b14dfd8816da | 29 | |
jah128 | 0:b14dfd8816da | 30 | |
jah128 | 0:b14dfd8816da | 31 | Display::Display(PinName sda, PinName scl) : Stream("display"), _i2c(sda,scl) |
jah128 | 0:b14dfd8816da | 32 | { |
jah128 | 0:b14dfd8816da | 33 | } |
jah128 | 0:b14dfd8816da | 34 | |
jah128 | 0:b14dfd8816da | 35 | Display::Display() : Stream("display"), _i2c(p28,p27) |
jah128 | 0:b14dfd8816da | 36 | { |
jah128 | 0:b14dfd8816da | 37 | } |
jah128 | 0:b14dfd8816da | 38 | |
jah128 | 0:b14dfd8816da | 39 | int Display::i2c_message(char byte) |
jah128 | 0:b14dfd8816da | 40 | { |
jah128 | 0:b14dfd8816da | 41 | char bytes [2]; |
jah128 | 0:b14dfd8816da | 42 | bytes[0]=0x80; |
jah128 | 0:b14dfd8816da | 43 | bytes[1]=byte; |
jah128 | 0:b14dfd8816da | 44 | int ret=_i2c.write(LCD_ADDRESS,bytes,2); |
jah128 | 0:b14dfd8816da | 45 | wait(0.01); |
jah128 | 0:b14dfd8816da | 46 | return ret; |
jah128 | 0:b14dfd8816da | 47 | } |
jah128 | 0:b14dfd8816da | 48 | |
jah128 | 0:b14dfd8816da | 49 | int Display::disp_putc(int c) |
jah128 | 0:b14dfd8816da | 50 | { |
jah128 | 0:b14dfd8816da | 51 | char message [2]; |
jah128 | 0:b14dfd8816da | 52 | message[0]=0x40; |
jah128 | 0:b14dfd8816da | 53 | message[1]=c; |
jah128 | 0:b14dfd8816da | 54 | _i2c.write(LCD_ADDRESS,message,2); |
jah128 | 0:b14dfd8816da | 55 | wait(0.01); |
jah128 | 0:b14dfd8816da | 56 | return c; |
jah128 | 0:b14dfd8816da | 57 | } |
jah128 | 0:b14dfd8816da | 58 | |
jah128 | 0:b14dfd8816da | 59 | void Display::init() |
jah128 | 0:b14dfd8816da | 60 | { |
jah128 | 0:b14dfd8816da | 61 | //Set initial states: display on, cursor off |
jah128 | 0:b14dfd8816da | 62 | display_on = 1; |
jah128 | 0:b14dfd8816da | 63 | cursor_on = 0; |
jah128 | 0:b14dfd8816da | 64 | blink_on = 0; |
jah128 | 0:b14dfd8816da | 65 | |
jah128 | 0:b14dfd8816da | 66 | i2c_message(0x38); |
jah128 | 0:b14dfd8816da | 67 | i2c_message(0x39); |
jah128 | 0:b14dfd8816da | 68 | i2c_message(0x14); |
jah128 | 0:b14dfd8816da | 69 | i2c_message(0x74); |
jah128 | 0:b14dfd8816da | 70 | i2c_message(0x54); |
jah128 | 0:b14dfd8816da | 71 | i2c_message(0x6F); |
jah128 | 0:b14dfd8816da | 72 | _set_display(); |
jah128 | 0:b14dfd8816da | 73 | clear_display(); |
jah128 | 0:b14dfd8816da | 74 | wait(0.05); |
jah128 | 0:b14dfd8816da | 75 | clear_display(); |
jah128 | 0:b14dfd8816da | 76 | set_position(0,0); |
jah128 | 0:b14dfd8816da | 77 | write_string(" YORK ROBOTICS"); |
jah128 | 0:b14dfd8816da | 78 | set_position(1,0); |
jah128 | 0:b14dfd8816da | 79 | write_string(" LABORATORY"); |
jah128 | 0:b14dfd8816da | 80 | init_timeout.attach(this,&Display::post_init,0.3); |
jah128 | 0:b14dfd8816da | 81 | wait(0.62); |
jah128 | 0:b14dfd8816da | 82 | } |
jah128 | 0:b14dfd8816da | 83 | |
jah128 | 0:b14dfd8816da | 84 | void Display::post_init() |
jah128 | 0:b14dfd8816da | 85 | { |
jah128 | 0:b14dfd8816da | 86 | clear_display(); |
jah128 | 0:b14dfd8816da | 87 | home(); |
jah128 | 0:b14dfd8816da | 88 | write_string("ROBOTIC ARM"); |
jah128 | 0:b14dfd8816da | 89 | set_position(1,0); |
jah128 | 0:b14dfd8816da | 90 | char line [17]; |
jah128 | 0:b14dfd8816da | 91 | sprintf(line,"VERSION %1.2f", SOFTWARE_VERSION_CODE ); |
jah128 | 0:b14dfd8816da | 92 | set_position(1,0); |
jah128 | 0:b14dfd8816da | 93 | write_string(line); |
jah128 | 0:b14dfd8816da | 94 | init_timeout.attach(this,&Display::post_post_init,0.3); |
jah128 | 0:b14dfd8816da | 95 | } |
jah128 | 0:b14dfd8816da | 96 | |
jah128 | 0:b14dfd8816da | 97 | void Display::post_post_init() |
jah128 | 0:b14dfd8816da | 98 | { |
jah128 | 0:b14dfd8816da | 99 | clear_display(); |
jah128 | 0:b14dfd8816da | 100 | home(); |
jah128 | 0:b14dfd8816da | 101 | } |
jah128 | 0:b14dfd8816da | 102 | |
jah128 | 0:b14dfd8816da | 103 | void Display::write_string(char * message) |
jah128 | 0:b14dfd8816da | 104 | { |
jah128 | 0:b14dfd8816da | 105 | size_t length = strlen(message); |
jah128 | 0:b14dfd8816da | 106 | if (length > 16) length = 16; |
jah128 | 0:b14dfd8816da | 107 | char to_send [length+1]; |
jah128 | 0:b14dfd8816da | 108 | to_send[0]=0x40; |
jah128 | 0:b14dfd8816da | 109 | for(int i=0; i<length; i++) { |
jah128 | 0:b14dfd8816da | 110 | to_send[i+1] = message[i]; |
jah128 | 0:b14dfd8816da | 111 | } |
jah128 | 0:b14dfd8816da | 112 | _i2c.write(LCD_ADDRESS,to_send,length+1); |
jah128 | 0:b14dfd8816da | 113 | |
jah128 | 0:b14dfd8816da | 114 | } |
jah128 | 0:b14dfd8816da | 115 | |
jah128 | 0:b14dfd8816da | 116 | |
jah128 | 0:b14dfd8816da | 117 | void Display::write_string(char * message, char length) |
jah128 | 0:b14dfd8816da | 118 | { |
jah128 | 0:b14dfd8816da | 119 | char to_send [length+1]; |
jah128 | 0:b14dfd8816da | 120 | to_send[0]=0x40; |
jah128 | 0:b14dfd8816da | 121 | for(int i=0; i<length; i++) { |
jah128 | 0:b14dfd8816da | 122 | to_send[i+1] = message[i]; |
jah128 | 0:b14dfd8816da | 123 | } |
jah128 | 0:b14dfd8816da | 124 | _i2c.write(LCD_ADDRESS,to_send,length+1); |
jah128 | 0:b14dfd8816da | 125 | |
jah128 | 0:b14dfd8816da | 126 | } |
jah128 | 0:b14dfd8816da | 127 | |
jah128 | 0:b14dfd8816da | 128 | void Display::set_position(char row, char column) |
jah128 | 0:b14dfd8816da | 129 | { |
jah128 | 0:b14dfd8816da | 130 | if(row < 2 && column < 16) { |
jah128 | 0:b14dfd8816da | 131 | char pos = 128 +((row * 64)+column); |
jah128 | 0:b14dfd8816da | 132 | i2c_message(pos); |
jah128 | 0:b14dfd8816da | 133 | } |
jah128 | 0:b14dfd8816da | 134 | } |
jah128 | 0:b14dfd8816da | 135 | |
jah128 | 0:b14dfd8816da | 136 | void Display::set_cursor(char enable) |
jah128 | 0:b14dfd8816da | 137 | { |
jah128 | 0:b14dfd8816da | 138 | cursor_on=enable; |
jah128 | 0:b14dfd8816da | 139 | _set_display(); |
jah128 | 0:b14dfd8816da | 140 | } |
jah128 | 0:b14dfd8816da | 141 | |
jah128 | 0:b14dfd8816da | 142 | void Display::set_blink(char enable) |
jah128 | 0:b14dfd8816da | 143 | { |
jah128 | 0:b14dfd8816da | 144 | blink_on=enable; |
jah128 | 0:b14dfd8816da | 145 | _set_display(); |
jah128 | 0:b14dfd8816da | 146 | } |
jah128 | 0:b14dfd8816da | 147 | |
jah128 | 0:b14dfd8816da | 148 | void Display::set_display(char enable) |
jah128 | 0:b14dfd8816da | 149 | { |
jah128 | 0:b14dfd8816da | 150 | display_on=enable; |
jah128 | 0:b14dfd8816da | 151 | _set_display(); |
jah128 | 0:b14dfd8816da | 152 | } |
jah128 | 0:b14dfd8816da | 153 | |
jah128 | 0:b14dfd8816da | 154 | |
jah128 | 0:b14dfd8816da | 155 | void Display::clear_display() |
jah128 | 0:b14dfd8816da | 156 | { |
jah128 | 0:b14dfd8816da | 157 | i2c_message(0x01); |
jah128 | 0:b14dfd8816da | 158 | } |
jah128 | 0:b14dfd8816da | 159 | |
jah128 | 0:b14dfd8816da | 160 | void Display::home() |
jah128 | 0:b14dfd8816da | 161 | { |
jah128 | 0:b14dfd8816da | 162 | i2c_message(0x02); |
jah128 | 0:b14dfd8816da | 163 | } |
jah128 | 0:b14dfd8816da | 164 | |
jah128 | 0:b14dfd8816da | 165 | |
jah128 | 0:b14dfd8816da | 166 | void Display::_set_display() |
jah128 | 0:b14dfd8816da | 167 | { |
jah128 | 0:b14dfd8816da | 168 | char mode = 8; |
jah128 | 0:b14dfd8816da | 169 | if(display_on>0) mode += 4; |
jah128 | 0:b14dfd8816da | 170 | if(cursor_on>0) mode += 2; |
jah128 | 0:b14dfd8816da | 171 | if(blink_on>0) mode ++; |
jah128 | 0:b14dfd8816da | 172 | i2c_message(mode); |
jah128 | 0:b14dfd8816da | 173 | } |
jah128 | 0:b14dfd8816da | 174 | |
jah128 | 0:b14dfd8816da | 175 | |
jah128 | 0:b14dfd8816da | 176 | int Display::_putc (int c) |
jah128 | 0:b14dfd8816da | 177 | { |
jah128 | 0:b14dfd8816da | 178 | putc(c); |
jah128 | 0:b14dfd8816da | 179 | return(c); |
jah128 | 0:b14dfd8816da | 180 | } |
jah128 | 0:b14dfd8816da | 181 | |
jah128 | 0:b14dfd8816da | 182 | int Display::_getc (void) |
jah128 | 0:b14dfd8816da | 183 | { |
jah128 | 0:b14dfd8816da | 184 | char r = 0; |
jah128 | 0:b14dfd8816da | 185 | return(r); |
jah128 | 0:b14dfd8816da | 186 | } |
jah128 | 0:b14dfd8816da | 187 | |
jah128 | 0:b14dfd8816da | 188 | /* |
jah128 | 0:b14dfd8816da | 189 | * Copyright 2017 University of York |
jah128 | 0:b14dfd8816da | 190 | * |
jah128 | 0:b14dfd8816da | 191 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. |
jah128 | 0:b14dfd8816da | 192 | * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
jah128 | 0:b14dfd8816da | 193 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS |
jah128 | 0:b14dfd8816da | 194 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
jah128 | 0:b14dfd8816da | 195 | * See the License for the specific language governing permissions and limitations under the License. |
jah128 | 0:b14dfd8816da | 196 | */ |