Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <io.h>
- #include <system.h>
- #include <definitions.h>
- #include "altera_up_avalon_parallel_port.h"
- #include "sys/alt_irq.h"
- #include "sys/alt_timestamp.h"
- #include "alt_types.h"
- struct alt_up_dev
- {
- alt_up_parallel_port_dev * uchwytSW;
- alt_up_parallel_port_dev * uchwytLEDR;
- alt_up_parallel_port_dev * uchwytLEDG;
- alt_up_parallel_port_dev * uchwytHEX3;
- };
- // Mozliwosci ledow
- enum LEDS_TYPE {
- GREEN = 0,
- RED = 1
- };
- // Sprawdza jaki typ jest ustawiony i zwraca odwołanie do odpowiednich diodek (Baza czerwonych albo baza zielonych)
- int getLedsBase(int type) {
- int ledsBase = -1;
- switch(type) {
- case RED:
- ledsBase = LEDS_RED_BASE;
- break;
- case GREEN:
- ledsBase = LEDS_GREEN_BASE;
- break;
- }
- return ledsBase;
- }
- // Zwraca uchwy odpowiedniego typu diodek zaleznego od typu (czerwone / zielone) wykorzystujac uchwyty.
- int getLeds(int type, struct alt_up_dev *up_dev) {
- if(up_dev) {
- if(type == RED) {
- return alt_up_parallel_port_read_data(up_dev->uchwytLEDR);
- }
- if(type == GREEN) {
- return alt_up_parallel_port_read_data(up_dev->uchwytLEDG);
- }
- } else {
- return IORD(getLedsBase(type), 0);
- }
- }
- // Ustawia odpowiednią diodkę daną wartościa wykorzystując uchwyty
- void setLeds(int type, int value, struct alt_up_dev *up_dev) {
- if(up_dev) {
- if(type == RED) {
- alt_up_parallel_port_write_data(up_dev->uchwytLEDR, value);
- }
- if(type == GREEN) {
- alt_up_parallel_port_write_data(up_dev->uchwytLEDG, value);
- }
- } else {
- IOWR(getLedsBase(type), 0, value);
- }
- }
- // Ustwia odpowiednio wartosci czerwonych ledow
- void printLeds(int leds, struct alt_up_dev *up_dev) {
- setLeds(RED, leds, up_dev);
- }
- // Ustawia wartosci czerwonych ledow z opoznieniem
- void printLedsWithDelay(int leds, struct alt_up_dev *up_dev) {
- // Czasy delayow
- const int ledOnDelay = 1;
- const int ledOffDelay = 2;
- //const int hexOnDelay = 0;
- //const int hexOffDelay = 3;
- const int errorLedOnDelay = 1;
- const int errorLedOfDelay = 3;
- //const int errorHexOnDelay = 1;
- //const int errorHexOfDelay = 1;
- // Pomocnicze
- int isError = 0;
- int currentAltNTicks = 0;
- int currentDelay = 0;
- // Obecnie zapalone ledy
- int previousLEDS = getLeds(RED, up_dev);
- // Jesli sie nie zmienia nic to zostaje jak jest
- if (previousLEDS == leds) {
- return;
- }
- // Sprawdzamy czy akcja dotyczy errora, czy był aktywny lub czy zmiana stanu sprawila ze jest aktywny
- if (leds == LED17 || previousLEDS == LED17) {
- isError = 1;
- }
- // Jesli leds bedzie wieksze, to znaczy ze suma logiczna ukladu sie zwiekszyla, wiec led musial zostac zapalony. Dlatego ustalamy czasy delaya na wlaczanie
- if(leds > previousLEDS) {
- // Jesli jest error to ustawiamy czas erroru, jesli nie to normalny
- if(isError == 1) {
- currentDelay = errorLedOnDelay;
- } else {
- currentDelay = ledOnDelay;
- }
- } // Jesli nie, to znaczy ze led zostaje wygaszony, dlatego ustawiamy czas delaya na czas wygaszania
- else {
- if(isError == 1) {
- currentDelay = errorLedOfDelay;
- } else {
- currentDelay = ledOffDelay;
- }
- }
- // Rozpoczynamy odliczanie czasu
- currentAltNTicks = alt_nticks();
- // Dopoki nie minie okreslona ilosc czasu obliczona na podstawie ustawionego delayu nie dzieje sie nic
- while(alt_nticks() - currentAltNTicks < currentDelay * alt_ticks_per_second()) {
- // Po prostu czekamy na uplyw odpowiedniej liczby sekund
- }
- // Ustawiamy odpowiednie ledy
- setLeds(RED, leds, up_dev);
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Mozliwosci hexow
- enum HEX_TYPE
- {
- THREE = 0
- };
- // Cyfry na hexie
- int NumberTo7SEG(int number) {
- int cHEX = 0;
- switch (number) {
- case 0:
- cHEX = SEGA | SEGB | SEGC | SEGD | SEGE | SEGF;
- break;
- case 1:
- cHEX = SEGB | SEGC;
- break;
- case 2:
- cHEX = SEGA | SEGB | SEGG | SEGE | SEGD;
- break;
- case 3:
- cHEX = SEGA | SEGB | SEGG | SEGC | SEGD;
- break;
- case 4:
- cHEX = SEGF | SEGG | SEGB | SEGC;
- break;
- case 5:
- cHEX = SEGA | SEGF | SEGG | SEGD | SEGC;
- break;
- case 6:
- cHEX = SEGA | SEGF | SEGE | SEGG | SEGD | SEGC;
- break;
- case 7:
- cHEX = SEGA | SEGB | SEGC;
- break;
- case 8:
- cHEX = SEGA | SEGB | SEGC | SEGD | SEGE | SEGF | SEGG;
- break;
- case 98:
- cHEX = SEGA | SEGB | SEGC | SEGD | SEGF | SEGG;
- break;
- default:
- cHEX = -1;
- break;
- }
- return cHEX;
- }
- // Zwraca baze hexow
- int getHexBase(int type) {
- int hexBase = -1;
- switch(type) {
- case THREE:
- hexBase = HEX_3_BASE;
- break;
- }
- return hexBase;
- }
- int getHex(int type, struct alt_up_dev *up_dev) {
- if(up_dev) {
- if(type == THREE) {
- return alt_up_parallel_port_read_data(up_dev->uchwytHEX3);
- }
- } else {
- return IORD(getHexBase(type), 0);
- }
- }
- // Ustawia odpowiedni hex daną wartościa wykorzystując uchwyty
- void setHex(int type, int value, struct alt_up_dev *up_dev) {
- if (up_dev) {
- if(type == THREE) {
- return alt_up_parallel_port_write_data(up_dev->uchwytHEX3, value);
- }
- } else {
- IOWR(getHexBase(type), 0, value);
- }
- }
- void printHex(int x, struct alt_up_dev *up_dev) {
- int cHEX = NumberTo7SEG(x);
- int error = 0;
- if (cHEX != -1) {
- setHex(cHEX, THREE, up_dev);
- }
- else {
- setHex((HEXE << 16) | (HEXr << 8) | HEXr, THREE, up_dev);
- }
- }
- void printHexWithDelay(int x, struct alt_up_dev *up_dev) {
- // Czasy delayow
- //const int ledOnDelay = 2;
- //const int ledOffDelay = 2;
- const int hexOnDelay = 0;
- const int hexOffDelay = 0;
- //const int errorLedOnDelay = 1;
- //const int errorLedOfDelay = 1;
- const int errorHexOnDelay = 0;
- const int errorHexOfDelay = 0;
- // Pomocnicze
- int isError = 0;
- int currentAltNTicks = 0;
- int currentDelay = 0;
- // Obecnie zapalone ledy
- int previousHex = getHex(THREE, up_dev);
- // Jesli sie nie zmienia nic to zostaje jak jest
- if (previousHex == x) {
- return;
- }
- // Sprawdzamy czy akcja dotyczy errora, czy był aktywny lub czy zmiana stanu sprawila ze jest aktywny
- if (x == ((HEXE << 16) | (HEXr << 8) | HEXr) || previousHex == ((HEXE << 16) | (HEXr << 8) | HEXr)) {
- isError = 1;
- }
- // Jesli leds bedzie wieksze, to znaczy ze suma logiczna ukladu sie zwiekszyla, wiec led musial zostac zapalony. Dlatego ustalamy czasy delaya na wlaczanie
- if(x > previousHex) {
- // Jesli jest error to ustawiamy czas erroru, jesli nie to normalny
- if(isError == 1) {
- currentDelay = errorHexOnDelay;
- } else {
- currentDelay = hexOnDelay;
- }
- } // Jesli nie, to znaczy ze led zostaje wygaszony, dlatego ustawiamy czas delaya na czas wygaszania
- else {
- if(isError == 1) {
- currentDelay = errorHexOfDelay;
- } else {
- currentDelay = hexOffDelay;
- }
- }
- // Rozpoczynamy odliczanie czasu
- currentAltNTicks = alt_nticks();
- // Dopoki nie minie okreslona ilosc czasu obliczona na podstawie ustawionego delayu nie dzieje sie nic
- while(alt_nticks() - currentAltNTicks < currentDelay * alt_ticks_per_second()) {
- // Po prostu czekamy na uplyw odpowiedniej liczby sekund
- }
- // Ustawiamy odpowiednie ledy
- setHex(THREE, x, up_dev);
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Ustawienie outputow z obslugi przerwan
- void fillOutputs(int currentSW, int *LEDS, int *HEX) {
- int currentLED = 0;
- int currentHEX = 0;
- // Uzywane sliderki
- /*
- int switch_0 = currentSW & SW0;
- int switch_1 = currentSW & SW1;
- int switch_2 = currentSW & SW2;
- int switch_3 = currentSW & SW3;
- int switch_4 = currentSW & SW4;
- int switch_5 = currentSW & SW5;
- int switch_6 = currentSW & SW6;
- */
- int state = currentSW & (SW0|SW1|SW2|SW3|SW4|SW5|SW6);
- switch(state) {
- case 0:
- break;
- case 1:
- currentLED = currentLED|LED0;
- currentHEX = 1;
- if((currentSW & SW7) && (currentSW & SW8)) {
- currentLED = LED17;
- currentHEX = -1;
- } else if((currentSW & SW7)) {
- currentLED = currentLED|LED7;
- currentHEX = 8;
- } else if((currentSW & SW8)) {
- currentLED = currentLED|LED8;
- currentHEX = 9;
- }
- break;
- case 2:
- currentLED = currentLED|LED1;
- currentHEX = 2;
- break;
- case 4:
- currentLED = currentLED|LED2;
- currentHEX = 3;
- break;
- case 8:
- currentLED = currentLED|LED3;
- currentHEX = 4;
- if(((currentSW & SW9) && (currentSW & SW10)) ||
- ((currentSW & SW9) && (currentSW & SW11)) ||
- ((currentSW & SW9) && (currentSW & SW12)) ||
- ((currentSW & SW10) && (currentSW & SW11)) ||
- ((currentSW & SW10) && (currentSW & SW12)) ||
- ((currentSW & SW11) && (currentSW & SW12))) {
- currentLED = LED17;
- currentHEX = -1;
- } else if((currentSW & SW9)) {
- currentLED = currentLED|LED9;
- currentHEX = 9;
- } else if((currentSW & SW10)) {
- currentLED = currentLED|LED10;
- currentHEX = 9;
- } else if((currentSW & SW11)) {
- currentLED = currentLED|LED11;
- currentHEX = 9;
- } else if((currentSW & SW12)) {
- currentLED = currentLED|LED12;
- currentHEX = 9;
- }
- break;
- case 16:
- currentLED = currentLED|LED4;
- currentHEX = 5;
- break;
- case 32:
- currentLED = currentLED|LED5;
- currentHEX = 6;
- break;
- case 64:
- currentLED = currentLED|LED6;
- currentHEX = 7;
- break;
- default:
- currentLED = LED17;
- currentHEX = -1;
- break;
- }
- *LEDS = currentLED;
- *HEX = currentHEX;
- }
- // Obsluga przerwan
- void interrupt_handler(struct alt_up_dev* up_dev, alt_u32 id) {
- int currentLED = 0;
- int currentHEX = 0;
- int currentSW = 0;
- // Pobranie wartosci ze switchow
- currentSW = alt_up_parallel_port_read_data(up_dev->uchwytSW);
- // Ustawienie odpowiednich outputow
- fillOutputs(currentSW, ¤tLED, ¤tHEX);
- // Wyswietalnie na diodach
- printLedsWithDelay(currentLED, up_dev);
- // Wyswietlanie na hexach
- printHex(currentHEX, up_dev);
- }
- // main
- int main() {
- printf("aaa");
- while(1) {
- struct alt_up_dev up_dev;
- alt_up_parallel_port_dev *uchwytSW = alt_up_parallel_port_open_dev("/dev/SW_SLIDERS");
- alt_up_parallel_port_dev *uchwytLEDR = alt_up_parallel_port_open_dev("/dev/LEDS_RED");
- alt_up_parallel_port_dev *uchwytLEDG = alt_up_parallel_port_open_dev("/dev/LEDS_GREEN");
- alt_up_parallel_port_dev * uchwytHEX3 = alt_up_parallel_port_open_dev("/dev/HEX_3");
- if (uchwytSW != NULL) {
- up_dev.uchwytSW = uchwytSW;
- }
- if (uchwytLEDG != NULL) {
- up_dev.uchwytLEDG = uchwytLEDG;
- }
- if (uchwytLEDR != NULL) {
- up_dev.uchwytLEDR = uchwytLEDR;
- }
- if (uchwytHEX3 != NULL) {
- up_dev.uchwytHEX3 = uchwytHEX3;
- }
- alt_irq_register(1, (void *)&up_dev, (void *)interrupt_handler);
- alt_up_parallel_port_set_interrupt_mask(uchwytSW, 0xff);
- }
- }
Add Comment
Please, Sign In to add comment