Laser cutter software
Dependencies: EthernetNetIf mbed SDFileSystem
LaosDisplay/LaosDisplay.cpp@0:18ead85c200b, 2011-08-11 (annotated)
- Committer:
- pbrier
- Date:
- Thu Aug 11 09:33:15 2011 +0000
- Revision:
- 0:18ead85c200b
Laos server
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pbrier | 0:18ead85c200b | 1 | /** |
pbrier | 0:18ead85c200b | 2 | * LaosDisplay.cpp |
pbrier | 0:18ead85c200b | 3 | * User interface class, for 2x16 display and keypad |
pbrier | 0:18ead85c200b | 4 | * |
pbrier | 0:18ead85c200b | 5 | * Copyright (c) 2011 Peter Brier |
pbrier | 0:18ead85c200b | 6 | * |
pbrier | 0:18ead85c200b | 7 | * This file is part of the LaOS project (see: http://wiki.protospace.nl/index.php/LaOS) |
pbrier | 0:18ead85c200b | 8 | * |
pbrier | 0:18ead85c200b | 9 | * LaOS is free software: you can redistribute it and/or modify |
pbrier | 0:18ead85c200b | 10 | * it under the terms of the GNU General Public License as published by |
pbrier | 0:18ead85c200b | 11 | * the Free Software Foundation, either version 3 of the License, or |
pbrier | 0:18ead85c200b | 12 | * (at your option) any later version. |
pbrier | 0:18ead85c200b | 13 | * |
pbrier | 0:18ead85c200b | 14 | * LaOS is distributed in the hope that it will be useful, |
pbrier | 0:18ead85c200b | 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
pbrier | 0:18ead85c200b | 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
pbrier | 0:18ead85c200b | 17 | * GNU General Public License for more details. |
pbrier | 0:18ead85c200b | 18 | * |
pbrier | 0:18ead85c200b | 19 | * You should have received a copy of the GNU General Public License |
pbrier | 0:18ead85c200b | 20 | * along with LaOS. If not, see <http://www.gnu.org/licenses/>. |
pbrier | 0:18ead85c200b | 21 | * |
pbrier | 0:18ead85c200b | 22 | * Assume (USB) serial connected display (or terminal) in future, this could be CAN or I2C connected |
pbrier | 0:18ead85c200b | 23 | * |
pbrier | 0:18ead85c200b | 24 | */ |
pbrier | 0:18ead85c200b | 25 | #include "LaosDisplay.h" |
pbrier | 0:18ead85c200b | 26 | |
pbrier | 0:18ead85c200b | 27 | |
pbrier | 0:18ead85c200b | 28 | // I2C |
pbrier | 0:18ead85c200b | 29 | I2C i2c(p9, p10); // sda, scl |
pbrier | 0:18ead85c200b | 30 | #define _I2C_ADDRESS 0x04 |
pbrier | 0:18ead85c200b | 31 | #define _I2C_HOME 0xFE |
pbrier | 0:18ead85c200b | 32 | #define _I2C_CLS 0xFF |
pbrier | 0:18ead85c200b | 33 | #define _I2C_BAUD 10000 |
pbrier | 0:18ead85c200b | 34 | |
pbrier | 0:18ead85c200b | 35 | |
pbrier | 0:18ead85c200b | 36 | // Make new config file object |
pbrier | 0:18ead85c200b | 37 | LaosDisplay::LaosDisplay() |
pbrier | 0:18ead85c200b | 38 | { |
pbrier | 0:18ead85c200b | 39 | serial = new Serial(USBTX, USBRX); |
pbrier | 0:18ead85c200b | 40 | serial->baud(115200); |
pbrier | 0:18ead85c200b | 41 | } |
pbrier | 0:18ead85c200b | 42 | |
pbrier | 0:18ead85c200b | 43 | |
pbrier | 0:18ead85c200b | 44 | //Write char |
pbrier | 0:18ead85c200b | 45 | void LaosDisplay::write(char c) |
pbrier | 0:18ead85c200b | 46 | { |
pbrier | 0:18ead85c200b | 47 | serial->putc(c); |
pbrier | 0:18ead85c200b | 48 | } |
pbrier | 0:18ead85c200b | 49 | |
pbrier | 0:18ead85c200b | 50 | // Write string |
pbrier | 0:18ead85c200b | 51 | void LaosDisplay::write(char *s) |
pbrier | 0:18ead85c200b | 52 | { |
pbrier | 0:18ead85c200b | 53 | if ( s == NULL ) return; |
pbrier | 0:18ead85c200b | 54 | i2c.write(_I2C_ADDRESS, s, strlen(s)); |
pbrier | 0:18ead85c200b | 55 | while (*s) |
pbrier | 0:18ead85c200b | 56 | serial->putc(*s++); |
pbrier | 0:18ead85c200b | 57 | } |
pbrier | 0:18ead85c200b | 58 | |
pbrier | 0:18ead85c200b | 59 | // Clear screen |
pbrier | 0:18ead85c200b | 60 | void LaosDisplay::cls() |
pbrier | 0:18ead85c200b | 61 | { |
pbrier | 0:18ead85c200b | 62 | write('\t'); |
pbrier | 0:18ead85c200b | 63 | } |
pbrier | 0:18ead85c200b | 64 | |
pbrier | 0:18ead85c200b | 65 | |
pbrier | 0:18ead85c200b | 66 | // Read Key |
pbrier | 0:18ead85c200b | 67 | int LaosDisplay::read() |
pbrier | 0:18ead85c200b | 68 | { |
pbrier | 0:18ead85c200b | 69 | char key; |
pbrier | 0:18ead85c200b | 70 | int result; |
pbrier | 0:18ead85c200b | 71 | result = i2c.read(_I2C_ADDRESS,&key, 1); |
pbrier | 0:18ead85c200b | 72 | if ( result ) return key; |
pbrier | 0:18ead85c200b | 73 | if ( serial->readable() ) |
pbrier | 0:18ead85c200b | 74 | return serial->getc(); |
pbrier | 0:18ead85c200b | 75 | else |
pbrier | 0:18ead85c200b | 76 | return 0; |
pbrier | 0:18ead85c200b | 77 | } |
pbrier | 0:18ead85c200b | 78 | |
pbrier | 0:18ead85c200b | 79 | |
pbrier | 0:18ead85c200b | 80 | |
pbrier | 0:18ead85c200b | 81 | /** |
pbrier | 0:18ead85c200b | 82 | *** Screens are defined with: |
pbrier | 0:18ead85c200b | 83 | *** name, line[2], int* i[4], char *s; |
pbrier | 0:18ead85c200b | 84 | *** |
pbrier | 0:18ead85c200b | 85 | *** in the lines, the following replacements are made: |
pbrier | 0:18ead85c200b | 86 | *** $$$$$$ : "$" chars are replaced with the string argument for this screen (left alligned), if argument is NULL, spaces are inserted |
pbrier | 0:18ead85c200b | 87 | *** +9876543210: "+" and numbers are replaced with decimal digits from the integer arguments (the "+" sign is replaced with '-' if the argument is negative |
pbrier | 0:18ead85c200b | 88 | *** |
pbrier | 0:18ead85c200b | 89 | *** |
pbrier | 0:18ead85c200b | 90 | **/ |
pbrier | 0:18ead85c200b | 91 | inline char digit(int i, int n) |
pbrier | 0:18ead85c200b | 92 | { |
pbrier | 0:18ead85c200b | 93 | int d=1; |
pbrier | 0:18ead85c200b | 94 | char c; |
pbrier | 0:18ead85c200b | 95 | if ( i<0 ) i *= -1; |
pbrier | 0:18ead85c200b | 96 | while(n--) d *= 10; |
pbrier | 0:18ead85c200b | 97 | c = '0' + ((i/d) % 10); |
pbrier | 0:18ead85c200b | 98 | return c; |
pbrier | 0:18ead85c200b | 99 | } |
pbrier | 0:18ead85c200b | 100 | void LaosDisplay::ShowScreen(const char *l, int *arg, char *s) |
pbrier | 0:18ead85c200b | 101 | { |
pbrier | 0:18ead85c200b | 102 | char c, next=0,surpress=1; |
pbrier | 0:18ead85c200b | 103 | cls(); |
pbrier | 0:18ead85c200b | 104 | while ( (c=*l) ) |
pbrier | 0:18ead85c200b | 105 | { |
pbrier | 0:18ead85c200b | 106 | switch ( c ) |
pbrier | 0:18ead85c200b | 107 | { |
pbrier | 0:18ead85c200b | 108 | case '$': |
pbrier | 0:18ead85c200b | 109 | if (s != NULL && *s) |
pbrier | 0:18ead85c200b | 110 | c = *s++; |
pbrier | 0:18ead85c200b | 111 | else |
pbrier | 0:18ead85c200b | 112 | c = ' '; |
pbrier | 0:18ead85c200b | 113 | break; |
pbrier | 0:18ead85c200b | 114 | |
pbrier | 0:18ead85c200b | 115 | case '+': |
pbrier | 0:18ead85c200b | 116 | if (arg != NULL && *arg < 0) |
pbrier | 0:18ead85c200b | 117 | c = '-'; |
pbrier | 0:18ead85c200b | 118 | else |
pbrier | 0:18ead85c200b | 119 | c = '+'; |
pbrier | 0:18ead85c200b | 120 | break; |
pbrier | 0:18ead85c200b | 121 | |
pbrier | 0:18ead85c200b | 122 | case '0': next=1; surpress=0; |
pbrier | 0:18ead85c200b | 123 | case '1': case '2': case '3': case '4': |
pbrier | 0:18ead85c200b | 124 | case '5': case '6': case '7': case '8': case '9': |
pbrier | 0:18ead85c200b | 125 | char d = ' '; |
pbrier | 0:18ead85c200b | 126 | if (arg != NULL ) |
pbrier | 0:18ead85c200b | 127 | { |
pbrier | 0:18ead85c200b | 128 | d = digit(*arg, *l-'0'); |
pbrier | 0:18ead85c200b | 129 | if ( d == '0' && surpress ) // supress leading zeros |
pbrier | 0:18ead85c200b | 130 | d = ' '; |
pbrier | 0:18ead85c200b | 131 | else |
pbrier | 0:18ead85c200b | 132 | surpress = 0; |
pbrier | 0:18ead85c200b | 133 | if ( next && arg != NULL ) // take next numeric argument |
pbrier | 0:18ead85c200b | 134 | { |
pbrier | 0:18ead85c200b | 135 | arg++; |
pbrier | 0:18ead85c200b | 136 | next=0; |
pbrier | 0:18ead85c200b | 137 | surpress=1; |
pbrier | 0:18ead85c200b | 138 | } |
pbrier | 0:18ead85c200b | 139 | } |
pbrier | 0:18ead85c200b | 140 | c = d; |
pbrier | 0:18ead85c200b | 141 | break; |
pbrier | 0:18ead85c200b | 142 | } |
pbrier | 0:18ead85c200b | 143 | write(c); |
pbrier | 0:18ead85c200b | 144 | l++; |
pbrier | 0:18ead85c200b | 145 | } |
pbrier | 0:18ead85c200b | 146 | } |
pbrier | 0:18ead85c200b | 147 | |
pbrier | 0:18ead85c200b | 148 | |
pbrier | 0:18ead85c200b | 149 | // EOF |