paul@0 | 1 | /* |
paul@0 | 2 | Interfacing the Arduino Duemilanove to the ElecFreaks alphanumeric display |
paul@0 | 3 | brick. |
paul@0 | 4 | |
paul@0 | 5 | Copyright (C) 2016 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 6 | |
paul@0 | 7 | This program is free software; you can redistribute it and/or modify it under |
paul@0 | 8 | the terms of the GNU General Public License as published by the Free Software |
paul@0 | 9 | Foundation; either version 3 of the License, or (at your option) any later |
paul@0 | 10 | version. |
paul@0 | 11 | |
paul@0 | 12 | This program is distributed in the hope that it will be useful, but WITHOUT ANY |
paul@0 | 13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
paul@0 | 14 | PARTICULAR PURPOSE. See the GNU General Public License for more details. |
paul@0 | 15 | |
paul@0 | 16 | You should have received a copy of the GNU General Public License along |
paul@0 | 17 | with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@0 | 18 | */ |
paul@0 | 19 | |
paul@0 | 20 | #include <Wire.h> |
paul@0 | 21 | |
paul@0 | 22 | const int BUFSIZE = 17; |
paul@0 | 23 | char inbuffer[BUFSIZE]; |
paul@0 | 24 | uint8_t nread = 0; |
paul@0 | 25 | |
paul@0 | 26 | uint8_t data[8] = {0, 0, 0, 0, 0, 0, 0, 0}; |
paul@0 | 27 | |
paul@0 | 28 | void enable_clock() |
paul@0 | 29 | { |
paul@0 | 30 | Wire.beginTransmission(0x70); |
paul@0 | 31 | Wire.write(0x21); |
paul@0 | 32 | Wire.endTransmission(); |
paul@0 | 33 | } |
paul@0 | 34 | |
paul@0 | 35 | void set_row_output() |
paul@0 | 36 | { |
paul@0 | 37 | Wire.beginTransmission(0x70); |
paul@0 | 38 | Wire.write(0xa0); |
paul@0 | 39 | Wire.endTransmission(); |
paul@0 | 40 | } |
paul@0 | 41 | |
paul@0 | 42 | void set_dimming() |
paul@0 | 43 | { |
paul@0 | 44 | Wire.beginTransmission(0x70); |
paul@0 | 45 | Wire.write(0xe3); // pulse_width = 4/16 |
paul@0 | 46 | Wire.endTransmission(); |
paul@0 | 47 | } |
paul@0 | 48 | |
paul@0 | 49 | void enable_display() |
paul@0 | 50 | { |
paul@0 | 51 | Wire.beginTransmission(0x70); |
paul@0 | 52 | Wire.write(0x81); // no blinking |
paul@0 | 53 | Wire.endTransmission(); |
paul@0 | 54 | } |
paul@0 | 55 | |
paul@0 | 56 | void disable_display() |
paul@0 | 57 | { |
paul@0 | 58 | Wire.beginTransmission(0x70); |
paul@0 | 59 | Wire.write(0x80); |
paul@0 | 60 | Wire.endTransmission(); |
paul@0 | 61 | } |
paul@0 | 62 | |
paul@0 | 63 | void init_alphanumeric() |
paul@0 | 64 | { |
paul@0 | 65 | enable_clock(); |
paul@0 | 66 | set_row_output(); |
paul@0 | 67 | set_dimming(); |
paul@0 | 68 | } |
paul@0 | 69 | |
paul@0 | 70 | void write_digits(uint8_t data[], uint8_t len) |
paul@0 | 71 | { |
paul@0 | 72 | uint8_t i; |
paul@0 | 73 | |
paul@0 | 74 | Wire.beginTransmission(0x70); |
paul@0 | 75 | Wire.write(0x00); // address = 0 |
paul@0 | 76 | |
paul@0 | 77 | for (i = 0; i < len; i++) |
paul@0 | 78 | { |
paul@0 | 79 | Wire.write(data[i]); |
paul@0 | 80 | } |
paul@0 | 81 | |
paul@0 | 82 | Wire.endTransmission(); |
paul@0 | 83 | } |
paul@0 | 84 | |
paul@0 | 85 | /* User interface functions. */ |
paul@0 | 86 | |
paul@0 | 87 | uint8_t fromHex(char c) |
paul@0 | 88 | { |
paul@0 | 89 | if ((c >= 48) && (c <= 57)) |
paul@0 | 90 | return c - 48; |
paul@0 | 91 | if ((c >= 65) && (c <= 70)) |
paul@0 | 92 | return c - 65 + 10; |
paul@0 | 93 | if ((c >= 97) && (c <= 102)) |
paul@0 | 94 | return c - 97 + 10; |
paul@0 | 95 | return 0; |
paul@0 | 96 | } |
paul@0 | 97 | |
paul@0 | 98 | void to_digits(char buffer[], uint8_t data[], uint8_t len) |
paul@0 | 99 | { |
paul@0 | 100 | uint8_t i, j, p, high, low; |
paul@0 | 101 | |
paul@0 | 102 | for (i = 0, j = 0; j < len; j += 2) |
paul@0 | 103 | { |
paul@0 | 104 | for (p = 2; p > 0; p--, i += 2) |
paul@0 | 105 | { |
paul@0 | 106 | high = fromHex(buffer[i]); |
paul@0 | 107 | if (high == 17) |
paul@0 | 108 | return; |
paul@0 | 109 | low = fromHex(buffer[i+1]); |
paul@0 | 110 | if (low == 17) |
paul@0 | 111 | return; |
paul@0 | 112 | |
paul@0 | 113 | /* Switch to little-endian. */ |
paul@0 | 114 | |
paul@0 | 115 | data[j+p-1] = (high << 4) + low; |
paul@0 | 116 | } |
paul@0 | 117 | } |
paul@0 | 118 | } |
paul@0 | 119 | |
paul@0 | 120 | void setup() |
paul@0 | 121 | { |
paul@0 | 122 | Wire.begin(); |
paul@0 | 123 | Serial.begin(115200); |
paul@0 | 124 | |
paul@0 | 125 | init_alphanumeric(); |
paul@0 | 126 | write_digits(data, 8); |
paul@0 | 127 | enable_display(); |
paul@0 | 128 | |
paul@0 | 129 | // Interface loop. |
paul@0 | 130 | |
paul@0 | 131 | Serial.println("?"); |
paul@0 | 132 | } |
paul@0 | 133 | |
paul@0 | 134 | void loop() |
paul@0 | 135 | { |
paul@0 | 136 | /* Read bytes, obtaining the number read excluding any newline terminator. */ |
paul@0 | 137 | |
paul@0 | 138 | if (nread += Serial.readBytesUntil('\n', inbuffer + nread, BUFSIZE - nread)) |
paul@0 | 139 | { |
paul@0 | 140 | /* Handle each command, waiting for the newline. */ |
paul@0 | 141 | |
paul@0 | 142 | if (nread >= 16) |
paul@0 | 143 | { |
paul@0 | 144 | to_digits(inbuffer, data, 8); |
paul@0 | 145 | write_digits(data, 8); |
paul@0 | 146 | nread = 0; |
paul@0 | 147 | Serial.println("OK"); |
paul@0 | 148 | } |
paul@0 | 149 | else |
paul@0 | 150 | { |
paul@0 | 151 | Serial.print(nread); |
paul@0 | 152 | Serial.println("..."); |
paul@0 | 153 | } |
paul@0 | 154 | |
paul@0 | 155 | Serial.flush(); |
paul@0 | 156 | } |
paul@0 | 157 | } |
paul@0 | 158 | |
paul@0 | 159 | extern "C" void __cxa_pure_virtual(void) { |
paul@0 | 160 | while(1); |
paul@0 | 161 | } |
paul@0 | 162 | |
paul@0 | 163 | // tabstop=4 expandtab shiftwidth=4 |