Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-------------------------------------------------------------------------------------------//
- // ledkey.h v0.0.6 beta by Tomasz Majcher //
- // //
- // TRANSLINE LUBIN //
- // transline.majcher@gmail.com //
- // licencja: FREE //
- // //
- // Ciężki Transport Drogowy ( silosy / wywrotki / gabaryty ) //
- // Automatyka Przemysłowa ( STM32 / ESP32 / FPGA ) //
- // Kryszywa Naturalne ( piasek / żwiry / Bazalt / Granit ) //
- // //
- // POZOSTAŁE USŁUGI TRANSLINE //
- // //
- // Logistyka Transportowa Wiktoria.Majcher@gmail.com //
- // Spedycja Krajowa Wiktoria.Majcher@gmail.com //
- // Windykacja Należności Sylwia.Majcher@gmail.com //
- // Usługi Serwisowe Marcin.Majcher@gmail.com //
- // //
- // data utworzenia : 01.12.2019 //
- // library : ledkey 8x pushbutto, 8x led, 2x lcd 4 digit //
- // //
- // //
- // v0.0.7 beta (26.12.2019) : optymalizacja klasy //
- // v0.0.6 beta (15.12.2019) : led_step_0_7 - dioda krocząca (wszystkie moduły) //
- // v0.0.5 beta (13.12.2019) : dodanie znaku stopnia (10) i literca "C" (11) //
- // v0.0.4 beta (03.12.2019) : optymalizacja LCD //
- // v0.0.3 beta (02.12.2019) : dodano obsługa LCD (2x) //
- // v0.0.2 beta (01.12.2019) : dodana obsługa przyciskow (8x) //
- // v0.0.1 beta (01.12.2019) : dodana obsługa ledów (8x) //
- // //
- ///////////////////////////////////////////////////////////////////////////////////////////////
- #ifndef wiring_shift_mod_h
- #define wiring_shift_mod_h
- #endif
- #define CLOCK_NORMAL 0
- #define CLOCK_INVERT 1
- #define CLOCK_TYPE CLOCK_INVERT
- #define CLOCK_DELAY_US 1
- //--- klasa LEDKEY ------------------------------------------
- //
- // konstruktory:
- // LEDKEY(); strobe = 2, clock = 1, data = 0
- // LEDKEY(strobe, clock, data)
- //
- //
- // metody LED:
- // LedTurnON1(index)
- // LedTurnON2(index)
- // LedTurnOFF1(index)
- // LedTurnOFF2(index)
- // LedChange1(index)
- // LedChange2(index)
- // LedStep0()
- // LedStep1()
- // LedStep2()
- //
- // metody BUTTON:
- // Button() --> zwraca (uint8_t) nr. wcisnietego przycisku 1,2,3,4,5,6,7,8 (dla kodów 1,2,4,8,16,32,64,128)
- //
- // metody LCD:
- // ShowLCD1(uint8_t tablica[4], bool tryb); --> na wejsciu tablica 4 liczb do wyswietlenia, tryb 0,1 (kropka miedzy 2 a 3 segmentem)
- // ShowLCD2(uint8_t tablica[4], bool tryb); --> na wejsciu tablica 4 liczb do wyswietlenia, tryb 0,1 (kropka miedzy 2 a 3 segmentem)
- //
- class LEDKEY
- {
- private:
- int stb_pin; // strobe pin
- int clk_pin; // clock pin
- int din_pin; // digital pin
- const uint8_t LedAdres1[4] = { 0xC1, 0xC3, 0xC5, 0xC7 }; // adresy LED moduł: LEDKEY1
- const uint8_t LedAdres2[4] = { 0xC9, 0xCB, 0xCD, 0xCF }; // adresy LED moduł: LEDKEY2
- const uint8_t LCDAdres1[4] = { 0xC0, 0xC2, 0xC4, 0xC6 }; // adresy LCD moduł: LEDKEY1
- const uint8_t LCDAdres2[4] = { 0xC8, 0xCA, 0xCC, 0xCE }; // adresy LCD moduł: LEDKEY2
- // --- struktura LED (2 obiekty, 2x 4 diody)
- struct LED
- {
- uint8_t LedAdres[4]; // adresy LED
- uint8_t LedStatus[4]; // stany LED (0 - OFF, 1 - ON);
- inline void setAdres(const uint8_t temp[]) { for(uint8_t index = 0; index < 4; index++) LedAdres[index] = temp[index]; }
- inline void setStatus(uint8_t index, uint8_t stan) { LedStatus[index] = stan; }
- inline bool getStatus(uint8_t index) { return LedStatus[index]; }
- }
- led1, led2;
- // --- struktura LCD (2 obiekty, 2x 4 digit)
- struct LCD
- {
- /*0*/ /*1*/ /*2*/ /*3*/ /*4*/ /*5*/ /*6*/ /*7*/ /*8*/ /*9*/ /*^*/ /*C*/
- uint8_t nCyfra[12] = { 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x63, 0x39 }; // cyfry bez kropki
- uint8_t dCyfra[12] = { 0xBF, 0x86, 0xdb, 0xcf, 0xe6, 0xed, 0xfd, 0x87, 0xff, 0xef, 0x63, 0x39 }; // cyfry z kropką
- uint8_t LCDAdres[4]; // adresy wyswietlacza
- inline void setAdres(const uint8_t temp[]) { for(uint8_t index = 0; index <4; index++) LCDAdres[index] = temp[index]; }
- inline uint8_t getAdres(uint8_t index) { return LCDAdres[index]; }
- uint8_t *makeLCDset(uint8_t temp[],bool rodzaj); // zwroc tablicę złożoną z licb dla LCD (1,2,3,4)
- uint8_t *makeLCDadr(); // zwroc tablicę złożoną z adresów dla LCD
- } *lcd1, *lcd2;
- public:
- LEDKEY(); // konstruktor domyślny
- LEDKEY(int , int , int );
- ~LEDKEY()
- {
- delete lcd1,lcd2;
- };
- void setPIN(); // utwórz piny
- uint8_t readButtons(void); // obsługa : przyciski
- // --- deklaracje dla LED -------------------------------------------------------------------------------------------------------------
- // ------------------------------------------------------------------------------------------------------------------------------------
- void LedTurnON(uint8_t index, LED *wsk); // wlacz diode (wskaznik na strukture led1 lub led2)
- inline void LedTurnON1(uint8_t index) { LedTurnON(index, &led1); }
- inline void LedTurnON2(uint8_t index) { LedTurnON(index, &led2); }
- void LedTurnOFF(uint8_t index, LED *wsk); // wylacz diode (wskaznik na strukture led1 lub led2)
- inline void LedTurnOFF1(uint8_t index) { LedTurnOFF(index, &led1); }
- inline void LedTurnOFF2(uint8_t index) { LedTurnOFF(index, &led2); }
- void LedStep(LED *wsk); // dioda kroczaca
- inline void LedStep1() { LedStep(&led1); }
- inline void LedStep2() { LedStep(&led2); }
- void LedStep0(); // dioda ktocząca (0,1,2,3,4,5,6,7) ze statyczną zmienną aktualnej
- void LedChange(uint8_t index, LED *wsk); // zmien status diody
- inline void LedChange1(uint8_t index) { LedChange(index,&led1); }
- inline void LedChange2(uint8_t index) { LedChange(index,&led2); }
- // ------------------------------------------------------------------------------------------------------------------------------------
- // --- deklaracje do obsługi LCD ------------------------------------------------------------------------------------------------------
- void sendCommand(uint8_t wartosc); // obsluga : send command
- void reset(); // obsługa : reset
- uint8_t shiftInMod(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t clock_type, uint16_t clock_delay_us);
- void shiftOutMod(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t clock_type, uint16_t clock_delay_us, uint8_t val);
- // ------------------------------------------------------------------------------------------------------------------------------------
- // --- deklaracje dla LCD -------------------------------------------------------------------------------------------------------------
- void ShowLCDset(uint8_t temp[], LCD *wsk, bool mode); // wyswietl liczby na LCD
- inline void ShowLCDset_wdot(uint8_t tab[], LCD *wsk) { ShowLCDset(tab,wsk,1); } // pokaz liczby z kropka
- inline void ShowLCDset_ndot(uint8_t tab[], LCD *wsk) { ShowLCDset(tab,wsk,0); } // pokaz liczby bez kropki
- void ShowLCDdig(uint8_t adres, uint8_t liczba); // wyswietl liczbe na LCD
- void ShowLCD1(uint8_t temp[], bool mode);
- void ShowLCD2(uint8_t temp[], bool mode);
- void ShowTEMP(uint8_t temp, uint8_t wyswietlacz); // pokaz temperature na LCD1
- // -----------------------------------------------------------------------------------------------------------------------------------
- // --- deklaracje dla PRZYCISK -------------------------------------------------------------------------------------------------------
- word Button(); // zwraca numer przycisku
- }; // koniec klasy LEDKEY
- // -----------------------------------------------------------------------------------------------------------------------------------------
- // --- definicje klasy LEDKEY --------------------------------------------------------------------------------------------------------------
- // -----------------------------------------------------------------------------------------------------------------------------------------
- // --- definicje obsługi LCD ---------------------------------------------------------------------------------------------------------------
- // -----------------------------------------------------------------------------------------------------------------------------------------
- uint8_t *LEDKEY::LCD::makeLCDset(uint8_t temp[],bool rodzaj)
- {
- static uint8_t tab[4]; // tablica statyczna na rejestr procesora
- for(uint8_t index = 0; index < 4; index++) tab[index] = nCyfra[temp[index]]; // zapisz to zwracanej tablicy kody liczb
- if(rodzaj) tab[1] = dCyfra[temp[1]]; // jesli 0 - liczba bez kropki, 1 - liczba z kropka
- return( tab ); // zwroc tablice z funkcji
- }
- uint8_t *LEDKEY::LCD::makeLCDadr()
- {
- static uint8_t tab[4];
- for(uint8_t index = 0; index <4; index++) tab[index] = LCDAdres[index];
- return( tab );
- }
- void LEDKEY::ShowLCDset(uint8_t temp[], LCD *wsk, bool mode)
- {
- uint8_t *T_adres; // wskaznik (stworz) do adresow
- uint8_t *T_digit; // wskaznik (stworz) do liczb
- T_adres = wsk -> makeLCDadr();
- if(!mode ) T_digit = wsk -> makeLCDset(temp,0); // zapisz pod wskaznik liczby bez kropki
- else
- if( mode ) T_digit = wsk -> makeLCDset(temp,1); // zapisz pod wskaznik liczby z kropką
- for(byte index = 0; index < 4; index++) ShowLCDdig(T_adres[index], T_digit[index]); // pokaz liczby na LCD
- //delete T_adres;
- //delete T_digit;
- }
- void LEDKEY::ShowLCD1(uint8_t temp[], bool mode)
- {
- static bool check_mode = 0;
- if(!mode) ShowLCDset_ndot(temp, lcd1); // pokaz na wyswietlaczu nr.1 tablice[] liczb bez kropki
- else
- if (mode) // jesli mrugajaca kropka wykonaj ponizsze warunki
- {
- if( !check_mode ) // kropka nie aktywna (wlacz)
- {
- ShowLCDset_ndot(temp, lcd1);
- check_mode = 1;
- }
- else // kropka aktywna (wylacz)
- {
- ShowLCDset_wdot(temp, lcd1);
- check_mode = 0;
- }
- }
- }
- void LEDKEY::ShowLCD2(uint8_t temp[], bool mode)
- {
- static bool check_mode = 0;
- if(!mode) ShowLCDset_ndot(temp, lcd2); // pokaz na wyswietlaczu nr.1 tablice[] liczb bez kropki
- else
- if( mode)
- {
- if( !check_mode )
- {
- ShowLCDset_ndot(temp, lcd2);
- check_mode = 1;
- }
- else
- {
- ShowLCDset_wdot(temp, lcd2);
- check_mode = 0;
- }
- }
- }
- // --- LEDKEY :: ShowTEMP --- na wejściu temperatura, i nr. wyswietlacza (1,2) ------------------------------------- //
- void LEDKEY::ShowTEMP(uint8_t temperatura, uint8_t wyswietlacz) // ShowTEMP ----------------------- //
- { // ................................ //
- uint8_t tab[4]; // tablica do zmontowania wyjscie . //
- tab[0] = temperatura / 10; // tab[0] - dziesiatki temperatury //
- tab[1] = temperatura % 10; // tab[1] - jednosci temperatury //
- tab[2] = 10; // tab[2] = 10 znak stopnia //
- tab[3] = 11; // tab[3] = 11 znak C //
- if(wyswietlacz == 1) ShowLCD1(tab,0); // jesli w = 1 to LCD 1 //
- else // w innym przypadku //
- if(wyswietlacz == 2) ShowLCD2(tab,0); // jesli w = 2 to LCD 2 //
- } // ................................ //
- // ----------------------------------------------------------------------------------------------------------------- //
- void LEDKEY::ShowLCDdig(uint8_t adres, uint8_t liczba)
- {
- sendCommand(0x40);
- digitalWrite(stb_pin, LOW);
- shiftOutMod(din_pin, clk_pin, LSBFIRST, CLOCK_TYPE, CLOCK_DELAY_US, adres); // adres segmentu
- shiftOutMod(din_pin, clk_pin, LSBFIRST, CLOCK_TYPE, CLOCK_DELAY_US, liczba); // liczba HEX
- digitalWrite(stb_pin, HIGH);
- }
- // -----------------------------------------------------------------------------------------------------------------------------------------
- // --- definicje klasy LEDKEY --------------------------------------------------------------------------------------------------------------
- // -----------------------------------------------------------------------------------------------------------------------------------------
- // --- definicje obsługi LED ---------------------------------------------------------------------------------------------------------------
- // -----------------------------------------------------------------------------------------------------------------------------------------
- void LEDKEY::LedTurnON(uint8_t index, LED *wsk)
- {
- sendCommand(0x44);
- digitalWrite(stb_pin, LOW);
- shiftOutMod(din_pin, clk_pin, LSBFIRST, CLOCK_TYPE, CLOCK_DELAY_US, wsk->LedAdres[index]);
- shiftOutMod(din_pin, clk_pin, LSBFIRST, CLOCK_TYPE, CLOCK_DELAY_US, 1);
- digitalWrite(stb_pin, HIGH);
- wsk->setStatus(index,1);
- }
- void LEDKEY::LedTurnOFF(uint8_t index, LED *wsk)
- {
- sendCommand(0x44);
- digitalWrite(stb_pin, LOW);
- shiftOutMod(din_pin, clk_pin, LSBFIRST, CLOCK_TYPE, CLOCK_DELAY_US, wsk->LedAdres[index]);
- shiftOutMod(din_pin, clk_pin, LSBFIRST, CLOCK_TYPE, CLOCK_DELAY_US, 0);
- digitalWrite(stb_pin, HIGH);
- wsk->setStatus(index,0);
- }
- void LEDKEY::LedChange(uint8_t index, LED *wsk)
- {
- if( wsk->getStatus(index) ) LedTurnOFF(index,wsk);
- else LedTurnON(index,wsk);
- }
- // ---------------------------------------------------------------------------------------------- //
- // --- LedKey0 --- dioda kroczącza : 0,1,2,3,4,5,6,7 -------------------------------------------- //
- // ---------------------------------------------------------------------------------------------- //
- void LEDKEY::LedStep0() //
- { // start ---------------------------------------- //
- static byte index = 0; // zmienna statyczna index (pozycja diody) ------ //
- if(index == 0) // jeśli index = 0, zgaś led 7, włącz led 0 ----- //
- { // ---------------------------------------------- //
- LedTurnON(index,&led1); // włącz diodę: 0 na module &led1 --------------- //
- LedTurnOFF(3,&led2); // wyłącz diodę 3 (7) na module &led2 ----------- //
- index ++; // zwiększ o jeden index ------------------------ //
- } // koniec --------------------------------------- //
- else if(index > 0 && index < 4) // jeśli index większy od 0 a mniejszy od 4 ----- //
- { // start ---------------------------------------- //
- LedTurnOFF(index - 1, &led1); // wyłącz diodę poprzedającą na module &led1 ---- //
- LedTurnON(index, &led1); // włącz diodę wskazywaną przez index na &led1 -- //
- index++; // zwiększ o jeden index ------------------------ //
- } // koniec --------------------------------------- //
- else if(index == 4) // jeśli index ma wartość równą 4 --------------- //
- { // start ---------------------------------------- //
- LedTurnON(index - 4, &led2); // włącz diodę 0 (5) na module &led2 ------------ //
- LedTurnOFF(3,&led1); // wyłącz diodę 3 (4) na module &led1 ----------- //
- index ++; // zwiększ o jeden wartość zmiennej index ------- //
- } // koniec --------------------------------------- //
- else if(index > 4 && index < 8) // jeśli index większy od 4 a mniejszy od 8 ----- //
- { // start ---------------------------------------- //
- LedTurnOFF(index - 5, &led2); // wyłącz diodę poprzedającą w module &led2 ----- //
- LedTurnON(index - 4, &led2); // włącz diodę wskazywaną przez index na &led2 -- //
- index ++; // zwiększ o jeden wartość zmiennej index ------- //
- } // koniec --------------------------------------- //
- else if (index == 8) // jeśli wartość index równa jest 8 ------------- //
- { // start ---------------------------------------- //
- LedTurnOFF(3, &led2); // wylacz diode 3 (7) na module &led2 ----------- //
- index = 0; // wyzerun zmienną index, ustaw 0 --------------- //
- LedTurnON(index, &led1); // włącz diodę 0 (0) na module &led1 ------------ //
- index++; // zwiększ wartość zmiennej index o jeden ------- //
- } // koniec --------------------------------------- //
- } // KONIEC -> void LEDKEY::LedStep0()------------- //
- // ---------------------------------------------------------------------------------------------- //
- // --- LedStep(Led który_moduł) ----------------------------------------------------------------- //
- // ---------------------------------------------------------------------------------------------- //
- void LEDKEY::LedStep(LED *wsk) //
- { //
- static byte index = 0; // zmienna statyczna -> pozycja diody ----------- //
- if (index == 0) // jesli wartość index = 0 ---------------------- //
- { // start ---------------------------------------- //
- LedTurnON(index,wsk); // zapal diode 0 (nr. 1) na module & ------------ //
- LedTurnOFF(3,wsk); // zgaś diodę 3 (nr. 4) na module & ------------ //
- index++; // zwiększ wartość zmiennej index o 1 ----------- //
- } // koniec --------------------------------------- //
- else if (index > 0 && index < 4) // jeśli zmienna index wieksza od 0 mniejsza od 4 //
- { // start ---------------------------------------- //
- LedTurnOFF(index - 1, wsk); // wyłącz diodę poprzedająca na module & -------- //
- LedTurnON (index, wsk); // włącz diodę wskazywaną przez wartość index & - //
- index++; // zwiększ wartość zmiennej index o 1 ----------- //
- } // koniec --------------------------------------- //
- else if (index == 4 ) // jesli wartość zmiennej index to 4 ------------ //
- { // start ---------------------------------------- //
- LedTurnOFF(3, wsk); // wylacz diodę 3 (nr.4 - ostatnią) na module & - //
- index = 0; // ustaw nową wartość zmiennej index, 0 --------- //
- LedTurnON(index, wsk); // zapal diode (pierwszą) na module & ----------- //
- index++; // zwiększ wartość zmiennej index o 1 ----------- //
- } // koniec --------------------------------------- //
- } // KONIEC - void LEDKEY::LedStep(LED *wsk) ------ //
- // ---------------------------------------------------------------------------------------------- //
- // ---------------------------------------------------------------------------------------------- //
- // --- definicje klasy LEDKEY ------------------------------------------------------------------- //
- // ---------------------------------------------------------------------------------------------- //
- // --- konstruktor domyślny --------------------------------------------------------------------- //
- // ---------------------------------------------------------------------------------------------- //
- LEDKEY::LEDKEY() // to jest konstruktor domyślny klasy LEDKEY ---- //
- { // start ---------------------------------------- //
- stb_pin = 2; // ustaw strobe_pin (PIN DIGITAL 2) ------------- //
- clk_pin = 1; // ustaw clk_pin (PIN DIGITAL 1) ------------- //
- din_pin = 0; // ustaw din_pin (PIN DIGITAL 0) ------------- //
- led1.setAdres(LedAdres1); // ustaw adresy fizyczne dla modułu led1 -------- //
- led2.setAdres(LedAdres2); // ustaw adresy fizyczne dla modułu led2 -------- //
- lcd1 = new LCD; // stwórz nowy obiekt lcd1 (wyświetlacz LCD 1) -- //
- lcd2 = new LCD; // stwórz nowy obiekt lcd2 (wyświetlacz LCD 2) -- //
- lcd1 -> setAdres(LCDAdres1); // ustaw adresy fizyczne dla modułu lcd1 -------- //
- lcd2 -> setAdres(LCDAdres2); // ustaw adresy fizyczne dla modułu lcd2 -------- //
- setPIN(); // wykonaj pinMode dla stb_pin, clk_pin, din_pin //
- } // koniec --------------------------------------- //
- // ---------------------------------------------------------------------------------------------- //
- // --- konstruktor z parametrem------------------------------------------------------------------ //
- // ---------------------------------------------------------------------------------------------- //
- LEDKEY::LEDKEY(int stbp, int clkp, int dinp) //
- { // to jest konstuktor z parametrami wejścia ----- //
- stb_pin = stbp; // ustaw stb_pin (PIN DIGITAL) = stbp ----------- //
- clk_pin = clkp; // ustaw clk_pin (PIN DIGITAL) = clkp ----------- //
- din_pin = dinp; // ustaw din_pin (PIN DIGITAL) = dinp ----------- //
- led1.setAdres(LedAdres1); // ustaw adresy fuzyczne dla modułu led1 -------- //
- led2.setAdres(LedAdres2); // ustaw adresy fizyczne dla modułu led2 -------- //
- lcd1 = new LCD; // stwórz nowy obiekt lcd1 (wyświetlacz LCD 1) -- //
- lcd2 = new LCD; // stwórz nowy obiekt lcd2 (wyświetlacz LCD 2) -- //
- lcd1 -> setAdres(LCDAdres1); // ustaw adresy fizyczne dla modułu lcd1 -------- //
- lcd2 -> setAdres(LCDAdres2); // ustaw adresy fizyczne dla modułu lcd2 -------- //
- setPIN(); // wykonaj pinMode dla stb_pin, clk_pon, din_pin //
- } // koniec --------------------------------------- //
- // ---------------------------------------------------------------------------------------------- //
- // --- definicje klasy LEDKEY ------------------------------------------------------------------- //
- // ---------------------------------------------------------------------------------------------- //
- // --- setup klasy LEDKEY ----------------------------------------------------------------------- //
- // ---------------------------------------------------------------------------------------------- //
- void LEDKEY::setPIN() //
- { // ustawianie pinów ----------------------------- //
- pinMode(stb_pin, OUTPUT); // stb_pin DIGITAL OUT -------------------------- //
- pinMode(clk_pin, OUTPUT); // clk_pin DIGITAL OUT -------------------------- //
- pinMode(din_pin, OUTPUT); // din_pin DIGITAL OUT -------------------------- //
- sendCommand(0x8f); // aktywuj LED & KEY ---------------------------- //
- reset(); // resetuj LED & KEY ---------------------------- //
- } // KONIEC LEDKEY::setPIN ------------------------ //
- // ---------------------------------------------------------------------------------------------- //
- // --- definicje klasy LEDKEY ------------------------------------------------------------------- //
- // ---------------------------------------------------------------------------------------------- //
- // --- obsługa diód i wyświetlacza fizyczna------------------------------------------------------ //
- // ---------------------------------------------------------------------------------------------- //
- void LEDKEY::sendCommand(uint8_t wartosc)
- {
- digitalWrite(stb_pin, LOW);
- shiftOutMod(din_pin, clk_pin, LSBFIRST, CLOCK_TYPE, CLOCK_DELAY_US, wartosc);
- digitalWrite(stb_pin, HIGH);
- }
- void LEDKEY::reset()
- {
- sendCommand(0x40);
- digitalWrite(stb_pin, LOW);
- shiftOutMod(din_pin, clk_pin, LSBFIRST, CLOCK_TYPE, CLOCK_DELAY_US, 0xc0); // reset od adresu 0
- for (uint8_t i = 0; i < 16; i++) shiftOutMod(din_pin, clk_pin, LSBFIRST, CLOCK_TYPE, CLOCK_DELAY_US, 0x00); // 8x LED + 8x DIGIT reset
- digitalWrite(stb_pin, HIGH);
- }
- uint8_t LEDKEY::shiftInMod(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t clock_type, uint16_t clock_delay_us)
- {
- uint8_t value = 0;
- for (uint8_t i = 0; i < 8; ++i)
- {
- digitalWrite(clockPin, (clock_type ? LOW : HIGH));
- delayMicroseconds(clock_delay_us);
- if (bitOrder == LSBFIRST) value |= digitalRead(dataPin) << i;
- else value |= digitalRead(dataPin) << (7 - i);
- digitalWrite(clockPin, (clock_type ? HIGH : LOW));
- }
- return value;
- }
- void LEDKEY::shiftOutMod(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t clock_type, uint16_t clock_delay_us, uint8_t val)
- {
- for (uint8_t i = 0; i < 8; i++)
- {
- if (bitOrder == LSBFIRST) digitalWrite(dataPin, !!(val & (1 << i)));
- else digitalWrite(dataPin, !!(val & (1 << (7 - i))));
- digitalWrite(clockPin, (clock_type ? LOW : HIGH));
- delayMicroseconds(clock_delay_us);
- digitalWrite(clockPin, (clock_type ? HIGH : LOW));
- }
- }
- // -----------------------------------------------------------------------------------------------------------------------------------------
- // --- definicje klasy LEDKEY --------------------------------------------------------------------------------------------------------------
- // -----------------------------------------------------------------------------------------------------------------------------------------
- // --- definicja obsługi przycisków---------------------------------------------------------------------------------------------------------
- // -----------------------------------------------------------------------------------------------------------------------------------------
- word LEDKEY::Button()
- {
- word button = readButtons(); // pobierz do zmienniej button kod przycisku z modulu LED&KEY
- switch (button)
- {
- case 1: return 1; // zwroc kod 1
- break; // przerwij dalej
- case 2: return 2; // zwroc kod 2
- break; // przerwij dalej
- case 4: return 3; // zwroc kod 3
- break; // przerwij dalej
- case 8: return 4; // zwroc kod 4
- break; // przerwij dalej
- case 16: return 5; // zwroc kod 5
- break; // przerwij dalej
- case 32: return 6; // zwroc kod 6
- break; // przerwij dalej
- case 64: return 7; // zwroc kod 7
- break; // przerwij dalej
- case 128: return 8; // zwroc kod 8
- break; // przerwij dalej
- default: return 0; // zwroc kod 0
- break; // przerwij dalej
- }
- }
- uint8_t LEDKEY::readButtons(void)
- {
- uint8_t buttons = 0;
- digitalWrite(stb_pin, LOW);
- shiftOutMod(din_pin, clk_pin, LSBFIRST, CLOCK_TYPE, CLOCK_DELAY_US, 0x42);
- pinMode(din_pin, INPUT);
- for (uint8_t i = 0; i < 4; i++)
- {
- uint8_t v = shiftInMod(din_pin, clk_pin, LSBFIRST, CLOCK_TYPE, CLOCK_DELAY_US) << i;
- buttons |= v;
- }
- pinMode(din_pin, OUTPUT);
- digitalWrite(stb_pin, HIGH);
- return buttons;
- }
- // -----------------------------------------------------------------------------------------------------------------------------------------
- // --- KONIEC KLASY KEYLED -----------------------------------------------------------------------------------------------------------------
- // -----------------------------------------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement