The London Hackspace bandwidth meter
Dependencies: LPD8806 MODSERIAL mbed picojson
See:
- https://github.com/londonhackspace/net-o-meter
- http://wiki.london.hackspace.org.uk/view/Projects/BandwidthMeter
vfd.cpp@4:7087ea3d13c1, 2012-08-27 (annotated)
- Committer:
- Jasper
- Date:
- Mon Aug 27 01:32:52 2012 +0000
- Revision:
- 4:7087ea3d13c1
- Parent:
- 3:7fca72f96711
- Child:
- 5:24172484b8c5
much fiddlign later, better cmd processor on the serial interface, give up on netcode for the moment
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jasper | 3:7fca72f96711 | 1 | /* |
Jasper | 3:7fca72f96711 | 2 | * The VFD code |
Jasper | 3:7fca72f96711 | 3 | * |
Jasper | 3:7fca72f96711 | 4 | * for a futuba M204LD01AA |
Jasper | 3:7fca72f96711 | 5 | * |
Jasper | 3:7fca72f96711 | 6 | * |
Jasper | 3:7fca72f96711 | 7 | * p27 -> input, busy |
Jasper | 3:7fca72f96711 | 8 | * p28 -> serial data in |
Jasper | 3:7fca72f96711 | 9 | * p29 -> clock |
Jasper | 3:7fca72f96711 | 10 | * p30 -> latch |
Jasper | 3:7fca72f96711 | 11 | * |
Jasper | 3:7fca72f96711 | 12 | * |
Jasper | 3:7fca72f96711 | 13 | */ |
Jasper | 3:7fca72f96711 | 14 | |
Jasper | 3:7fca72f96711 | 15 | #include "mbed.h" |
Jasper | 3:7fca72f96711 | 16 | #include "vfd.h" |
Jasper | 4:7087ea3d13c1 | 17 | #include "Stream.h" |
Jasper | 4:7087ea3d13c1 | 18 | #include "FunctionPointer.h" |
Jasper | 3:7fca72f96711 | 19 | |
Jasper | 3:7fca72f96711 | 20 | // InturruptIn busy_intr(p27) |
Jasper | 3:7fca72f96711 | 21 | |
Jasper | 3:7fca72f96711 | 22 | DigitalIn vfd_p_busy(p27); |
Jasper | 3:7fca72f96711 | 23 | DigitalOut vfd_p_data(p28); |
Jasper | 3:7fca72f96711 | 24 | DigitalOut vfd_p_clock(p29); |
Jasper | 3:7fca72f96711 | 25 | DigitalOut vfd_p_latch(p30); |
Jasper | 3:7fca72f96711 | 26 | |
Jasper | 3:7fca72f96711 | 27 | /* current state */ |
Jasper | 3:7fca72f96711 | 28 | int c_data; |
Jasper | 3:7fca72f96711 | 29 | bool c_a0; |
Jasper | 3:7fca72f96711 | 30 | bool c_blank; |
Jasper | 3:7fca72f96711 | 31 | |
Jasper | 4:7087ea3d13c1 | 32 | bool vfd_dead = 0; |
Jasper | 4:7087ea3d13c1 | 33 | |
Jasper | 3:7fca72f96711 | 34 | void vfd_shift_clock(void) { |
Jasper | 3:7fca72f96711 | 35 | vfd_p_clock = 0; |
Jasper | 3:7fca72f96711 | 36 | wait_us(100); |
Jasper | 3:7fca72f96711 | 37 | vfd_p_clock = 1; |
Jasper | 3:7fca72f96711 | 38 | wait_us(100); |
Jasper | 3:7fca72f96711 | 39 | } |
Jasper | 3:7fca72f96711 | 40 | |
Jasper | 4:7087ea3d13c1 | 41 | void vfd_doit(int data, int mode, int wr, int ss, int reset) { |
Jasper | 3:7fca72f96711 | 42 | int i, bit; |
Jasper | 3:7fca72f96711 | 43 | |
Jasper | 3:7fca72f96711 | 44 | vfd_p_data = 0; // nc |
Jasper | 3:7fca72f96711 | 45 | vfd_shift_clock(); |
Jasper | 3:7fca72f96711 | 46 | vfd_p_data = 0; // nc |
Jasper | 3:7fca72f96711 | 47 | vfd_shift_clock(); |
Jasper | 3:7fca72f96711 | 48 | |
Jasper | 4:7087ea3d13c1 | 49 | vfd_p_data = reset; // reset, 0 = in reset |
Jasper | 3:7fca72f96711 | 50 | vfd_shift_clock(); |
Jasper | 3:7fca72f96711 | 51 | |
Jasper | 3:7fca72f96711 | 52 | vfd_p_data = c_blank; // Blank, 0 = blanked |
Jasper | 3:7fca72f96711 | 53 | vfd_shift_clock(); |
Jasper | 3:7fca72f96711 | 54 | |
Jasper | 3:7fca72f96711 | 55 | vfd_p_data = ss; // SS, clockish |
Jasper | 3:7fca72f96711 | 56 | vfd_shift_clock(); |
Jasper | 3:7fca72f96711 | 57 | |
Jasper | 3:7fca72f96711 | 58 | vfd_p_data = 1; // rd |
Jasper | 3:7fca72f96711 | 59 | vfd_shift_clock(); |
Jasper | 3:7fca72f96711 | 60 | |
Jasper | 3:7fca72f96711 | 61 | vfd_p_data = mode; // A0, data/command |
Jasper | 3:7fca72f96711 | 62 | vfd_shift_clock(); |
Jasper | 3:7fca72f96711 | 63 | |
Jasper | 3:7fca72f96711 | 64 | vfd_p_data = wr; // WR |
Jasper | 3:7fca72f96711 | 65 | vfd_shift_clock(); |
Jasper | 3:7fca72f96711 | 66 | |
Jasper | 3:7fca72f96711 | 67 | for (i = 0 ; i < 8; i++) { |
Jasper | 3:7fca72f96711 | 68 | bit = (data & (1 << i)) >> i; |
Jasper | 3:7fca72f96711 | 69 | vfd_p_data = bit; |
Jasper | 3:7fca72f96711 | 70 | vfd_shift_clock(); |
Jasper | 3:7fca72f96711 | 71 | } |
Jasper | 3:7fca72f96711 | 72 | |
Jasper | 3:7fca72f96711 | 73 | vfd_p_latch = 0; |
Jasper | 3:7fca72f96711 | 74 | wait_us(10); |
Jasper | 3:7fca72f96711 | 75 | vfd_p_latch = 1; |
Jasper | 3:7fca72f96711 | 76 | wait_us(10); |
Jasper | 3:7fca72f96711 | 77 | } |
Jasper | 3:7fca72f96711 | 78 | |
Jasper | 3:7fca72f96711 | 79 | /* mode == 1 if command */ |
Jasper | 4:7087ea3d13c1 | 80 | void vfd_send(int data, int mode, int force) |
Jasper | 3:7fca72f96711 | 81 | { |
Jasper | 3:7fca72f96711 | 82 | data = data & 0xff; |
Jasper | 3:7fca72f96711 | 83 | c_data = data; |
Jasper | 3:7fca72f96711 | 84 | c_blank = 1; |
Jasper | 3:7fca72f96711 | 85 | |
Jasper | 4:7087ea3d13c1 | 86 | if (mode == 0) |
Jasper | 4:7087ea3d13c1 | 87 | printf("%02x ", data); |
Jasper | 4:7087ea3d13c1 | 88 | |
Jasper | 4:7087ea3d13c1 | 89 | if (vfd_dead && ! force) { |
Jasper | 4:7087ea3d13c1 | 90 | printf("vfd dead (?), not sending.\r\n"); |
Jasper | 4:7087ea3d13c1 | 91 | return; |
Jasper | 4:7087ea3d13c1 | 92 | } |
Jasper | 4:7087ea3d13c1 | 93 | if (force) |
Jasper | 4:7087ea3d13c1 | 94 | printf("forcing vfd for 0x%02x %d\r\n", data, mode); |
Jasper | 4:7087ea3d13c1 | 95 | |
Jasper | 4:7087ea3d13c1 | 96 | if (vfd_p_busy) { |
Jasper | 3:7fca72f96711 | 97 | printf("pre, busy: %d\r\n", vfd_p_busy.read()); |
Jasper | 4:7087ea3d13c1 | 98 | } |
Jasper | 3:7fca72f96711 | 99 | |
Jasper | 3:7fca72f96711 | 100 | if (vfd_p_busy) |
Jasper | 3:7fca72f96711 | 101 | { |
Jasper | 3:7fca72f96711 | 102 | wait_ms(4); // should loop, 3.1 ms is max according to the data sheet. |
Jasper | 3:7fca72f96711 | 103 | if (vfd_p_busy) |
Jasper | 3:7fca72f96711 | 104 | { |
Jasper | 3:7fca72f96711 | 105 | printf("still busy :(\r\n"); |
Jasper | 4:7087ea3d13c1 | 106 | printf("resetting\r\n"); |
Jasper | 4:7087ea3d13c1 | 107 | if (!force) |
Jasper | 4:7087ea3d13c1 | 108 | vfd_reset(); |
Jasper | 4:7087ea3d13c1 | 109 | if (vfd_p_busy) { |
Jasper | 4:7087ea3d13c1 | 110 | printf("still busy after reset!\r\n"); |
Jasper | 4:7087ea3d13c1 | 111 | if (!force) |
Jasper | 4:7087ea3d13c1 | 112 | return; |
Jasper | 4:7087ea3d13c1 | 113 | } |
Jasper | 4:7087ea3d13c1 | 114 | if (!force) |
Jasper | 4:7087ea3d13c1 | 115 | return; |
Jasper | 3:7fca72f96711 | 116 | } |
Jasper | 3:7fca72f96711 | 117 | } |
Jasper | 3:7fca72f96711 | 118 | |
Jasper | 3:7fca72f96711 | 119 | /* wr,ss */ |
Jasper | 4:7087ea3d13c1 | 120 | vfd_doit(data, mode, 0, 0, 1); |
Jasper | 3:7fca72f96711 | 121 | wait_us(1); |
Jasper | 4:7087ea3d13c1 | 122 | vfd_doit(data, mode, 1, 0, 1); |
Jasper | 3:7fca72f96711 | 123 | wait_us(1); |
Jasper | 4:7087ea3d13c1 | 124 | vfd_doit(data, mode, 1, 1, 1); |
Jasper | 3:7fca72f96711 | 125 | wait_us(1); |
Jasper | 3:7fca72f96711 | 126 | |
Jasper | 3:7fca72f96711 | 127 | if (vfd_p_busy) |
Jasper | 3:7fca72f96711 | 128 | printf("post1, busy: %d\r\n", vfd_p_busy.read()); |
Jasper | 3:7fca72f96711 | 129 | |
Jasper | 4:7087ea3d13c1 | 130 | vfd_doit(data, mode, 0, 0, 1); |
Jasper | 4:7087ea3d13c1 | 131 | wait_ms(4); // loop and wait for not busy |
Jasper | 3:7fca72f96711 | 132 | |
Jasper | 3:7fca72f96711 | 133 | if (vfd_p_busy) |
Jasper | 3:7fca72f96711 | 134 | printf("post2, busy: %d\r\n", vfd_p_busy.read()); |
Jasper | 3:7fca72f96711 | 135 | |
Jasper | 3:7fca72f96711 | 136 | while (vfd_p_busy) { |
Jasper | 3:7fca72f96711 | 137 | wait_us(250); |
Jasper | 3:7fca72f96711 | 138 | printf("post-while, busy: %d\r\n", vfd_p_busy.read()); |
Jasper | 3:7fca72f96711 | 139 | } |
Jasper | 3:7fca72f96711 | 140 | } |
Jasper | 3:7fca72f96711 | 141 | |
Jasper | 4:7087ea3d13c1 | 142 | void vfd_command(int data, int force) { |
Jasper | 4:7087ea3d13c1 | 143 | vfd_send(data, 1, force); |
Jasper | 3:7fca72f96711 | 144 | } |
Jasper | 3:7fca72f96711 | 145 | |
Jasper | 3:7fca72f96711 | 146 | void vfd_data(int data) { |
Jasper | 4:7087ea3d13c1 | 147 | if ((data & 0xff) == 0x7f) { |
Jasper | 4:7087ea3d13c1 | 148 | data = 0x20; |
Jasper | 4:7087ea3d13c1 | 149 | printf("changed data to %02x\r\n", data); |
Jasper | 4:7087ea3d13c1 | 150 | } |
Jasper | 4:7087ea3d13c1 | 151 | vfd_send(data, 0, 0); |
Jasper | 3:7fca72f96711 | 152 | } |
Jasper | 3:7fca72f96711 | 153 | |
Jasper | 3:7fca72f96711 | 154 | void vfd_init(void) { |
Jasper | 4:7087ea3d13c1 | 155 | vfd_data(0x11); // normal mode |
Jasper | 3:7fca72f96711 | 156 | vfd_data(0x0c); // clear |
Jasper | 3:7fca72f96711 | 157 | vfd_data(0x1b); // esc |
Jasper | 3:7fca72f96711 | 158 | vfd_data(0); // v pos |
Jasper | 3:7fca72f96711 | 159 | vfd_data(0); // h pos |
Jasper | 3:7fca72f96711 | 160 | } |
Jasper | 3:7fca72f96711 | 161 | |
Jasper | 3:7fca72f96711 | 162 | void vfd_blank(void) { |
Jasper | 3:7fca72f96711 | 163 | /* blank */ |
Jasper | 3:7fca72f96711 | 164 | } |
Jasper | 3:7fca72f96711 | 165 | |
Jasper | 3:7fca72f96711 | 166 | void vfd_show(void) { |
Jasper | 3:7fca72f96711 | 167 | /* unblank */ |
Jasper | 3:7fca72f96711 | 168 | } |
Jasper | 3:7fca72f96711 | 169 | |
Jasper | 4:7087ea3d13c1 | 170 | void vfd_pos(int h, int v) { |
Jasper | 4:7087ea3d13c1 | 171 | if (v > 3 || v < 0) |
Jasper | 4:7087ea3d13c1 | 172 | return; |
Jasper | 4:7087ea3d13c1 | 173 | if (h > 19 || h < 0) |
Jasper | 4:7087ea3d13c1 | 174 | return; |
Jasper | 4:7087ea3d13c1 | 175 | vfd_data(0x1b); |
Jasper | 4:7087ea3d13c1 | 176 | vfd_data(v); |
Jasper | 4:7087ea3d13c1 | 177 | vfd_data(h); |
Jasper | 4:7087ea3d13c1 | 178 | } |
Jasper | 3:7fca72f96711 | 179 | |
Jasper | 4:7087ea3d13c1 | 180 | void vfd_reset(void) { |
Jasper | 4:7087ea3d13c1 | 181 | int i, data, mode; |
Jasper | 4:7087ea3d13c1 | 182 | |
Jasper | 4:7087ea3d13c1 | 183 | data = 0; mode = 1; |
Jasper | 4:7087ea3d13c1 | 184 | |
Jasper | 4:7087ea3d13c1 | 185 | printf("reset sent, busy: %d\r\n", vfd_p_busy.read()); |
Jasper | 4:7087ea3d13c1 | 186 | |
Jasper | 4:7087ea3d13c1 | 187 | /* everything low, includeing reset */ |
Jasper | 4:7087ea3d13c1 | 188 | for (i = 0 ; i < 16; i++) { |
Jasper | 4:7087ea3d13c1 | 189 | vfd_p_data = 0; |
Jasper | 4:7087ea3d13c1 | 190 | vfd_shift_clock(); |
Jasper | 4:7087ea3d13c1 | 191 | } |
Jasper | 4:7087ea3d13c1 | 192 | vfd_p_latch = 0; |
Jasper | 4:7087ea3d13c1 | 193 | wait_us(10); |
Jasper | 4:7087ea3d13c1 | 194 | vfd_p_latch = 1; |
Jasper | 4:7087ea3d13c1 | 195 | |
Jasper | 4:7087ea3d13c1 | 196 | vfd_doit(data, mode, 0, 0, 1); |
Jasper | 4:7087ea3d13c1 | 197 | wait_us(1); |
Jasper | 4:7087ea3d13c1 | 198 | vfd_doit(data, mode, 1, 0, 1); |
Jasper | 4:7087ea3d13c1 | 199 | wait_us(1); |
Jasper | 4:7087ea3d13c1 | 200 | vfd_doit(data, mode, 1, 1, 1); |
Jasper | 4:7087ea3d13c1 | 201 | wait_us(1); |
Jasper | 4:7087ea3d13c1 | 202 | |
Jasper | 4:7087ea3d13c1 | 203 | if (vfd_p_busy) |
Jasper | 4:7087ea3d13c1 | 204 | printf("post1, busy: %d\r\n", vfd_p_busy.read()); |
Jasper | 4:7087ea3d13c1 | 205 | |
Jasper | 4:7087ea3d13c1 | 206 | vfd_doit(data, mode, 0, 0, 1); |
Jasper | 4:7087ea3d13c1 | 207 | wait_ms(1); // loop and wait for not busy |
Jasper | 4:7087ea3d13c1 | 208 | |
Jasper | 4:7087ea3d13c1 | 209 | printf("reset sent, busy: %d\r\n", vfd_p_busy.read()); |
Jasper | 4:7087ea3d13c1 | 210 | wait_ms(10); |
Jasper | 4:7087ea3d13c1 | 211 | if (vfd_p_busy) { |
Jasper | 4:7087ea3d13c1 | 212 | printf("still busy, not calling vfd_init()\r\n"); |
Jasper | 4:7087ea3d13c1 | 213 | vfd_reset_cmd(); |
Jasper | 4:7087ea3d13c1 | 214 | printf("post reset_cmd\r\n"); |
Jasper | 4:7087ea3d13c1 | 215 | vfd_dead = 1; |
Jasper | 4:7087ea3d13c1 | 216 | wait_ms(10); |
Jasper | 4:7087ea3d13c1 | 217 | } else { |
Jasper | 4:7087ea3d13c1 | 218 | if (!vfd_dead) |
Jasper | 4:7087ea3d13c1 | 219 | vfd_init(); |
Jasper | 4:7087ea3d13c1 | 220 | } |
Jasper | 4:7087ea3d13c1 | 221 | } |
Jasper | 4:7087ea3d13c1 | 222 | |
Jasper | 4:7087ea3d13c1 | 223 | void vfd_reset_cmd(void) { |
Jasper | 4:7087ea3d13c1 | 224 | vfd_command(0xff, 1); |
Jasper | 4:7087ea3d13c1 | 225 | } |
Jasper | 4:7087ea3d13c1 | 226 | |
Jasper | 4:7087ea3d13c1 | 227 | /* |
Jasper | 4:7087ea3d13c1 | 228 | class Vfd : Public Stream { |
Jasper | 4:7087ea3d13c1 | 229 | public: |
Jasper | 4:7087ea3d13c1 | 230 | |
Jasper | 4:7087ea3d13c1 | 231 | |
Jasper | 4:7087ea3d13c1 | 232 | } |
Jasper | 4:7087ea3d13c1 | 233 | */ |
Jasper | 4:7087ea3d13c1 | 234 | |
Jasper | 4:7087ea3d13c1 | 235 |