Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <system.h>
  3. #include "includes.h"
  4. #include <io.h>
  5.  
  6. #define SEGA 0x00001
  7. #define SEGB 0x00002
  8. #define SEGC 0x00004
  9. #define SEGD 0x00008
  10. #define SEGE 0x00010
  11. #define SEGF 0x00020
  12. #define SEGG 0x00040
  13.  
  14. #define SW0 0x00000001
  15. #define SW1 0x00000002
  16. #define SW2 0x00000004
  17. #define SW3 0x00000008
  18. #define SW4 0x00000010
  19. #define SW5 0x00000020
  20. #define SW6 0x00000040
  21. #define SW7 0x00000080
  22. #define SW8 0x00000100
  23. #define SW9 0x00000200
  24.  
  25. // leds
  26. #define LED0 0x00000001
  27. #define LED1 0x00000002
  28. #define LED2 0x00000004
  29. #define LED3 0x00000008
  30. #define LED4 0x00000010
  31. #define LED5 0x00000020
  32. #define LED6 0x00000040
  33. #define LED7 0x00000080
  34. #define LED8 0x00000100
  35. #define LED9 0x00000200
  36.  
  37. #define ZERO SEGA | SEGB | SEGC | SEGD |SEGE | SEGF
  38. #define ONE SEGB | SEGC
  39. #define TWO SEGA | SEGB | SEGG | SEGE | SEGD
  40. #define THREE SEGA | SEGB | SEGC | SEGD | SEGG
  41. #define FOUR SEGB | SEGC | SEGF | SEGG
  42. #define FIVE SEGA | SEGC | SEGD | SEGF | SEGG
  43. #define SIX SEGA | SEGC | SEGD | SEGE | SEGF | SEGG
  44. #define SEVEN SEGA | SEGB | SEGC | SEGG
  45. #define EIGHT SEGA | SEGB | SEGC | SEGD | SEGE | SEGF | SEGG
  46. #define NINE SEGA | SEGB | SEGC | SEGD | SEGF | SEGG
  47. #define E SEGA | SEGD | SEGE | SEGF | SEGG
  48. #define R SEGE | SEGG
  49.  
  50. #define A SEGB | SEGC
  51. #define M SEGA | SEGB | SEGC | SEGE | SEGF
  52. #define F SEGA | SEGB | SEGD | SEGE | SEGG
  53.  
  54. int AM = 0;
  55. int FM = 0;
  56. int max = 99;
  57. int state = 0;
  58. void VGA_text(int, int, char *);
  59. int zmienZakres(int swstate, int liczba) { // zwraca true, jesli zmieni zakres poprawnie
  60.  
  61. if ((swstate & SW0)) { // AM
  62. if ((swstate & SW6)) { // dodawanie
  63. if (AM + liczba < max)
  64. AM += liczba;
  65. else
  66. return 0;
  67. } else if ((swstate & SW7)) { // odejmowanie
  68. if (AM - liczba > 0)
  69. AM -= liczba;
  70. else
  71. return 0;
  72. }
  73. }
  74. if ((swstate & SW1)) { // FM
  75. if ((swstate & SW6)) { // dodawanie
  76. if (FM + liczba < max)
  77. FM += liczba;
  78. else
  79. return 0;
  80. } else if ((swstate & SW7)) { // odejmowanie
  81. if (FM - liczba > 0)
  82. FM -= liczba;
  83. else
  84. return 0;
  85. }
  86. }
  87.  
  88. return 1;
  89. }
  90.  
  91. struct interrupt_data {
  92. int myLEDS;
  93. int mySLIDERS;
  94. int myHEX;
  95.  
  96. };
  97. struct interrupt_data *data;
  98.  
  99. /* Definition of Task Stacks */
  100. #define TASK_STACKSIZE 2048
  101. OS_STK task1_stk[TASK_STACKSIZE];
  102. OS_STK task2_stk[TASK_STACKSIZE];
  103. OS_STK task3_stk[TASK_STACKSIZE];
  104. OS_STK task4_stk[TASK_STACKSIZE];
  105. OS_STK task5_stk[TASK_STACKSIZE];
  106. OS_STK task6_stk[TASK_STACKSIZE];
  107. OS_STK task7_stk[TASK_STACKSIZE];
  108. OS_STK task8_stk[TASK_STACKSIZE];
  109. OS_STK task9_stk[TASK_STACKSIZE];
  110.  
  111. OS_EVENT *SWBox0;
  112. OS_EVENT *SWBox1;
  113. OS_EVENT *SWBox2;
  114. OS_EVENT *SWBox3;
  115. OS_EVENT *SWBox4;
  116. OS_EVENT *SWBox5;
  117. OS_EVENT *SWBox6;
  118. OS_EVENT *SWBox7;
  119. OS_EVENT *SWBox8;
  120. OS_EVENT *SWBox9;
  121.  
  122. int numer1 = 0;
  123. int numer2 = 0;
  124. int numer3 = 0;
  125. int numer4 = 0;
  126.  
  127. alt_alarm error_alarm;
  128. int errorWorking = 0;
  129. alt_u32 led_alarm_proc(void *context) {
  130. //printf("WYWOLUJE ALARM %d", (int)context);
  131. errorWorking = 0;
  132. IOWR(data->myLEDS, 0, 0); // gasimy wszystkie diody
  133. IOWR(data->myLEDS, 0, SW9);
  134. return 0;
  135.  
  136. }
  137. int time_delay = 2;
  138. void taskError(void *pdata) {
  139.  
  140. while (1) {
  141. int err;
  142. OSTimeDlyHMSM(0, 0, 0, 10);
  143. int *num = OSMboxPend(SWBox1, 0, &err);
  144. int swstate = *num;
  145. if (((swstate & SW0) && (swstate & SW1))
  146. || ((swstate & SW6) && (swstate & SW7))) {
  147. //IOWR(data->myLEDS, 0, 0); // gasimy wszystkie diody
  148. //IOWR(data->myLEDS, 0, SW9);
  149. if (errorWorking == 0) {
  150. errorWorking = 1;
  151. alt_alarm_start(&error_alarm, alt_ticks_per_second() * time_delay,
  152. led_alarm_proc, swstate);
  153. }
  154. IOWR(data->myHEX, 0, R);
  155. IOWR(data->myHEX, 1, R);
  156. IOWR(data->myHEX, 2, E);
  157. IOWR(data->myHEX, 3, ZERO);
  158. IOWR(data->myHEX, 4, ZERO);
  159. char text1_1_row[40] = "***ERROR***";
  160. VGA_text(30, 15, text1_1_row);
  161. }
  162.  
  163. }
  164. }
  165.  
  166. void task3(void *pdata) {
  167. data->myLEDS = LEDS_BASE;
  168. data->mySLIDERS = SW_SLIDERS_BASE;
  169. data->myHEX = HEX_BASE;
  170. while (1) {
  171. int err;
  172. OSTimeDlyHMSM(0, 0, 2, 10);
  173. int *num = OSMboxPend(SWBox1, 0, &err);
  174. int swstate = *num;
  175. if (!((swstate & SW0) && (swstate & SW1))
  176. || ((swstate & SW6) && (swstate & SW7))) {
  177. if (swstate & SW0) {
  178. IOWR(NEW_COMPONENT_0_BASE, 0, 0x0001);
  179. IOWR(data->myLEDS, 0, LED0);
  180. IOWR(data->myHEX, 0, M);
  181. IOWR(data->myHEX, 1, A);
  182. char text1_1_row[40] = "***Grzanie gora***";
  183. VGA_text(30, 15, text1_1_row);
  184.  
  185. }
  186. if (swstate & SW1) {
  187. IOWR(data->myLEDS, 0, LED1);
  188. IOWR(data->myHEX, 0, M);
  189. IOWR(data->myHEX, 1, F);
  190. char text1_1_row[40] = "***Grzanie dol***";
  191. VGA_text(30, 15, text1_1_row);
  192. }
  193. }
  194. }
  195.  
  196. }
  197. void task4(void *pdata) {
  198. data->myLEDS = LEDS_BASE;
  199. data->mySLIDERS = SW_SLIDERS_BASE;
  200. data->myHEX = HEX_BASE;
  201.  
  202. while (1) {
  203. int err;
  204. int *num = OSMboxPend(SWBox1, 0, &err);
  205. int swstate = *num;
  206. if (!((swstate & SW0) && (swstate & SW1))
  207. || ((swstate & SW6) && (swstate & SW7))) {
  208. if (swstate & SW0) {
  209. IOWR(data->myHEX, 1, A);
  210. char text1_1_row[40] = "***Grzenie gora***";
  211. VGA_text(30, 15, text1_1_row);
  212. }
  213. if (swstate & SW1) {
  214. IOWR(data->myHEX, 1, F);
  215. char text1_1_row[40] = "***Grzanie dol***";
  216. VGA_text(30, 15, text1_1_row);
  217. }
  218. if ((swstate & SW2)) {
  219. char text1_1_row[40] = "Dodano 1";
  220. VGA_text(45, 15, text1_1_row);
  221.  
  222. if (swstate & SW0) {
  223. if (swstate & SW6)
  224. IOWR(data->myLEDS, 0, LED0 | LED2 | LED6);
  225. if (swstate & SW7)
  226. IOWR(data->myLEDS, 0, LED0 | LED2 | LED7);
  227. } else {
  228. if (swstate & SW6)
  229. IOWR(data->myLEDS, 0, LED1 | LED2 | LED6);
  230. if (swstate & SW7)
  231. IOWR(data->myLEDS, 0, LED1 | LED2 | LED7);
  232. }}
  233. else {
  234. if (swstate & SW0) {
  235. if (swstate & SW6)
  236. IOWR(data->myLEDS, 0, LED0 | LED6);
  237. if (swstate & SW7)
  238. IOWR(data->myLEDS, 0, LED0 | LED7);
  239. } else {
  240. if (swstate & SW6)
  241. IOWR(data->myLEDS, 0, LED1 | LED6);
  242. if (swstate & SW7)
  243. IOWR(data->myLEDS, 0, LED1 | LED7);
  244. }
  245. }
  246. }
  247. }
  248.  
  249. }
  250. void task5(void *pdata) {
  251. data->myLEDS = LEDS_BASE;
  252. data->mySLIDERS = SW_SLIDERS_BASE;
  253. data->myHEX = HEX_BASE;
  254. while (1) {
  255. int err;
  256. int *num = OSMboxPend(SWBox1, 0, &err);
  257. int swstate = *num;
  258.  
  259. if (swstate & SW8) {
  260. if (swstate & SW0) {
  261. IOWR(data->myHEX, 1, A);
  262. }
  263. if (swstate & SW1) {
  264. IOWR(data->myHEX, 0, M);
  265. IOWR(data->myHEX, 1, F);
  266. }
  267. if ((swstate & SW2)) {
  268. IOWR(data->myHEX, 3, ONE);
  269. IOWR(data->myHEX, 4, ZERO);
  270. if (swstate & SW0) {
  271. numer1 = AM;
  272. IOWR(data->myLEDS, 0, LED0 | LED2 | LED8);
  273. } else {
  274. numer1 = FM;
  275. IOWR(data->myLEDS, 0, LED1 | LED2 | LED8);
  276. }
  277. }
  278. if ((swstate & SW3)) {
  279. IOWR(data->myHEX, 3, TWO);
  280. IOWR(data->myHEX, 4, ZERO);
  281. if (swstate & SW0) {
  282. numer2 = AM;
  283. IOWR(data->myLEDS, 0, LED0 | LED3 | LED8);
  284. } else {
  285. numer2 = FM;
  286. IOWR(data->myLEDS, 0, LED1 | LED3 | LED8);
  287. }
  288. }
  289.  
  290. if ((swstate & SW4)) {
  291. IOWR(data->myHEX, 3, THREE);
  292. IOWR(data->myHEX, 4, ZERO);
  293. if (swstate & SW0) {
  294. numer3 = AM;
  295. IOWR(data->myLEDS, 0, LED0 | LED4 | LED8);
  296. } else {
  297. numer3 = FM;
  298. IOWR(data->myLEDS, 0, LED1 | LED4 | LED8);
  299. }
  300. }
  301.  
  302. if ((swstate & SW5)) {
  303. IOWR(data->myHEX, 3, FOUR);
  304. IOWR(data->myHEX, 4, ZERO);
  305. if (swstate & SW0) {
  306. numer4 = AM;
  307. IOWR(data->myLEDS, 0, LED0 | LED5 | LED8);
  308. } else {
  309. numer4 = FM;
  310. IOWR(data->myLEDS, 0, LED1 | LED5 | LED8);
  311. }
  312. }
  313. }
  314. }
  315.  
  316. }
  317. void task6(void *pdata) {
  318.  
  319. }
  320. void task7(void *pdata) {
  321.  
  322. }
  323. void task8(void *pdata) {
  324.  
  325. }
  326. void task9(void *pdata) {
  327.  
  328. }
  329.  
  330. /* Definition of Task Priorities */
  331.  
  332. #define TASK1_PRIORITY 2
  333. #define TASK2_PRIORITY 3
  334. #define TASK3_PRIORITY 11
  335. #define TASK4_PRIORITY 4
  336. #define TASK5_PRIORITY 5
  337. #define TASK6_PRIORITY 6
  338. #define TASK7_PRIORITY 7
  339. #define TASK8_PRIORITY 8
  340. #define TASK9_PRIORITY 9
  341.  
  342. //alt_up_parallel_port_dev const *uchwytLEDR; = alt_up_parallel_port_open_dev("/dev/parallel_port_2");
  343. void clearScreen() {
  344. char text1_1_row[40] =
  345. " ";
  346. VGA_text(30, 15, text1_1_row);
  347. }
  348.  
  349. int startTicksTime = 0;
  350. int startTicksTime2 = 0;
  351. void task2(void* pdata) {
  352. int oldState = -1;
  353.  
  354. data->myLEDS = LEDS_BASE;
  355. data->mySLIDERS = SW_SLIDERS_BASE;
  356. data->myHEX = HEX_BASE;
  357.  
  358. int oldValue = -1;
  359. int swstate = 0;
  360. int *num;
  361. do {
  362.  
  363. swstate = IORD(SW_SLIDERS_BASE, 0);
  364. do {
  365. switch (state) {
  366. case 0: // dla nieustalonego stanu
  367.  
  368. if (((swstate & SW0) && (swstate & SW1))
  369. || ((swstate & SW6) && (swstate & SW7))) {
  370. state = 1;
  371. } else if (((swstate & SW0) || (swstate & SW1))
  372. && ((swstate & SW6) || (swstate & SW7))) {
  373. state = 9;
  374. } else if (((swstate & SW0) || (swstate & SW1))
  375. && (swstate & SW8)) {
  376. state = 8;
  377. } else if ((swstate & SW0) || (swstate & SW1)) {
  378. state = 2;
  379. } else {
  380. IOWR(NEW_COMPONENT_0_BASE, 0, 0x0000);
  381. clearScreen();
  382. IOWR(data->myLEDS, 0, 0);
  383. for (int i = 0; i <= 5; i++) {
  384. IOWR(data->myHEX, i, 0);
  385. }
  386. }
  387. break;
  388. case 1: // ERROR
  389. IOWR(NEW_COMPONENT_0_BASE, 0, 3);
  390. OSMboxPostOpt(SWBox1, &swstate, OS_POST_OPT_BROADCAST);
  391.  
  392. OSTimeDlyHMSM(0, 0, 0, 10);
  393. state = 0;
  394. break;
  395.  
  396. case 2:
  397. OSTimeDlyHMSM(0, 0, 1, 0);
  398. OSMboxPostOpt(SWBox1, &swstate, OS_POST_OPT_BROADCAST);
  399. state = 0;
  400. break;
  401. case 8:
  402. if (startTicksTime2 == 0) {
  403. startTicksTime2 = alt_nticks();
  404. } else if (((alt_nticks() - startTicksTime2)
  405. > (2 * alt_ticks_per_second()))) {
  406. OSMboxPostOpt(SWBox1, &swstate, OS_POST_OPT_BROADCAST);
  407. OSTimeDlyHMSM(0, 0, 0, 10);
  408. startTicksTime2 = 0;
  409. }
  410. break;
  411.  
  412. case 9:
  413. if (startTicksTime == 0) {
  414. startTicksTime = alt_nticks();
  415. } else if (((alt_nticks() - startTicksTime)
  416. > (2 * alt_ticks_per_second()))) {
  417. OSMboxPostOpt(SWBox1, &swstate, OS_POST_OPT_BROADCAST);
  418. OSTimeDlyHMSM(0, 0, 0, 10);
  419. startTicksTime = 0;
  420. }
  421. state = 0;
  422. break;
  423. }
  424. } while (state != 0);
  425.  
  426. oldValue = swstate;
  427. } while (1);
  428. //oldValue = swstate;
  429. }
  430.  
  431. /* Prints "Hello World" and sleeps for three seconds */
  432. void task1(void* pdata) {
  433. while (1) {
  434. int sw;
  435. int * msg;
  436. sw = IORD(SW_SLIDERS_BASE, 0);
  437. printf("%d", sw);
  438.  
  439. //OSMboxPostOpt(SWBox0, &sw, OS_POST_OPT_BROADCAST);
  440.  
  441. OSTimeDlyHMSM(0, 0, 0, 10);
  442. }
  443. }
  444. /* Prints "Hello World" and sleeps for three seconds */
  445.  
  446. /* The main function creates two task and starts multi-tasking */
  447. int main(void) {
  448.  
  449.  
  450.  
  451. /* create a message to be displayed on the VGA display */
  452.  
  453. /* output text message near the middle of the VGA monitor */
  454.  
  455. SWBox1 = OSMboxCreate((void*) 0); // error
  456.  
  457. OSTaskCreateExt(task1,
  458. NULL, (void *) &task1_stk[TASK_STACKSIZE - 1],
  459. TASK1_PRIORITY,
  460. TASK1_PRIORITY, task1_stk,
  461. TASK_STACKSIZE,
  462. NULL, 0);
  463.  
  464. OSTaskCreateExt(task2,
  465. NULL, (void *) &task2_stk[TASK_STACKSIZE - 1],
  466. TASK2_PRIORITY,
  467. TASK2_PRIORITY, task2_stk,
  468. TASK_STACKSIZE,
  469. NULL, 0);
  470.  
  471. OSTaskCreateExt(taskError,
  472. NULL, (void *) &task3_stk[TASK_STACKSIZE - 1],
  473. TASK3_PRIORITY,
  474. TASK3_PRIORITY, task3_stk,
  475. TASK_STACKSIZE,
  476. NULL, 0);
  477.  
  478. OSTaskCreateExt(task3,
  479. NULL, (void *) &task4_stk[TASK_STACKSIZE - 1],
  480. TASK4_PRIORITY,
  481. TASK4_PRIORITY, task4_stk,
  482. TASK_STACKSIZE,
  483. NULL, 0);
  484.  
  485. OSTaskCreateExt(task4,
  486. NULL, (void *) &task5_stk[TASK_STACKSIZE - 1],
  487. TASK5_PRIORITY,
  488. TASK5_PRIORITY, task5_stk,
  489. TASK_STACKSIZE,
  490. NULL, 0);
  491. OSTaskCreateExt(task5,
  492. NULL, (void *) &task6_stk[TASK_STACKSIZE - 1],
  493. TASK6_PRIORITY,
  494. TASK6_PRIORITY, task6_stk,
  495. TASK_STACKSIZE,
  496. NULL, 0);
  497.  
  498. OSStart();
  499.  
  500. return 0;
  501. }
  502.  
  503. void VGA_text(int x, int y, char * text_ptr) {
  504. int offset;
  505. /* Declare volatile pointer to char buffer (volatile means that IO load
  506. and store instructions will be used to access these pointer locations,
  507. instead of regular memory loads and stores) */
  508. volatile char * character_buffer = (char *) 0x80000; // VGA character buffer
  509.  
  510. /* assume that the text string fits on one line */
  511. offset = (y << 7) + x;
  512. while (*(text_ptr)) {
  513. *(character_buffer + offset) = *(text_ptr); // write to the character buffer
  514. ++text_ptr;
  515. ++offset;
  516. }
  517. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement