Advertisement
Sothian

Przerwania AltNticks

Oct 27th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.45 KB | None | 0 0
  1. struct alt_up_dev
  2. {
  3.     alt_up_parallel_port_dev * uchwytSW;
  4.     alt_up_parallel_port_dev * uchwytLEDR;
  5.     alt_up_parallel_port_dev * uchwytLEDG;
  6.     alt_up_parallel_port_dev * uchwytHEX3;
  7. };
  8. // Mozliwosci ledow
  9. enum LEDS_TYPE {
  10.     GREEN = 0,
  11.     RED = 1
  12. };
  13. // Sprawdza jaki typ jest ustawiony i zwraca odwołanie do odpowiednich diodek (Baza czerwonych albo baza zielonych)
  14. int getLedsBase(int type) {
  15.     int ledsBase = -1;
  16.     switch(type) {
  17.         case RED:
  18.             ledsBase = LEDS_RED_BASE;
  19.             break;
  20.         case GREEN:
  21.             ledsBase = LEDS_GREEN_BASE;
  22.             break;
  23.     }
  24.     return ledsBase;
  25. }
  26. // Zwraca uchwy odpowiedniego typu diodek zaleznego od typu (czerwone / zielone) wykorzystujac uchwyty.
  27. int getLeds(int type, struct alt_up_dev *up_dev) {
  28.     if(up_dev) {
  29.         if(type == RED) {
  30.             return alt_up_parallel_port_read_data(up_dev->uchwytLEDR);
  31.         }
  32.         if(type == GREEN) {
  33.             return alt_up_parallel_port_read_data(up_dev->uchwytLEDG);
  34.         }
  35.     } else {
  36.         return IORD(getLedsBase(type), 0);
  37.     }
  38. }
  39. // Ustawia odpowiednią diodkę daną wartościa wykorzystując uchwyty
  40. void setLeds(int type, int value, struct alt_up_dev *up_dev) {
  41.     if(up_dev) {
  42.         if(type == RED) {
  43.             alt_up_parallel_port_write_data(up_dev->uchwytLEDR, value);
  44.         }
  45.         if(type == GREEN) {
  46.             alt_up_parallel_port_write_data(up_dev->uchwytLEDG, value);
  47.         }
  48.     } else {
  49.         IOWR(getLedsBase(type), 0, value);
  50.     }
  51. }
  52. // Ustwia odpowiednio wartosci czerwonych ledow
  53. void printLeds(int leds, struct alt_up_dev *up_dev) {
  54.     setLeds(RED, leds, up_dev);
  55. }
  56. // Ustawia wartosci czerwonych ledow z opoznieniem
  57. void printLedsWithDelay(int leds, struct alt_up_dev *up_dev) {
  58.     // Czasy delayow
  59.     const int ledOnDelay = 2;
  60.     const int ledOffDelay = 2;
  61.     //const int hexOnDelay = 0;
  62.     //const int hexOffDelay = 3;
  63.     const int errorLedOnDelay = 1;
  64.     const int errorLedOfDelay = 1;
  65.     //const int errorHexOnDelay = 1;
  66.     //const int errorHexOfDelay = 1;
  67.  
  68.     // Pomocnicze
  69.     int isError = 0;
  70.     int currentAltNTicks = 0;
  71.     int currentDelay = 0;
  72.  
  73.     // Obecnie zapalone ledy
  74.     int previousLEDS = getLEDS(RED, up_dev);
  75.  
  76.     // Jesli sie nie zmienia nic to zostaje jak jest
  77.     if (previousLEDS == leds) {
  78.         return;
  79.     }
  80.     // Sprawdzamy czy akcja dotyczy errora, czy był aktywny lub czy zmiana stanu sprawila ze jest aktywny
  81.     if (leds == LED17 || previousLEDS == LED17) {
  82.         isError = 1;
  83.     }
  84.     // Jesli leds bedzie wieksze, to znaczy ze suma logiczna ukladu sie zwiekszyla, wiec led musial zostac zapalony. Dlatego ustalamy czasy delaya na wlaczanie
  85.     if(leds > previousLEDS) {
  86.         // Jesli jest error to ustawiamy czas erroru, jesli nie to normalny
  87.         if(isError == 1) {
  88.             currentDelay = errorLedOnDelay;
  89.         } else {
  90.             currentDelay = ledOnDelay;
  91.         }
  92.     } // Jesli nie, to znaczy ze led zostaje wygaszony, dlatego ustawiamy czas delaya na czas wygaszania
  93.     else {
  94.         if(isError == 1) {
  95.             currentDelay = errorLedOfDelay;
  96.         } else {
  97.             currentDelay = ledOffDelay;
  98.         }
  99.     }
  100.  
  101.     // Rozpoczynamy odliczanie czasu
  102.     currentAltTicks = alt_nticks();
  103.  
  104.     // Dopoki nie minie okreslona ilosc czasu obliczona na podstawie ustawionego delayu nie dzieje sie nic
  105.     while(alt_nticks() - currentAltTicks < currentDelay * alt_ticks_per_second()) {
  106.         // Po prostu czekamy na uplyw odpowiedniej liczby sekund
  107.     }
  108.  
  109.     // Ustawiamy odpowiednie ledy
  110.     setLeds(RED, leds, up_dev);
  111. }
  112.  
  113. /////////////////////////////////////////////////////////////////////////////////////////////////////////
  114.  
  115. // Mozliwosci hexow
  116. enum HEX_TYPE
  117. {
  118.     THREE = 0
  119. };
  120. // Cyfry na hexie
  121. int NumberTo7SEG(int number) {
  122.     int cHEX = 0;
  123.     switch (number) {
  124.         case 0:
  125.             cHEX = SEGA | SEGB | SEGC | SEGD | SEGE | SEGF;
  126.             break;
  127.         case 1:
  128.             cHEX = SEGB | SEGC;
  129.             break;
  130.         case 2:
  131.             cHEX = SEGA | SEGB | SEGG | SEGE | SEGD;
  132.             break;
  133.         case 3:
  134.             cHEX = SEGA | SEGB | SEGG | SEGC | SEGD;
  135.             break;
  136.         case 4:
  137.             cHEX = SEGF | SEGG | SEGB | SEGC;
  138.             break;
  139.         case 5:
  140.             cHEX = SEGA | SEGF | SEGG | SEGD | SEGC;
  141.             break;
  142.         case 6:
  143.             cHEX = SEGA | SEGF | SEGE | SEGG | SEGD | SEGC;
  144.             break;
  145.         case 7:
  146.             cHEX = SEGA | SEGB | SEGC;
  147.             break;
  148.         case 8:
  149.             cHEX = SEGA | SEGB | SEGC | SEGD | SEGE | SEGF | SEGG;
  150.             break;
  151.         case 98:
  152.             cHEX = SEGA | SEGB | SEGC | SEGD | SEGF | SEGG;
  153.             break;
  154.         default:
  155.             cHEX = -1;
  156.             break;
  157.     }
  158.     return cHEX;
  159. }
  160. // Zwraca baze hexow
  161. int getHexBase(int type) {
  162.     int hexBase = -1;
  163.     switch(type) {
  164.         case THREE:
  165.             hexBase = HEX_3_BASE;
  166.             break;
  167.     }
  168.  
  169.     return hexBase;
  170. }
  171. int getHex(int type, struct alt_up_dev *up_dev) {
  172.     if(up_dev) {
  173.         if(type == THREE) {
  174.             return alt_up_parallel_port_read_data(up_dev->uchwytHEX3);
  175.         }
  176.     } else {
  177.         return IORD(getGexBase(type), 0);
  178.     }
  179. }
  180. // Ustawia odpowiedni hex daną wartościa wykorzystując uchwyty
  181. void setHex(int type, int value, struct alt_up_dev *up_dev) {
  182.     if (up_dev) {
  183.         if(type == THREE) {
  184.             return alt_up_parallel_port_write_data(up_dev->uchwytHEX3, value);
  185.         }
  186.     } else {
  187.         IOWR(getHexBase(type), 0, value);
  188.     }
  189. }
  190.  
  191. void printHex(int x, struct alt_up_dev *up_dev) {
  192.     int cHEX = NumberTo7SEG(x);
  193.     int error = 0;
  194.  
  195.     if (cHEX != -1) {
  196.         setHex(cHEX, THREE, up_dev);
  197.     }
  198.     else {
  199.         int errHEX0 = SEGE | SEGG;
  200.         int errHEX1 = SEGE | SEGG;
  201.         int errHEX2 = SEGA | SEGF | SEGG | SEGE | SEGD;
  202.         setHex((errHEX2 << 16) | (errHEX1 << 8) | errHEX0, THREE, up_dev);
  203.     }
  204. }
  205.  
  206. void printHexWithDelay(int x, struct alt_up_dev *up_dev) {
  207.     // Czasy delayow
  208.     //const int ledOnDelay = 2;
  209.     //const int ledOffDelay = 2;
  210.     const int hexOnDelay = 0;
  211.     const int hexOffDelay = 3;
  212.     //const int errorLedOnDelay = 1;
  213.     //const int errorLedOfDelay = 1;
  214.     const int errorHexOnDelay = 1;
  215.     const int errorHexOfDelay = 1;
  216.  
  217.     // Pomocnicze
  218.     int isError = 0;
  219.     int currentAltNTicks = 0;
  220.     int currentDelay = 0;
  221.  
  222.     // Obecnie zapalone ledy
  223.     int previousHex = getHex(THREE, up_dev);
  224.  
  225.     // Jesli sie nie zmienia nic to zostaje jak jest
  226.     if (previousHex == x) {
  227.         return;
  228.     }
  229.     // Sprawdzamy czy akcja dotyczy errora, czy był aktywny lub czy zmiana stanu sprawila ze jest aktywny
  230.     if (x == ((errHEX2 << 16) | (errHEX1 << 8) | errHEX0) || previousHex == ((errHEX2 << 16) | (errHEX1 << 8) | errHEX0)) {
  231.         isError = 1;
  232.     }
  233.     // Jesli leds bedzie wieksze, to znaczy ze suma logiczna ukladu sie zwiekszyla, wiec led musial zostac zapalony. Dlatego ustalamy czasy delaya na wlaczanie
  234.     if(x > previousHex) {
  235.         // Jesli jest error to ustawiamy czas erroru, jesli nie to normalny
  236.         if(isError == 1) {
  237.             currentDelay = errorHexOnDelay;
  238.         } else {
  239.             currentDelay = hexOnDelay;
  240.         }
  241.     } // Jesli nie, to znaczy ze led zostaje wygaszony, dlatego ustawiamy czas delaya na czas wygaszania
  242.     else {
  243.         if(isError == 1) {
  244.             currentDelay = errorHexOfDelay;
  245.         } else {
  246.             currentDelay = hexOffDelay;
  247.         }
  248.     }
  249.  
  250.     // Rozpoczynamy odliczanie czasu
  251.     currentAltTicks = alt_nticks();
  252.  
  253.     // Dopoki nie minie okreslona ilosc czasu obliczona na podstawie ustawionego delayu nie dzieje sie nic
  254.     while(alt_nticks() - currentAltTicks < currentDelay * alt_ticks_per_second()) {
  255.         // Po prostu czekamy na uplyw odpowiedniej liczby sekund
  256.     }
  257.  
  258.     // Ustawiamy odpowiednie ledy
  259.     setHex(THREE, x, up_dev);
  260. }
  261.  
  262. /////////////////////////////////////////////////////////////////////////////////////////////////////////
  263.  
  264. // Ustawienie outputow z obslugi przerwan
  265. void fillOutputs(int currentSW, int *LEDS, int *HEX) {
  266.     int currentLED = 0;
  267.     int currentHEX = 0;
  268.  
  269.     // Uzywane sliderki
  270.     int switch_0 = currentSW & SW0;
  271.     int switch_1 = currentSW & SW1;
  272.     int switch_2 = currentSW & SW2;
  273.     int switch_3 = currentSW & SW3;
  274.     int switch_4 = currentSW & SW4;
  275.     int switch_5 = currentSW & SW5;
  276.     int switch_6 = currentSW & SW6;
  277.  
  278.     switch(currentSW) {
  279.         case 0:
  280.             break;
  281.         case switch_0:
  282.             currentLED = | LED0;
  283.             currentHEX = 1;
  284.             if((currentSW & SW7) && (currentSW & SW8)) {
  285.                 currentLED = LED17;
  286.                 currentHEX = -1;
  287.             } else if((currentSW & SW7)) {
  288.                 currentLED = | LED7;
  289.                 currentHEX = 8;
  290.             } else if((currentSW & SW8)) {
  291.                 currentLED = | LED8;
  292.                 currentHEX = 9;
  293.             }
  294.  
  295.             break;
  296.         case switch_1:
  297.             currentLED = | LED1;
  298.             currentHEX = 2;
  299.             break;
  300.         case switch_2:
  301.             currentLED = | LED2;
  302.             currentHEX = 3;
  303.             break;
  304.         case switch_3:
  305.             currentLED = | LED3;
  306.             currentHEX = 4;
  307.             if(((currentSW & SW9) && (currentSW & SW10)) ||
  308.               ((currentSW & SW9) && (currentSW & SW11)) ||
  309.               ((currentSW & SW9) && (currentSW & SW12)) ||
  310.               ((currentSW & SW10) && (currentSW & SW11)) ||
  311.               ((currentSW & SW10) && (currentSW & SW12)) ||
  312.               ((currentSW & SW11) && (currentSW & SW12))) {
  313.                 currentLED = LED17;
  314.                 currentHEX = -1;
  315.             } else if((currentSW & SW9)) {
  316.                 currentLED = | LED9;
  317.                 currentHEX = 9;
  318.             } else if((currentSW & SW10)) {
  319.                 currentLED = | LED10;
  320.                 currentHEX = 9;
  321.             } else if((currentSW & SW11)) {
  322.                 currentLED = | LED11;
  323.                 currentHEX = 9;
  324.             } else if((currentSW & SW12)) {
  325.                 currentLED = | LED12;
  326.                 currentHEX = 9;
  327.             }
  328.             break;
  329.         case switch_4:
  330.             currentLED = | LED4;
  331.             currentHEX = 5;
  332.             break;
  333.         case switch_5:
  334.             currentLED = | LED5;
  335.             currentHEX = 6;
  336.             break;
  337.         case switch_6:
  338.             currentLED = | LED6;
  339.             currentHEX = 7;
  340.             break;
  341.         default:
  342.             currentLED = LED17;
  343.             currentHEX = -1;
  344.             break;
  345.     }
  346.  
  347.     *LEDS = currentLED;
  348.     *HEX = currentHEX;
  349. }
  350.  
  351. // Obsluga przerwan
  352. void interrupt_handler(struct alt_up_dev* up_dev, alt_u32 id) {
  353.     int currentLED = 0;
  354.     int currentHEX = 0;
  355.     int currentSW = 0;
  356.  
  357.     // Pobranie wartosci ze switchow
  358.     currentSW = alt_up_parallel_port_read_data(up_dev->uchwytSW);
  359.  
  360.     // Ustawienie odpowiednich outputow
  361.     fillOutputs(currentSW, &currentLED, &currentHEX);
  362.  
  363.     // Wyswietalnie na diodach
  364.     printLedsWithDelay(currentLED, up_dev);
  365.  
  366.     // Wyswietlanie na hexach
  367.     printHex(currentHEX0, up_dev);
  368. }
  369.  
  370. // main
  371. int main() {
  372.     while(1) {
  373.         struct alt_up_dev up_dev;
  374.         alt_up_parallel_port_dev *uchwytSW = alt_up_parallel_port_open_dev("/dev/SW_SLIDERS");
  375.         alt_up_parallel_port_dev *uchwytLEDR = alt_up_parallel_port_open_dev("/dev/LEDS_RED");
  376.         alt_up_parallel_port_dev *uchwytLEDG = alt_up_parallel_port_open_dev("/dev/LEDS_GREEN");
  377.         alt_up_parallel_port_dev * uchwytHEX3 = alt_up_parallel_port_open_dev("/dev/HEX_3");
  378.  
  379.         if (uchwytSW != NULL) {
  380.             up_dev.uchwytSW = uchwytSW;
  381.         }  
  382.         if (uchwytLEDG != NULL) {
  383.             up_dev.uchwytLEDG = uchwytLEDG;
  384.         }
  385.         if (uchwytLEDR != NULL) {
  386.             up_dev.uchwytLEDR = uchwytLEDR;
  387.         }
  388.         if (uchwytHEX3 != NULL) {
  389.             up_dev.uchwytHEX3 = uchwytHEX3;
  390.         }
  391.  
  392.         alt_irq_register(1, (void *)&up_dev, (void *)interrupt_handler);
  393.         alt_up_parallel_port_set_interrupt_mask(uchwytSW, 0xff);
  394.     }
  395. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement