Advertisement
Sothian

alt??

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