6 x 7 segment display library for PCA9637 driven breakout board

Dependents:   FTSE100 InternetDispBoB digitalThermometer Counter ... more

Committer:
d_worrall
Date:
Mon Jul 04 08:42:21 2011 +0000
Revision:
13:38780808dc2f
Parent:
12:2235c72db927
Child:
14:10570d97f4d3
version8

Who changed what in which revision?

UserRevisionLine numberNew contents of line
d_worrall 0:843654413849 1 //NXP PCA9635 library
d_worrall 0:843654413849 2 //mbed Team - 28th June 2011
d_worrall 0:843654413849 3 //Daniel Worrall
d_worrall 0:843654413849 4
d_worrall 0:843654413849 5 #include "mbed.h"
d_worrall 0:843654413849 6 #include "PCA9635.h"
d_worrall 0:843654413849 7 #include "dispBoB.h"
d_worrall 0:843654413849 8
d_worrall 12:2235c72db927 9
d_worrall 9:44de04d410df 10 static const char loc[] = {0x02, 0x02, 0x04, 0x04, 0x10, 0x10, 0x12, 0x12};
d_worrall 9:44de04d410df 11 static const short dispL[] = {
d_worrall 6:1721cdf1024e 12 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
d_worrall 6:1721cdf1024e 13 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
d_worrall 12:2235c72db927 14 0x0000, 0x1008, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0002, 0xC006, 0x600C, 0x0000, 0x0000, 0x0000, 0x0001, 0x1000, 0x0000,
d_worrall 0:843654413849 15 0xE00E, 0x2008, 0xC00D, 0x600D, 0x200B, 0x6007, 0xE007, 0x200E, 0xE00F, 0x600F, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
d_worrall 6:1721cdf1024e 16
d_worrall 12:2235c72db927 17 0x0000, 0xE00D, 0xE003, 0xC001, 0xE009, 0xC007, 0x8007, 0x600F, 0xA003, 0x8000, 0x6008, 0xA007, 0xC002, 0xA005, 0xA001, 0xE001,
d_worrall 12:2235c72db927 18 0x800F, 0x200F, 0x8001, 0x6007, 0xC003, 0xE000, 0xE000, 0xE004, 0xA00B, 0x600B, 0xC00D, 0xC006, 0x0000, 0x600C, 0x0000, 0x4000,
d_worrall 0:843654413849 19 0x1000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
d_worrall 6:1721cdf1024e 20 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xC006, 0x0000, 0x600C, 0x0000, 0x0000
d_worrall 6:1721cdf1024e 21 };
d_worrall 9:44de04d410df 22 static const short dispR[] = {
d_worrall 6:1721cdf1024e 23 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
d_worrall 6:1721cdf1024e 24 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
d_worrall 12:2235c72db927 25 0x0000, 0x0180, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0020, 0x0C60, 0x06C0, 0x0000, 0x0000, 0x0000, 0x0010, 0x0100, 0x0000,
d_worrall 6:1721cdf1024e 26 0x0EE0, 0x0280, 0x0CD0, 0x06D0, 0x02B0, 0x0670, 0x0E70, 0x02E0, 0x0EF0, 0x06F0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
d_worrall 0:843654413849 27
d_worrall 12:2235c72db927 28 0x0000, 0x0ED0, 0x0E30, 0x0C10, 0x0E90, 0x0C70, 0x0870, 0x06F0, 0x0A30, 0x0800, 0x0680, 0x0A70, 0x0C20, 0x0A50, 0x0A10, 0x0E10,
d_worrall 12:2235c72db927 29 0x08F0, 0x02F0, 0x0810, 0x0670, 0x0C30, 0x0E00, 0x0E00, 0x0E40, 0x0AB0, 0x06B0, 0x0CD0, 0x0C60, 0x0000, 0x06C0, 0x0000, 0x0400,
d_worrall 0:843654413849 30 0x0100, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
d_worrall 6:1721cdf1024e 31 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0C60, 0x0000, 0x06C0, 0x0000, 0x0000
d_worrall 0:843654413849 32 };
d_worrall 9:44de04d410df 33 short element[8];
d_worrall 9:44de04d410df 34 short ppair[4];
d_worrall 0:843654413849 35 string nullArray = " ";
d_worrall 0:843654413849 36
d_worrall 0:843654413849 37 dispBoB::dispBoB(PinName sda, PinName scl, PinName en): _pca(sda, scl), _en(en) {
d_worrall 12:2235c72db927 38 init();
d_worrall 12:2235c72db927 39 }
d_worrall 12:2235c72db927 40 void dispBoB::init(void){ //initiate - call this whenever the system needs to be reset
d_worrall 0:843654413849 41 _en = 0;
d_worrall 9:44de04d410df 42 _pca.init(loc[0]);
d_worrall 0:843654413849 43 }
d_worrall 0:843654413849 44
d_worrall 12:2235c72db927 45 void dispBoB::cls(void){ //clear screen and screen buffers
d_worrall 0:843654413849 46 for(int j = 0; j < 8; j++){
d_worrall 0:843654413849 47 locate(j);
d_worrall 12:2235c72db927 48 element[j] = 0x0000;
d_worrall 0:843654413849 49 _pca.bus(0x0000);
d_worrall 0:843654413849 50 }
d_worrall 9:44de04d410df 51 locate(0);
d_worrall 0:843654413849 52 }
d_worrall 0:843654413849 53
d_worrall 0:843654413849 54 void dispBoB::locate(char pos){
d_worrall 9:44de04d410df 55 _pca.setAddress(loc[pos]);
d_worrall 0:843654413849 56 _cursor = pos;
d_worrall 0:843654413849 57 }
d_worrall 0:843654413849 58
d_worrall 9:44de04d410df 59 int dispBoB::_putc(int c){
d_worrall 12:2235c72db927 60 if(_cursor>6){
d_worrall 12:2235c72db927 61 _cursor++;
d_worrall 12:2235c72db927 62 return -1; //skip if exceeds display size allow extra '.' in 6th >> to place in 5th
d_worrall 12:2235c72db927 63 }
d_worrall 11:5e97be74f4d1 64
d_worrall 12:2235c72db927 65 if(c=='.'){ //choose not to print a '.' outside the display bounds
d_worrall 12:2235c72db927 66 if(_cursor>6){
d_worrall 12:2235c72db927 67 _cursor++;
d_worrall 12:2235c72db927 68 return -1;
d_worrall 12:2235c72db927 69 }
d_worrall 12:2235c72db927 70 if((_cursor==0)&&((element[_cursor]&0x1000)==0x1000)) element[_cursor] = 0x0000;
d_worrall 12:2235c72db927 71 if(_cursor!=0) _cursor--;
d_worrall 12:2235c72db927 72 if(((element[_cursor]&0x1000)==0x1000)||((element[_cursor]&0x0100)==0x0100)){
d_worrall 12:2235c72db927 73 _cursor++;
d_worrall 12:2235c72db927 74 element[_cursor]=0x0000;
d_worrall 12:2235c72db927 75 }
d_worrall 12:2235c72db927 76 } else {
d_worrall 12:2235c72db927 77 if(_cursor>5){ //for non '.' chars at end of line, flag up overflow and don't print
d_worrall 12:2235c72db927 78 _cursor++;
d_worrall 12:2235c72db927 79 return -1;
d_worrall 12:2235c72db927 80 }
d_worrall 12:2235c72db927 81 element[_cursor]=0x0000; //reset an element before pasting a char other than '.'
d_worrall 12:2235c72db927 82 }
d_worrall 12:2235c72db927 83
d_worrall 12:2235c72db927 84 if(_cursor & 1){ //look up char >> element conversion and paste to display
d_worrall 12:2235c72db927 85 element[_cursor] |= dispR[toupper(c)];
d_worrall 0:843654413849 86 } else {
d_worrall 12:2235c72db927 87 element[_cursor] |= dispL[toupper(c)];
d_worrall 0:843654413849 88 }
d_worrall 0:843654413849 89
d_worrall 0:843654413849 90 int i = _cursor/2;
d_worrall 12:2235c72db927 91 ppair[i] = element[(2*i)] + element[(2*i)+1]; //combine left and right elements into a form readable by the PCA9635
d_worrall 12:2235c72db927 92
d_worrall 12:2235c72db927 93 _pca.setAddress(loc[_cursor]); //set location to paste to
d_worrall 12:2235c72db927 94 bus(ppair[i]); //sned data tp PCA9635
d_worrall 9:44de04d410df 95
d_worrall 12:2235c72db927 96 locate(_cursor+1); //allow cursor to be in element[6], decision of what to do
d_worrall 12:2235c72db927 97 //is made at the start of the putc() algo. depending on char.
d_worrall 9:44de04d410df 98 return c;
d_worrall 0:843654413849 99 }
d_worrall 0:843654413849 100
d_worrall 9:44de04d410df 101 int dispBoB::_getc(){
d_worrall 9:44de04d410df 102 return -1;
d_worrall 0:843654413849 103 }
d_worrall 0:843654413849 104
d_worrall 0:843654413849 105 void dispBoB::scroll(string str, float speed){
d_worrall 12:2235c72db927 106 char buffer[12]; //instantiate a 12 element buffer array
d_worrall 12:2235c72db927 107 str.insert(0, nullArray); //paste 6 zeros at either end of string
d_worrall 0:843654413849 108 str.insert(str.length(), nullArray);
d_worrall 0:843654413849 109
d_worrall 12:2235c72db927 110 for(int k = 0; k < str.length(); k++){
d_worrall 12:2235c72db927 111 str.copy(buffer, 12, k); //mask a 12 element buffer starting at k
d_worrall 12:2235c72db927 112 locate(0); //locate to position 0
d_worrall 12:2235c72db927 113 printf("%s", buffer); //print buffer to display
d_worrall 12:2235c72db927 114 if((element[0]&0x1000)==0x1000){ //if position zero has two characters i.e. char + '.'
d_worrall 12:2235c72db927 115 if(element[0]!=0x1000)k++; //then treat as one
d_worrall 12:2235c72db927 116 else;
d_worrall 12:2235c72db927 117 }
d_worrall 12:2235c72db927 118 wait(speed); //hold frame for duration of speed x milliseconds
d_worrall 0:843654413849 119 }
d_worrall 0:843654413849 120 }
d_worrall 0:843654413849 121
d_worrall 9:44de04d410df 122 //supply PCA9635 functionality to dispBoB class
d_worrall 8:a875c4229c57 123 void dispBoB::bus(short leds){
d_worrall 8:a875c4229c57 124 _pca.bus(leds);
d_worrall 8:a875c4229c57 125 }
d_worrall 0:843654413849 126
d_worrall 13:38780808dc2f 127 void dispBoB::brightness(char value){
d_worrall 13:38780808dc2f 128 for(int i = 0; i < 3; i++){
d_worrall 13:38780808dc2f 129 _pca.setAddress(loc[i]);
d_worrall 13:38780808dc2f 130 _pca.brightness(0xFF, value);
d_worrall 13:38780808dc2f 131 }
d_worrall 13:38780808dc2f 132 }
d_worrall 0:843654413849 133
d_worrall 0:843654413849 134
d_worrall 0:843654413849 135
d_worrall 0:843654413849 136
d_worrall 0:843654413849 137
d_worrall 0:843654413849 138
d_worrall 0:843654413849 139
d_worrall 13:38780808dc2f 140