# HG changeset patch # User Paul Boddie # Date 1475353867 -7200 # Node ID 03fdf31325dfbd941e829c0d0138c1974ec2a3ba # Parent 09866ccb6a1d7d0acf7699ddfea99c3b711af85c Renamed the program source file. diff -r 09866ccb6a1d -r 03fdf31325df Alphanumeric.cpp --- a/Alphanumeric.cpp Wed Sep 28 15:45:09 2016 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,278 +0,0 @@ -/* -Interfacing the Arduino Duemilanove to the ElecFreaks alphanumeric display -brick. - -Copyright (C) 2016 Paul Boddie - -This program is free software; you can redistribute it and/or modify it under -the terms of the GNU General Public License as published by the Free Software -Foundation; either version 3 of the License, or (at your option) any later -version. - -This program is distributed in the hope that it will be useful, but WITHOUT ANY -WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -PARTICULAR PURPOSE. See the GNU General Public License for more details. - -You should have received a copy of the GNU General Public License along -with this program. If not, see . -*/ - -#include -#include "shapes.h" - -/* Multiplexer circuit definitions. */ - -const uint8_t MUX_S = 3, - D0 = 4, D1 = 5, D2 = 6, D3 = 7, - D4 = 8, D5 = 9, D6 = 10, D7 = 11; - -const uint8_t datapins[8] = {D7, D6, D5, D4, D3, D2, D1, D0}; - -const uint16_t digits[] = { - OUTER_FRAME, - RIGHT_EDGE, - HORIZONTALS | UPPER_RIGHT_VERTICAL | LOWER_LEFT_VERTICAL, - HORIZONTALS | RIGHT_EDGE, - UPPER_LEFT_VERTICAL | MIDDLE_ELEMENTS | RIGHT_EDGE, - HORIZONTALS | UPPER_LEFT_VERTICAL | LOWER_RIGHT_VERTICAL, - HORIZONTALS | LEFT_EDGE | LOWER_RIGHT_VERTICAL, - TOP_EDGE | UPPER_RIGHT_DIAGONAL | LOWER_LEFT_DIAGONAL, - OUTER_FRAME | MIDDLE_ELEMENTS, - HORIZONTALS | RIGHT_EDGE | UPPER_LEFT_VERTICAL, - HORIZONTALS | RIGHT_EDGE | LOWER_LEFT_VERTICAL, - LEFT_EDGE | MIDDLE_ELEMENTS | BOTTOM_EDGE | LOWER_RIGHT_VERTICAL, - MIDDLE_ELEMENTS | BOTTOM_EDGE | LOWER_LEFT_VERTICAL, - RIGHT_EDGE | MIDDLE_ELEMENTS | BOTTOM_EDGE | LOWER_LEFT_VERTICAL, - HORIZONTALS | LEFT_EDGE | UPPER_RIGHT_VERTICAL, - LEFT_EDGE | TOP_EDGE | MIDDLE_ELEMENTS - }; - -/* Serial communications definitions. */ - -const int BUFSIZE = 17; -char inbuffer[BUFSIZE]; -uint8_t nread = 0; - -/* Display memory buffer. */ - -uint8_t data[8] = {0, 0, 0, 0, 0, 0, 0, 0}; - -/* Display operations. */ - -void enable_clock() -{ - Wire.beginTransmission(0x70); - Wire.write(0x21); - Wire.endTransmission(); -} - -void set_row_output() -{ - Wire.beginTransmission(0x70); - Wire.write(0xa0); - Wire.endTransmission(); -} - -void set_dimming() -{ - Wire.beginTransmission(0x70); - Wire.write(0xe3); // pulse_width = 4/16 - Wire.endTransmission(); -} - -void enable_display() -{ - Wire.beginTransmission(0x70); - Wire.write(0x81); // no blinking - Wire.endTransmission(); -} - -void disable_display() -{ - Wire.beginTransmission(0x70); - Wire.write(0x80); - Wire.endTransmission(); -} - -void init_alphanumeric() -{ - enable_clock(); - set_row_output(); - set_dimming(); -} - -void write_digits(uint8_t data[], uint8_t len) -{ - uint8_t i; - - Wire.beginTransmission(0x70); - Wire.write(0x00); // address = 0 - - for (i = 0; i < len; i++) - { - Wire.write(data[i]); - } - - Wire.endTransmission(); -} - -/* User interface functions. */ - -uint8_t fromHex(char c) -{ - if ((c >= 48) && (c <= 57)) - return c - 48; - if ((c >= 65) && (c <= 70)) - return c - 65 + 10; - if ((c >= 97) && (c <= 102)) - return c - 97 + 10; - return 0; -} - -void to_digits(char buffer[], uint8_t data[], uint8_t len) -{ - uint8_t i, j, p, high, low; - - for (i = 0, j = 0; j < len; j += 2) - { - for (p = 2; p > 0; p--, i += 2) - { - high = fromHex(buffer[i]); - if (high == 17) - return; - low = fromHex(buffer[i+1]); - if (low == 17) - return; - - /* Switch to little-endian. */ - - data[j+p-1] = (high << 4) + low; - } - } -} - -/* Data sampling functions. */ - -uint8_t sample_value(uint8_t start, uint8_t end) -{ - uint8_t bit, value; - - value = 0; - - for (bit = start; bit < end; bit++) - { - value = (value << 1) | digitalRead(datapins[bit]); - } - - return value; -} - -void sample_data(uint8_t data[], uint8_t start, uint8_t end) -{ - uint8_t low, high, value; - uint16_t digit_value; - - value = sample_value(start, end); - digit_value = digits[value]; - - /* Switch to little-endian. */ - - data[0] = (uint8_t) (digit_value & 0xff); - data[1] = (uint8_t) (digit_value >> 8); -} - -void scan_inputs(uint8_t data[]) -{ - digitalWrite(MUX_S, 0); - sample_data(&data[2], 4, 8); /* bits 3..0 */ - sample_data(&data[0], 0, 4); /* bits 7..4 */ - - digitalWrite(MUX_S, 1); - sample_data(&data[6], 4, 8); - sample_data(&data[4], 0, 4); -} - -/* Main functions. */ - -void client_setup() -{ - Wire.begin(); - Serial.begin(115200); - - init_alphanumeric(); - write_digits(data, 8); - enable_display(); - - // Interface loop. - - Serial.println("?"); -} - -void sample_setup() -{ - Wire.begin(); - - pinMode(MUX_S, OUTPUT); - pinMode(D0, INPUT); - pinMode(D1, INPUT); - pinMode(D2, INPUT); - pinMode(D3, INPUT); - pinMode(D4, INPUT); - pinMode(D5, INPUT); - pinMode(D6, INPUT); - pinMode(D7, INPUT); - - init_alphanumeric(); - write_digits(data, 8); - enable_display(); -} - -void setup() -{ - /* client_setup(); */ - sample_setup(); -} - -void sample_loop() -{ - scan_inputs(data); - write_digits(data, 8); - delay(250); -} - -void client_loop() -{ - /* Read bytes, obtaining the number read excluding any newline terminator. */ - - if (nread += Serial.readBytesUntil('\n', inbuffer + nread, BUFSIZE - nread)) - { - /* Handle each command, waiting for the newline. */ - - if (nread >= 16) - { - to_digits(inbuffer, data, 8); - write_digits(data, 8); - nread = 0; - Serial.println("OK"); - } - else - { - Serial.print(nread); - Serial.println("..."); - } - - Serial.flush(); - } -} - -void loop() -{ - /* client_loop(); */ - sample_loop(); -} - -extern "C" void __cxa_pure_virtual(void) { - while(1); -} - -// tabstop=4 expandtab shiftwidth=4 diff -r 09866ccb6a1d -r 03fdf31325df ArduinoAlphanumeric.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ArduinoAlphanumeric.cpp Sat Oct 01 22:31:07 2016 +0200 @@ -0,0 +1,278 @@ +/* +Interfacing the Arduino Duemilanove to the ElecFreaks alphanumeric display +brick. + +Copyright (C) 2016 Paul Boddie + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program. If not, see . +*/ + +#include +#include "shapes.h" + +/* Multiplexer circuit definitions. */ + +const uint8_t MUX_S = 3, + D0 = 4, D1 = 5, D2 = 6, D3 = 7, + D4 = 8, D5 = 9, D6 = 10, D7 = 11; + +const uint8_t datapins[8] = {D7, D6, D5, D4, D3, D2, D1, D0}; + +const uint16_t digits[] = { + OUTER_FRAME, + RIGHT_EDGE, + HORIZONTALS | UPPER_RIGHT_VERTICAL | LOWER_LEFT_VERTICAL, + HORIZONTALS | RIGHT_EDGE, + UPPER_LEFT_VERTICAL | MIDDLE_ELEMENTS | RIGHT_EDGE, + HORIZONTALS | UPPER_LEFT_VERTICAL | LOWER_RIGHT_VERTICAL, + HORIZONTALS | LEFT_EDGE | LOWER_RIGHT_VERTICAL, + TOP_EDGE | UPPER_RIGHT_DIAGONAL | LOWER_LEFT_DIAGONAL, + OUTER_FRAME | MIDDLE_ELEMENTS, + HORIZONTALS | RIGHT_EDGE | UPPER_LEFT_VERTICAL, + HORIZONTALS | RIGHT_EDGE | LOWER_LEFT_VERTICAL, + LEFT_EDGE | MIDDLE_ELEMENTS | BOTTOM_EDGE | LOWER_RIGHT_VERTICAL, + MIDDLE_ELEMENTS | BOTTOM_EDGE | LOWER_LEFT_VERTICAL, + RIGHT_EDGE | MIDDLE_ELEMENTS | BOTTOM_EDGE | LOWER_LEFT_VERTICAL, + HORIZONTALS | LEFT_EDGE | UPPER_RIGHT_VERTICAL, + LEFT_EDGE | TOP_EDGE | MIDDLE_ELEMENTS + }; + +/* Serial communications definitions. */ + +const int BUFSIZE = 17; +char inbuffer[BUFSIZE]; +uint8_t nread = 0; + +/* Display memory buffer. */ + +uint8_t data[8] = {0, 0, 0, 0, 0, 0, 0, 0}; + +/* Display operations. */ + +void enable_clock() +{ + Wire.beginTransmission(0x70); + Wire.write(0x21); + Wire.endTransmission(); +} + +void set_row_output() +{ + Wire.beginTransmission(0x70); + Wire.write(0xa0); + Wire.endTransmission(); +} + +void set_dimming() +{ + Wire.beginTransmission(0x70); + Wire.write(0xe3); // pulse_width = 4/16 + Wire.endTransmission(); +} + +void enable_display() +{ + Wire.beginTransmission(0x70); + Wire.write(0x81); // no blinking + Wire.endTransmission(); +} + +void disable_display() +{ + Wire.beginTransmission(0x70); + Wire.write(0x80); + Wire.endTransmission(); +} + +void init_alphanumeric() +{ + enable_clock(); + set_row_output(); + set_dimming(); +} + +void write_digits(uint8_t data[], uint8_t len) +{ + uint8_t i; + + Wire.beginTransmission(0x70); + Wire.write(0x00); // address = 0 + + for (i = 0; i < len; i++) + { + Wire.write(data[i]); + } + + Wire.endTransmission(); +} + +/* User interface functions. */ + +uint8_t fromHex(char c) +{ + if ((c >= 48) && (c <= 57)) + return c - 48; + if ((c >= 65) && (c <= 70)) + return c - 65 + 10; + if ((c >= 97) && (c <= 102)) + return c - 97 + 10; + return 0; +} + +void to_digits(char buffer[], uint8_t data[], uint8_t len) +{ + uint8_t i, j, p, high, low; + + for (i = 0, j = 0; j < len; j += 2) + { + for (p = 2; p > 0; p--, i += 2) + { + high = fromHex(buffer[i]); + if (high == 17) + return; + low = fromHex(buffer[i+1]); + if (low == 17) + return; + + /* Switch to little-endian. */ + + data[j+p-1] = (high << 4) + low; + } + } +} + +/* Data sampling functions. */ + +uint8_t sample_value(uint8_t start, uint8_t end) +{ + uint8_t bit, value; + + value = 0; + + for (bit = start; bit < end; bit++) + { + value = (value << 1) | digitalRead(datapins[bit]); + } + + return value; +} + +void sample_data(uint8_t data[], uint8_t start, uint8_t end) +{ + uint8_t low, high, value; + uint16_t digit_value; + + value = sample_value(start, end); + digit_value = digits[value]; + + /* Switch to little-endian. */ + + data[0] = (uint8_t) (digit_value & 0xff); + data[1] = (uint8_t) (digit_value >> 8); +} + +void scan_inputs(uint8_t data[]) +{ + digitalWrite(MUX_S, 0); + sample_data(&data[2], 4, 8); /* bits 3..0 */ + sample_data(&data[0], 0, 4); /* bits 7..4 */ + + digitalWrite(MUX_S, 1); + sample_data(&data[6], 4, 8); + sample_data(&data[4], 0, 4); +} + +/* Main functions. */ + +void client_setup() +{ + Wire.begin(); + Serial.begin(115200); + + init_alphanumeric(); + write_digits(data, 8); + enable_display(); + + // Interface loop. + + Serial.println("?"); +} + +void sample_setup() +{ + Wire.begin(); + + pinMode(MUX_S, OUTPUT); + pinMode(D0, INPUT); + pinMode(D1, INPUT); + pinMode(D2, INPUT); + pinMode(D3, INPUT); + pinMode(D4, INPUT); + pinMode(D5, INPUT); + pinMode(D6, INPUT); + pinMode(D7, INPUT); + + init_alphanumeric(); + write_digits(data, 8); + enable_display(); +} + +void setup() +{ + /* client_setup(); */ + sample_setup(); +} + +void sample_loop() +{ + scan_inputs(data); + write_digits(data, 8); + delay(250); +} + +void client_loop() +{ + /* Read bytes, obtaining the number read excluding any newline terminator. */ + + if (nread += Serial.readBytesUntil('\n', inbuffer + nread, BUFSIZE - nread)) + { + /* Handle each command, waiting for the newline. */ + + if (nread >= 16) + { + to_digits(inbuffer, data, 8); + write_digits(data, 8); + nread = 0; + Serial.println("OK"); + } + else + { + Serial.print(nread); + Serial.println("..."); + } + + Serial.flush(); + } +} + +void loop() +{ + /* client_loop(); */ + sample_loop(); +} + +extern "C" void __cxa_pure_virtual(void) { + while(1); +} + +// tabstop=4 expandtab shiftwidth=4