Handheld controller (RF) for Pi Swarm system

Dependencies:   mbed

Fork of Pi_Swarm_Handheld_Controller by piswarm

Committer:
jah128
Date:
Tue Jun 10 11:05:23 2014 +0000
Revision:
0:d63a63feb104
Handheld controller for Pi Swarm (old code)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jah128 0:d63a63feb104 1 /* University of York Robotics Laboratory MBED Library: Display Driver
jah128 0:d63a63feb104 2 *
jah128 0:d63a63feb104 3 * File: display.cpp
jah128 0:d63a63feb104 4 *
jah128 0:d63a63feb104 5 * (C) Dr James Hilder, Dept. Electronics & Computer Science, University of York
jah128 0:d63a63feb104 6 *
jah128 0:d63a63feb104 7 * October 2013
jah128 0:d63a63feb104 8 *
jah128 0:d63a63feb104 9 * Driver for the Midas 20x4 I2C LCD Display (MCCOG42005A6W-BNMLWI) LCD
jah128 0:d63a63feb104 10 *
jah128 0:d63a63feb104 11 * Farnell part 2218946
jah128 0:d63a63feb104 12 *
jah128 0:d63a63feb104 13 */
jah128 0:d63a63feb104 14
jah128 0:d63a63feb104 15 #include "mbed.h"
jah128 0:d63a63feb104 16 #include "display.h"
jah128 0:d63a63feb104 17
jah128 0:d63a63feb104 18 Display::Display(PinName sda, PinName scl, PinName reset) : Stream("display"), _i2c(sda,scl), _reset(reset) {
jah128 0:d63a63feb104 19
jah128 0:d63a63feb104 20 }
jah128 0:d63a63feb104 21
jah128 0:d63a63feb104 22 Display::Display() : Stream("display"), _i2c(p9,p10), _reset(p12) {
jah128 0:d63a63feb104 23
jah128 0:d63a63feb104 24 }
jah128 0:d63a63feb104 25
jah128 0:d63a63feb104 26 int Display::i2c_message(char byte){
jah128 0:d63a63feb104 27 char bytes [2];
jah128 0:d63a63feb104 28 bytes[0]=0x80;
jah128 0:d63a63feb104 29 bytes[1]=byte;
jah128 0:d63a63feb104 30 return _i2c.write(LCD_ADDRESS,bytes,2);
jah128 0:d63a63feb104 31 }
jah128 0:d63a63feb104 32
jah128 0:d63a63feb104 33 int Display::disp_putc(int c){
jah128 0:d63a63feb104 34 char message [2];
jah128 0:d63a63feb104 35 message[0]=0x40;
jah128 0:d63a63feb104 36 message[1]=c;
jah128 0:d63a63feb104 37 _i2c.write(LCD_ADDRESS,message,2);
jah128 0:d63a63feb104 38 return c;
jah128 0:d63a63feb104 39 }
jah128 0:d63a63feb104 40
jah128 0:d63a63feb104 41
jah128 0:d63a63feb104 42
jah128 0:d63a63feb104 43 void Display::init_display(){
jah128 0:d63a63feb104 44 //Set initial states: display on, cursor off
jah128 0:d63a63feb104 45 display_on = 1;
jah128 0:d63a63feb104 46 cursor_on = 1;
jah128 0:d63a63feb104 47 blink_on = 1;
jah128 0:d63a63feb104 48
jah128 0:d63a63feb104 49 _reset=1;
jah128 0:d63a63feb104 50 wait(0.02);
jah128 0:d63a63feb104 51 //Set reset low
jah128 0:d63a63feb104 52 _reset=0;
jah128 0:d63a63feb104 53 wait(0.001);
jah128 0:d63a63feb104 54 _reset=1;
jah128 0:d63a63feb104 55 wait(0.03);
jah128 0:d63a63feb104 56 i2c_message(0x3a);
jah128 0:d63a63feb104 57 i2c_message(0x1e);
jah128 0:d63a63feb104 58 i2c_message(0x39);
jah128 0:d63a63feb104 59 i2c_message(0x1c);
jah128 0:d63a63feb104 60 i2c_message(0x79);
jah128 0:d63a63feb104 61 i2c_message(0x5d);
jah128 0:d63a63feb104 62 i2c_message(0x6d);
jah128 0:d63a63feb104 63 _set_display();
jah128 0:d63a63feb104 64 clear_display();
jah128 0:d63a63feb104 65 }
jah128 0:d63a63feb104 66
jah128 0:d63a63feb104 67 void Display::write_string(char * message, char length){
jah128 0:d63a63feb104 68 char to_send [length+1];
jah128 0:d63a63feb104 69 to_send[0]=0x40;
jah128 0:d63a63feb104 70 for(int i=0;i<length;i++){
jah128 0:d63a63feb104 71 to_send[i+1] = message[i];
jah128 0:d63a63feb104 72 }
jah128 0:d63a63feb104 73 _i2c.write(LCD_ADDRESS,to_send,length+1);
jah128 0:d63a63feb104 74 }
jah128 0:d63a63feb104 75
jah128 0:d63a63feb104 76 void Display::set_position(char row, char column){
jah128 0:d63a63feb104 77 if(row < 4 && column < 20){
jah128 0:d63a63feb104 78 char pos = 128 +((row * 32)+column);
jah128 0:d63a63feb104 79 i2c_message(pos);
jah128 0:d63a63feb104 80 }
jah128 0:d63a63feb104 81 }
jah128 0:d63a63feb104 82
jah128 0:d63a63feb104 83 void Display::set_cursor(char enable){
jah128 0:d63a63feb104 84 cursor_on=enable;
jah128 0:d63a63feb104 85 _set_display();
jah128 0:d63a63feb104 86 }
jah128 0:d63a63feb104 87
jah128 0:d63a63feb104 88 void Display::set_blink(char enable){
jah128 0:d63a63feb104 89 blink_on=enable;
jah128 0:d63a63feb104 90 _set_display();
jah128 0:d63a63feb104 91 }
jah128 0:d63a63feb104 92
jah128 0:d63a63feb104 93 void Display::set_display(char enable){
jah128 0:d63a63feb104 94 display_on=enable;
jah128 0:d63a63feb104 95 _set_display();
jah128 0:d63a63feb104 96 }
jah128 0:d63a63feb104 97
jah128 0:d63a63feb104 98 void Display::clear_display(){
jah128 0:d63a63feb104 99 i2c_message(0x01);
jah128 0:d63a63feb104 100 }
jah128 0:d63a63feb104 101
jah128 0:d63a63feb104 102 void Display::home(){
jah128 0:d63a63feb104 103 i2c_message(0x02);
jah128 0:d63a63feb104 104 }
jah128 0:d63a63feb104 105
jah128 0:d63a63feb104 106
jah128 0:d63a63feb104 107 void Display::_set_display(){
jah128 0:d63a63feb104 108 char mode = 8;
jah128 0:d63a63feb104 109 if(display_on>0) mode += 4;
jah128 0:d63a63feb104 110 if(cursor_on>0) mode += 2;
jah128 0:d63a63feb104 111 if(blink_on>0) mode ++;
jah128 0:d63a63feb104 112 i2c_message(mode);
jah128 0:d63a63feb104 113 }
jah128 0:d63a63feb104 114
jah128 0:d63a63feb104 115
jah128 0:d63a63feb104 116 int Display::_putc (int c) {
jah128 0:d63a63feb104 117 disp_putc(c);
jah128 0:d63a63feb104 118 return(c);
jah128 0:d63a63feb104 119 }
jah128 0:d63a63feb104 120
jah128 0:d63a63feb104 121 int Display::_getc (void) {
jah128 0:d63a63feb104 122 char r = 0;
jah128 0:d63a63feb104 123 return(r);
jah128 0:d63a63feb104 124 }
jah128 0:d63a63feb104 125
jah128 0:d63a63feb104 126
jah128 0:d63a63feb104 127 /* Permission is hereby granted, free of charge, to any person obtaining a copy
jah128 0:d63a63feb104 128 * of this software and associated documentation files (the "Software"), to deal
jah128 0:d63a63feb104 129 * in the Software without restriction, including without limitation the rights
jah128 0:d63a63feb104 130 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
jah128 0:d63a63feb104 131 * copies of the Software, and to permit persons to whom the Software is
jah128 0:d63a63feb104 132 * furnished to do so, subject to the following conditions:
jah128 0:d63a63feb104 133 *
jah128 0:d63a63feb104 134 * The above copyright notice and this permission notice shall be included in
jah128 0:d63a63feb104 135 * all copies or substantial portions of the Software.
jah128 0:d63a63feb104 136 *
jah128 0:d63a63feb104 137 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
jah128 0:d63a63feb104 138 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
jah128 0:d63a63feb104 139 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
jah128 0:d63a63feb104 140 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
jah128 0:d63a63feb104 141 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jah128 0:d63a63feb104 142 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
jah128 0:d63a63feb104 143 * THE SOFTWARE.
jah128 0:d63a63feb104 144 */