1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/Alphanumeric.cpp Sun Jan 10 00:30:41 2016 +0100
1.3 @@ -0,0 +1,163 @@
1.4 +/*
1.5 +Interfacing the Arduino Duemilanove to the ElecFreaks alphanumeric display
1.6 +brick.
1.7 +
1.8 +Copyright (C) 2016 Paul Boddie <paul@boddie.org.uk>
1.9 +
1.10 +This program is free software; you can redistribute it and/or modify it under
1.11 +the terms of the GNU General Public License as published by the Free Software
1.12 +Foundation; either version 3 of the License, or (at your option) any later
1.13 +version.
1.14 +
1.15 +This program is distributed in the hope that it will be useful, but WITHOUT ANY
1.16 +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
1.17 +PARTICULAR PURPOSE. See the GNU General Public License for more details.
1.18 +
1.19 +You should have received a copy of the GNU General Public License along
1.20 +with this program. If not, see <http://www.gnu.org/licenses/>.
1.21 +*/
1.22 +
1.23 +#include <Wire.h>
1.24 +
1.25 +const int BUFSIZE = 17;
1.26 +char inbuffer[BUFSIZE];
1.27 +uint8_t nread = 0;
1.28 +
1.29 +uint8_t data[8] = {0, 0, 0, 0, 0, 0, 0, 0};
1.30 +
1.31 +void enable_clock()
1.32 +{
1.33 + Wire.beginTransmission(0x70);
1.34 + Wire.write(0x21);
1.35 + Wire.endTransmission();
1.36 +}
1.37 +
1.38 +void set_row_output()
1.39 +{
1.40 + Wire.beginTransmission(0x70);
1.41 + Wire.write(0xa0);
1.42 + Wire.endTransmission();
1.43 +}
1.44 +
1.45 +void set_dimming()
1.46 +{
1.47 + Wire.beginTransmission(0x70);
1.48 + Wire.write(0xe3); // pulse_width = 4/16
1.49 + Wire.endTransmission();
1.50 +}
1.51 +
1.52 +void enable_display()
1.53 +{
1.54 + Wire.beginTransmission(0x70);
1.55 + Wire.write(0x81); // no blinking
1.56 + Wire.endTransmission();
1.57 +}
1.58 +
1.59 +void disable_display()
1.60 +{
1.61 + Wire.beginTransmission(0x70);
1.62 + Wire.write(0x80);
1.63 + Wire.endTransmission();
1.64 +}
1.65 +
1.66 +void init_alphanumeric()
1.67 +{
1.68 + enable_clock();
1.69 + set_row_output();
1.70 + set_dimming();
1.71 +}
1.72 +
1.73 +void write_digits(uint8_t data[], uint8_t len)
1.74 +{
1.75 + uint8_t i;
1.76 +
1.77 + Wire.beginTransmission(0x70);
1.78 + Wire.write(0x00); // address = 0
1.79 +
1.80 + for (i = 0; i < len; i++)
1.81 + {
1.82 + Wire.write(data[i]);
1.83 + }
1.84 +
1.85 + Wire.endTransmission();
1.86 +}
1.87 +
1.88 +/* User interface functions. */
1.89 +
1.90 +uint8_t fromHex(char c)
1.91 +{
1.92 + if ((c >= 48) && (c <= 57))
1.93 + return c - 48;
1.94 + if ((c >= 65) && (c <= 70))
1.95 + return c - 65 + 10;
1.96 + if ((c >= 97) && (c <= 102))
1.97 + return c - 97 + 10;
1.98 + return 0;
1.99 +}
1.100 +
1.101 +void to_digits(char buffer[], uint8_t data[], uint8_t len)
1.102 +{
1.103 + uint8_t i, j, p, high, low;
1.104 +
1.105 + for (i = 0, j = 0; j < len; j += 2)
1.106 + {
1.107 + for (p = 2; p > 0; p--, i += 2)
1.108 + {
1.109 + high = fromHex(buffer[i]);
1.110 + if (high == 17)
1.111 + return;
1.112 + low = fromHex(buffer[i+1]);
1.113 + if (low == 17)
1.114 + return;
1.115 +
1.116 + /* Switch to little-endian. */
1.117 +
1.118 + data[j+p-1] = (high << 4) + low;
1.119 + }
1.120 + }
1.121 +}
1.122 +
1.123 +void setup()
1.124 +{
1.125 + Wire.begin();
1.126 + Serial.begin(115200);
1.127 +
1.128 + init_alphanumeric();
1.129 + write_digits(data, 8);
1.130 + enable_display();
1.131 +
1.132 + // Interface loop.
1.133 +
1.134 + Serial.println("?");
1.135 +}
1.136 +
1.137 +void loop()
1.138 +{
1.139 + /* Read bytes, obtaining the number read excluding any newline terminator. */
1.140 +
1.141 + if (nread += Serial.readBytesUntil('\n', inbuffer + nread, BUFSIZE - nread))
1.142 + {
1.143 + /* Handle each command, waiting for the newline. */
1.144 +
1.145 + if (nread >= 16)
1.146 + {
1.147 + to_digits(inbuffer, data, 8);
1.148 + write_digits(data, 8);
1.149 + nread = 0;
1.150 + Serial.println("OK");
1.151 + }
1.152 + else
1.153 + {
1.154 + Serial.print(nread);
1.155 + Serial.println("...");
1.156 + }
1.157 +
1.158 + Serial.flush();
1.159 + }
1.160 +}
1.161 +
1.162 +extern "C" void __cxa_pure_virtual(void) {
1.163 + while(1);
1.164 +}
1.165 +
1.166 +// tabstop=4 expandtab shiftwidth=4