Luka Elmir

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DotMatrix.h Source File

DotMatrix.h

00001 #include "mbed.h"
00002 #include "MatrixSPI.h"
00003 #include <string>
00004 
00005 #ifndef DOTMATRIX_H
00006 #define DOTMATRIX_H
00007 
00008 enum ModPrikazivanja{scroll, char_by_char, live_input};
00009 const char znakovi[66][6] =
00010 {
00011     {0, 0x7e, 0x11, 0x11, 0x7e, 0},//A
00012     {0, 0x7e, 0x49, 0x49, 0x36, 0}, //B
00013     {0, 0x3e, 0x41, 0x41, 0x22, 0}, //C
00014     {0, 0x7f, 0x41, 0x41, 0x3e, 0}, //D
00015     {0, 0x7f, 0x49, 0x49, 0x41, 0}, //E
00016     {0, 0x7f, 0x09, 0x09, 0x01, 0}, //F
00017     {0x3e, 0x41, 0x51, 0x32, 0x70, 0}, //G
00018     {0, 0x7f, 0x08, 0x08, 0x7f, 0}, //H
00019     {0, 0x41, 0x7f, 0x41, 0, 0}, //I
00020     {0, 0x21, 0x41, 0x41, 0x3f, 0}, //J
00021     {0, 0x7f, 0x08, 0x14, 0x63, 0}, //K
00022     {0, 0x7f, 0x40, 0x40, 0x40, 0}, //L
00023     {0x7f, 0x02, 0x0c, 0x02, 0x7f, 0}, //M
00024     {0x7f, 0x04, 0x08, 0x10, 0x7f, 0}, //N
00025     {0, 0x3e, 0x41, 0x41, 0x3e, 0}, //O
00026     {0, 0x7f, 0x09, 0x09, 0x06, 0}, //P
00027     {0x3e, 0x41, 0x51, 0x3e, 0x40, 0}, //Q
00028     {0, 0x7f, 0x09, 0x09, 0x76, 0}, //R
00029     {0, 0x26, 0x89, 0x89, 0x32, 0}, //S
00030     {0x03, 0x01, 0x7f, 0x01, 0x03, 0},  //T
00031     {0x1f, 0x20, 0x40, 0x40, 0x20, 0x1f},  //U
00032     {0x1f, 0x20, 0x40, 0x20, 0x1f, 0}, //V
00033     {0x3f, 0x40, 0x38, 0x40, 0x3f, 0}, //W
00034     {0x63, 0x14, 0x08, 0x14, 0x63, 0}, //X
00035     {0x07, 0x08, 0x70, 0x08, 0x07, 0}, //Y
00036     {0x61, 0x51, 0x49, 0x45, 0x43, 0}, //Z
00037     {0, 0x20, 0x54, 0x54, 0x78, 0}, //a
00038     {0, 0x7f, 0x44, 0x44, 0x38, 0}, //b
00039     {0, 0x38, 0x44, 0x44, 0x28, 0}, //c
00040     {0, 0x38, 0x44, 0x44, 0x7f, 0}, //d
00041     {0, 0x38, 0x54, 0x54, 0x18, 0}, //e
00042     {0, 0xf8, 0x24, 0x24, 0x08, 0}, //f
00043     {0, 0x98, 0xa4, 0xa4, 0x78, 0}, //g
00044     {0, 0x7f, 0x04, 0x04, 0x78, 0}, //h
00045     {0, 0x44, 0x7d, 0x40, 0, 0}, //i
00046     {0, 0x40, 0x80, 0x88, 0x7a, 0}, //j
00047     {0, 0x7f, 0x10, 0x28, 0x44, 0}, //k
00048     {0, 0x41, 0x7f, 0x40, 0, 0}, //l
00049     {0x7c, 0x04, 0x7c, 0x04, 0x78, 0}, //m
00050     {0, 0x7c, 0x04, 0x04, 0x78, 0}, //n
00051     {0, 0x38, 0x44, 0x44, 0x38, 0}, //o
00052     {0, 0xfc, 0x24, 0x24, 0x18, 0}, //p
00053     {0, 0x18, 0x24, 0x24, 0xfc, 0}, //q
00054     {0, 0x7c, 0x08, 0x04, 0x04, 0}, //r
00055     {0, 0x48, 0x54, 0x54, 0x24, 0}, //s
00056     {0, 0x04, 0x3f, 0x44, 0, 0}, //t
00057     {0, 0x3c, 0x40, 0x40, 0x7c, 0}, //u
00058     {0x1c, 0x20, 0x40, 0x20, 0x1c, 0}, //v
00059     {0x1c, 0x20, 0x10, 0x20, 0x1c, 0}, //w
00060     {0x44, 0x28, 0x10, 0x28, 0x44, 0}, //x
00061     {0, 0x9c, 0xa0, 0xa0, 0x7c, 0}, //y
00062     {0x44, 0x64, 0x54, 0x4c, 0x44, 0}, //z
00063     {0x3e, 0x51, 0x49, 0x45, 0x3e, 0}, //0
00064     {0, 0x42, 0x7f, 0x40, 0, 0}, //1
00065     {0x42, 0x61, 0x51, 0x49, 0x46, 0}, //2
00066     {0, 0x22, 0x41, 0x49, 0x36, 0}, //3
00067     {0x08, 0x0c, 0x0a, 0x7c, 0x08, 0}, //4
00068     {0, 0x27, 0x45, 0x45, 0x39, 0}, //5
00069     {0, 0x3e, 0x49, 0x49, 0x32, 0}, //6
00070     {0x63, 0x11, 0x09, 0x05, 0x03, 0}, //7
00071     {0, 0x36, 0x49, 0x49, 0x36, 0}, //8
00072     {0, 0x26, 0x49, 0x49, 0x3e, 0}, //9
00073     {0, 0xb0, 0x70, 0, 0, 0}, //,
00074     {0, 0x60, 0x60, 0, 0, 0}, //.
00075     {0x10,0x27,0x40,0x40,0x27,0x10}, // :-)
00076     {0,0,0,0,0,0} // prazno
00077 };
00078 
00079 class DotMatrix{
00080     
00081 public:
00082     DotMatrix(PinName data_input = dp2, PinName clock = dp6, PinName loadcs = dp24)
00083     :spi(data_input, clock, loadcs), turnedON(false), mod(scroll), char_trenutni(0),
00084     char_offset(0), repeat(true), brzina_ms(1000), isAttached(false)
00085     {    
00086         recenica = "Kokolo moje jedino.";  
00087         for(int i = 0; i < 8; i++) buffer[i] = 0;
00088     }
00089     
00090     void refresh();
00091     void setIntensity(float jacina);
00092     void setSpeed_ms(int ms);
00093     void turnON();
00094     void turnOFF();
00095     void PromijeniRecenicu(string recenica){
00096         this->recenica =  " " + recenica;
00097         char_trenutni = 0;
00098         char_offset = 0;
00099     }
00100     void NadodajNaRecenicu(char c){
00101         recenica += c;   
00102     }
00103     void NadodajNaRecenicu(string str){
00104         recenica += str;   
00105     }
00106     void PromijeniMod(ModPrikazivanja mod){
00107         if(this->mod != mod){
00108             this->mod = mod;
00109             attach();   
00110         }   
00111     }
00112     ModPrikazivanja dajMod() const{
00113         return mod;   
00114     }
00115     void SetRepeat(bool r){
00116      repeat = r;   
00117     }
00118     
00119     bool GetRepeat() const{
00120         return repeat;   
00121     }
00122     
00123     void setChar(char c, int offset)
00124     {
00125         int index;
00126         if(c >= 'A' && c <= 'Z')
00127             index = c - 'A';
00128         else if(c>= 'a' && c <='z')
00129             index = c - 'a' + 26;
00130         else if( c>= '0' && c <= '9')
00131             index = c - '0' + 52;
00132         else if (c== ',')
00133             index = 62;
00134         else if(c=='.')
00135             index = 63;
00136         else if(c==' ')
00137             index = 65;
00138         else
00139             index = 64;
00140         
00141         if(offset < 0)
00142         {
00143             for(int i = 0; i < 6 + offset; i++)
00144             {
00145                 buffer[i] = znakovi[index][i - offset];   
00146             }  
00147         }   
00148         else 
00149         {
00150             for(int i = offset; i < 8 && i < 6 + offset; i++)
00151             {
00152                 buffer[i] = znakovi[index][i - offset];   
00153             }
00154         }
00155     }
00156     
00157     void displaySingleChar(char c){
00158         setChar(c, 1);   
00159         refresh();
00160     }
00161     
00162       
00163     void prikaziScroll(){
00164         if(char_trenutni == recenica.size()) refresh();
00165         else {
00166             setChar(recenica[char_trenutni], char_offset);
00167             if(char_trenutni < recenica.size() - 1)
00168                 setChar(recenica[char_trenutni + 1], char_offset + 7);
00169             
00170             if(char_offset < -6){
00171                 char_offset = 1; // plus 7
00172                 char_trenutni++;
00173             } 
00174         }
00175         
00176         if(char_trenutni >= recenica.size() && repeat)
00177             char_trenutni = 0;
00178         
00179     }
00180     void prikaziCBC(){
00181         if(char_trenutni == recenica.size()) refresh();
00182         else {//ako ne bude valjao refresh ne bude prazno prikazivali... skloniti else i dole >= u > promijeniti
00183             displaySingleChar(recenica[char_trenutni]);
00184             char_trenutni++;       
00185         }
00186         
00187          if(char_trenutni >= recenica.size() && repeat)
00188             char_trenutni = 0;  
00189     }
00190         
00191     int brzina_ms;    
00192     string recenica;
00193     int buffer[8];
00194     
00195 private:
00196     void attach();
00197     void detach(){
00198         if(isAttached) ticker.detach();
00199         isAttached = false;   
00200     }
00201     
00202     
00203     MatrixSPI spi;
00204     bool turnedON;
00205     ModPrikazivanja mod;
00206     int char_trenutni, char_offset;
00207     Ticker ticker;
00208     bool isAttached; 
00209     bool repeat;
00210 };
00211 
00212 
00213 
00214 void DotMatrix::refresh(){
00215     for(int i = 0; i < 8; i++)
00216     {
00217         spi.sendCol(i, buffer[i]); 
00218         buffer[i] = 0;  
00219     }
00220 }
00221 
00222 void DotMatrix::setIntensity(float jacina){
00223     if(jacina <= 0){
00224         spi.turnOFF();
00225         turnedON = false;   
00226         
00227         
00228     } else if(jacina >= 0.98){
00229         if(!turnedON){ 
00230             spi.turnON();
00231             turnedON = true;
00232         }
00233         
00234         spi.sendSingle(MatrixSPI::intensity, 0x0f);
00235     } else {
00236         if(!turnedON){ 
00237             spi.turnON();
00238             turnedON = true;
00239         }
00240         
00241         int temp =  jacina*0x0f;
00242         spi.sendSingle(MatrixSPI::intensity, temp);
00243     } 
00244 }
00245 
00246 void DotMatrix::attach()
00247 {
00248     detach();
00249     if(mod == scroll)
00250     {
00251         isAttached = true;
00252         ticker.attach(this, &DotMatrix::prikaziScroll, brzina_ms/8);
00253     } else if(mod == char_by_char){
00254         isAttached = true;
00255         ticker.attach(this, &DotMatrix::prikaziCBC, brzina_ms);
00256     }
00257 }
00258 
00259 void DotMatrix::setSpeed_ms(int ms)
00260 {
00261     brzina_ms = ms;
00262     ticker.detach();
00263     attach();   
00264 }
00265 
00266 void DotMatrix::turnON(){
00267     if(!turnedON){
00268         spi.turnON();
00269         turnedON = true;
00270         
00271         attach();   
00272     }
00273 }
00274 
00275 void DotMatrix::turnOFF(){
00276     if(turnedON){
00277         spi.turnOFF();
00278         turnedON = false;
00279         
00280         ticker.detach();   
00281     }   
00282 }
00283 
00284 #endif //DotMatrix_h