Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <system.h>
- #include "altera_up_avalon_character_lcd.h"
- #include "includes.h"
- #include "altera_up_avalon_parallel_port.h"
- #include <stddef.h>
- #include "sys/alt_alarm.h"
- #include "alt_types.h"
- #define SW0 0x00000001
- #define SW1 0x00000002
- #define SW2 0x00000004
- #define SW3 0x00000008
- #define SW4 0x00000010
- #define SW5 0x00000020
- #define SW6 0x00000040
- #define SW7 0x00000080
- #define SW8 0x00000100
- #define SW9 0x00000200
- #define SW10 0x00000400
- #define SW11 0x00000800
- #define SW12 0x00001000
- #define SW13 0x00002000
- #define SW14 0x00004000
- #define SW15 0x00008000
- #define SW16 0x00010000
- #define SW17 0x00020000
- #define KEY1 0x00000002
- #define KEY2 0x00000004
- #define KEY3 0x00000008
- #define LED0 0x00000001
- #define LED1 0x00000002
- #define LED2 0x00000004
- #define LED3 0x00000008
- #define LED4 0x00000010
- #define LED5 0x00000020
- #define LED6 0x00000040
- #define LED7 0x00000080
- #define LED8 0x00000100
- #define LED9 0x00000200
- #define LED10 0x00000400
- #define LED11 0x00000800
- #define LED12 0x00001000
- #define LED13 0x00002000
- #define LED14 0x00004000
- #define LED15 0x00008000
- #define LED16 0x00010000
- #define LED17 0x00020000
- #define SEGA 0x01 // __A__
- #define SEGB 0x02 // | |
- #define SEGC 0x04 // F | | B
- #define SEGD 0x08 // |_____|
- #define SEGE 0x10 // | G |
- #define SEGF 0x20 // E | | C
- #define SEGG 0x40 // |_____|
- //-------------------------//
- /* Definition of Task Stacks */
- #define TASK_STACKSIZE 2048
- OS_STK pokoj1_stk[TASK_STACKSIZE];
- OS_STK pokoj2_stk[TASK_STACKSIZE];
- OS_STK pokoj3_stk[TASK_STACKSIZE];
- OS_STK pokoj4_stk[TASK_STACKSIZE];
- OS_STK pokoj5_stk[TASK_STACKSIZE];
- OS_EVENT *SWBox1;
- OS_EVENT *LEDBox1;
- struct o {
- int led;
- int nad;
- };
- alt_up_parallel_port_dev *uchwytLEDR;
- alt_up_character_lcd_dev *uchwytLCD;
- alt_alarm alarm;
- int flag;
- int nad;
- int ledg = 0;
- int ledr = 0;
- int pokoje[6] = { 0, 0, 0, 0, 0, 0 };
- void pokoj1(void* pdata) {
- printf("Task1");
- int s2 = 0;
- int s3 = 0;
- int sw = 0;
- INT8U err;
- int czas;
- int flaga_zgas = 0;
- int czas2;
- int flaga_zgas2 = 0;
- while (1) {
- sw = IORD(PARALLEL_PORT_0_BASE, 0);
- OSMboxPostOpt(SWBox1, &sw, OS_POST_OPT_BROADCAST);
- if (!(sw & SW2) && !(sw & SW3)) {
- OSTimeDlyHMSM(0, 0, 0, 10);
- s2 = 0;
- s3 = 0;
- continue;
- }
- INT8U err;
- int kierunek = sw & SW17;
- if (sw & SW2 && s2 == 0) {
- s2 = 1;
- if (kierunek != 0) {
- if (pokoje[2] > 0) {
- pokoje[1]++;
- }
- } else {
- pokoje[1]--;
- }
- }
- if (!(sw & SW2)) {
- s2 = 0;
- }
- if (sw & SW3 && s3 == 0) {
- s3 = 1;
- if (kierunek != 0) {
- pokoje[1]--;
- } else {
- if (pokoje[4] > 0) {
- pokoje[1]++;
- }
- }
- }
- if (!(sw & SW3)) {
- s3 = 0;
- }
- if (pokoje[1] > 0) {
- ledg |= LED1;
- }
- if (pokoje[1] < 0) {
- if (flaga_zgas2 == 0) {
- czas2 = alt_nticks();
- flaga_zgas2 = 1;
- }
- printf("czas %d %d", alt_nticks() - czas2, alt_ticks_per_second());
- if (alt_nticks() - czas2 >= 1 * alt_ticks_per_second()) {
- pokoje[1] = 0;
- ledr |= LED1;
- flaga_zgas2 = 0;
- }
- }
- if (pokoje[1] == 0) {
- if ((ledg && LED1) && (flaga_zgas == 0)) {
- czas = alt_nticks();
- flaga_zgas = 1;
- }
- printf("czas %d %d", alt_nticks() - czas, alt_ticks_per_second());
- if (alt_nticks() - czas >= 3 * alt_ticks_per_second()) {
- ledg = ledg & ~LED1;
- flaga_zgas = 0;
- }
- }
- alt_up_character_lcd_set_cursor_pos(uchwytLCD, 0, 1);
- char liczba[3];
- snprintf(liczba, 3, "%d", pokoje[1]);
- alt_up_character_lcd_string(uchwytLCD, liczba);
- IOWR(PARALLEL_PORT_1_BASE, 0, ledg);
- IOWR(PARALLEL_PORT_2_BASE, 0, ledr);
- OSTimeDlyHMSM(0, 0, 0, 10);
- }
- }
- void pokoj2(void* pdata) {
- printf("Task2");
- int s1 = 0;
- int s2 = 0;
- int s5 = 0;
- int sw = 0;
- int *in;
- INT8U err;
- int czas;
- int flaga_zgas = 0;
- int czas2;
- int flaga_zgas2 = 0;
- while (1) {
- in = OSMboxPend(SWBox1, 0, &err);
- sw = *in;
- if (!(sw & SW1) && !(sw & SW2) && !(sw & SW5)) {
- OSTimeDlyHMSM(0, 0, 0, 10);
- s1 = 0;
- s2 = 0;
- s5 = 0;
- continue;
- }
- INT8U err;
- int kierunek = sw & SW17;
- if (sw & SW1 && s1 == 0) {
- s1 = 1;
- if (kierunek != 0) {
- pokoje[2]++;
- } else {
- pokoje[2]--;
- }
- }
- if (!(sw & SW1)) {
- s1 = 0;
- }
- if (sw & SW2 && s2 == 0) {
- s2 = 1;
- if (kierunek != 0) {
- pokoje[2]--;
- } else {
- if (pokoje[1] > 0) {
- pokoje[2]++;
- }
- }
- }
- if (!(sw & SW2)) {
- s2 = 0;
- }
- if (sw & SW5 && s5 == 0) {
- s5 = 1;
- if (kierunek != 0) {
- pokoje[2]--;
- } else {
- if (pokoje[3] > 0) {
- pokoje[2]++;
- }
- }
- }
- if (!(sw & SW5)) {
- s5 = 0;
- }
- if (pokoje[2] > 0) {
- ledg |= LED2;
- }
- if (pokoje[2] < 0) {
- if (flaga_zgas2 == 0) {
- czas2 = alt_nticks();
- flaga_zgas2 = 1;
- }
- printf("czas %d %d", alt_nticks() - czas2, alt_ticks_per_second());
- if (alt_nticks() - czas2 >= 1 * alt_ticks_per_second()) {
- pokoje[2] = 0;
- ledr |= LED2;
- flaga_zgas2 = 0;
- }
- }
- if (pokoje[2] <= 0) {
- if ((ledg && LED2) && (flaga_zgas == 0)) {
- czas = alt_nticks();
- flaga_zgas = 1;
- }
- printf("czas %d %d", alt_nticks() - czas, alt_ticks_per_second());
- if (alt_nticks() - czas >= 3 * alt_ticks_per_second()) {
- ledg = ledg & ~LED2;
- flaga_zgas = 0;
- }
- }
- printf("osoby: %d", pokoje[2]);
- alt_up_character_lcd_set_cursor_pos(uchwytLCD, 3, 1);
- char liczba[3];
- snprintf(liczba, 3, "%d", pokoje[2]);
- alt_up_character_lcd_string(uchwytLCD, liczba);
- IOWR(PARALLEL_PORT_1_BASE, 0, ledg);
- IOWR(PARALLEL_PORT_2_BASE, 0, ledr);
- OSTimeDlyHMSM(0, 0, 0, 10);
- }
- }
- void pokoj3(void* pdata) {
- printf("Task3");
- int s5 = 0;
- int s6 = 0;
- int sw = 0;
- int *in;
- INT8U err;
- int czas;
- int flaga_zgas = 0;
- int czas2;
- int flaga_zgas2 = 0;
- while (1) {
- in = OSMboxPend(SWBox1, 0, &err);
- sw = *in;
- if (!(sw & SW5) && !(sw & SW6) ) {
- OSTimeDlyHMSM(0, 0, 0, 10);
- s5 = 0;
- s6 = 0;
- continue;
- }
- int kierunek = sw & SW17;
- if (sw & SW5 && s5 == 0) {
- s5 = 1;
- if (kierunek != 0) {
- if(pokoje[2]>0){
- pokoje[3]++;
- }
- } else {
- pokoje[3]--;
- }
- }
- if (!(sw & SW5)) {
- s5 = 0;
- }
- if (sw & SW6 && s6 == 0) {
- s6 = 1;
- if (kierunek != 0) {
- pokoje[3]--;
- } else {
- if(pokoje[5]>0){
- pokoje[3]++;
- }
- }
- }
- if (!(sw & SW6)) {
- s6 = 0;
- }
- if (pokoje[3] > 0) {
- ledg |= LED3;
- }
- if (pokoje[3] < 0) {
- if (flaga_zgas2 == 0) {
- czas2 = alt_nticks();
- flaga_zgas2 = 1;
- }
- printf("czas %d %d", alt_nticks() - czas2, alt_ticks_per_second());
- if (alt_nticks() - czas2 >= 1 * alt_ticks_per_second()) {
- pokoje[3] = 0;
- ledr |= LED3;
- flaga_zgas2 = 0;
- }
- }
- if (pokoje[3] == 0) {
- if ((ledg && LED3) && (flaga_zgas == 0)) {
- czas = alt_nticks();
- flaga_zgas = 1;
- }
- printf("czas %d %d", alt_nticks() - czas, alt_ticks_per_second());
- if (alt_nticks() - czas >= 3 * alt_ticks_per_second()) {
- ledg = ledg & ~LED3;
- flaga_zgas = 0;
- }
- }
- alt_up_character_lcd_set_cursor_pos(uchwytLCD, 6, 1);
- char liczba[3];
- snprintf(liczba, 3, "%d", pokoje[3]);
- alt_up_character_lcd_string(uchwytLCD, liczba);
- IOWR(PARALLEL_PORT_1_BASE, 0, ledg);
- IOWR(PARALLEL_PORT_2_BASE, 0, ledr);
- OSTimeDlyHMSM(0, 0, 0, 10);
- }
- }
- void pokoj4(void* pdata) {
- printf("Task4");
- int s3 = 0;
- int s4 = 0;
- int sw = 0;
- int *in;
- INT8U err;
- int czas;
- int flaga_zgas = 0;
- int czas2;
- int flaga_zgas2 = 0;
- while (1) {
- in = OSMboxPend(SWBox1, 0, &err);
- sw = *in;
- if (!(sw & SW3) && !(sw & SW4) ) {
- OSTimeDlyHMSM(0, 0, 0, 10);
- s3 = 0;
- s4 = 0;
- continue;
- }
- int kierunek = sw & SW17;
- if (sw & SW3 && s3 == 0) {
- s3 = 1;
- if (kierunek != 0) {
- if(pokoje[1]>0){
- pokoje[4]++;
- }
- } else {
- pokoje[4]--;
- }
- }
- if (!(sw & SW3)) {
- s3 = 0;
- }
- if (sw & SW4 && s4 == 0) {
- s4 = 1;
- if (kierunek != 0) {
- pokoje[4]--;
- } else {
- pokoje[4]++;
- }
- }
- if (!(sw & SW4)) {
- s4 = 0;
- }
- if (pokoje[4] > 0) {
- ledg |= LED4;
- }
- if (pokoje[4] < 0) {
- if (flaga_zgas2 == 0) {
- czas2 = alt_nticks();
- flaga_zgas2 = 1;
- }
- printf("czas %d %d", alt_nticks() - czas2, alt_ticks_per_second());
- if (alt_nticks() - czas2 >= 1 * alt_ticks_per_second()) {
- pokoje[4] = 0;
- ledr |= LED4;
- flaga_zgas2 = 0;
- }
- }
- if (pokoje[4] == 0) {
- if ((ledg && LED4) && (flaga_zgas == 0)) {
- czas = alt_nticks();
- flaga_zgas = 1;
- }
- printf("czas %d %d", alt_nticks() - czas, alt_ticks_per_second());
- if (alt_nticks() - czas >= 3 * alt_ticks_per_second()) {
- ledg = ledg & ~LED4;
- flaga_zgas = 0;
- }
- }
- printf("pokoj4: %d", pokoje[4]);
- alt_up_character_lcd_set_cursor_pos(uchwytLCD, 9, 1);
- char liczba[3];
- snprintf(liczba, 3, "%d", pokoje[4]);
- alt_up_character_lcd_string(uchwytLCD, liczba);
- IOWR(PARALLEL_PORT_1_BASE, 0, ledg);
- IOWR(PARALLEL_PORT_2_BASE, 0, ledr);
- OSTimeDlyHMSM(0, 0, 0, 10);
- }
- }
- void pokoj5(void* pdata) {
- printf("Task4");
- int s6 = 0;
- int s7 = 0;
- int sw = 0;
- int *in;
- INT8U err;
- int czas;
- int flaga_zgas = 0;
- int czas2;
- int flaga_zgas2 = 0;
- while (1) {
- in = OSMboxPend(SWBox1, 0, &err);
- sw = *in;
- if (!(sw & SW6) && !(sw & SW7) ) {
- OSTimeDlyHMSM(0, 0, 0, 10);
- s6 = 0;
- s7 = 0;
- continue;
- }
- int kierunek = sw & SW17;
- if (sw & SW6 && s6 == 0) {
- s6 = 1;
- if (kierunek != 0) {
- if(pokoje[3]>0){
- pokoje[5]++;
- }
- } else {
- pokoje[5]--;
- }
- }
- if (!(sw & SW6)) {
- s6 = 0;
- }
- if (sw & SW7 && s7 == 0) {
- s7 = 1;
- if (kierunek != 0) {
- pokoje[5]--;
- } else {
- pokoje[5]++;
- }
- }
- if (!(sw & SW7)) {
- s7 = 0;
- }
- if (pokoje[5] > 0) {
- ledg |= LED5;
- }
- if (pokoje[5] < 0) {
- if (flaga_zgas2 == 0) {
- czas2 = alt_nticks();
- flaga_zgas2 = 1;
- }
- printf("czas %d %d", alt_nticks() - czas2, alt_ticks_per_second());
- if (alt_nticks() - czas2 >= 1 * alt_ticks_per_second()) {
- pokoje[5] = 0;
- ledr |= LED5;
- flaga_zgas2 = 0;
- }
- }
- if (pokoje[5] == 0) {
- if ((ledg && LED5) && (flaga_zgas == 0)) {
- czas = alt_nticks();
- flaga_zgas = 1;
- }
- printf("czas %d %d", alt_nticks() - czas, alt_ticks_per_second());
- if (alt_nticks() - czas >= 3 * alt_ticks_per_second()) {
- ledg = ledg & ~LED5;
- flaga_zgas = 0;
- }
- }
- printf("pokoj4: %d", pokoje[5]);
- alt_up_character_lcd_set_cursor_pos(uchwytLCD, 12, 1);
- char liczba[3];
- snprintf(liczba, 3, "%d", pokoje[5]);
- alt_up_character_lcd_string(uchwytLCD, liczba);
- IOWR(PARALLEL_PORT_1_BASE, 0, ledg);
- IOWR(PARALLEL_PORT_2_BASE, 0, ledr);
- OSTimeDlyHMSM(0, 0, 0, 10);
- }
- }
- /* The main function creates two task and starts multi-tasking */
- int main(void) {
- flag = 1;
- IOWR(PARALLEL_PORT_1_BASE, 0, 0);
- IOWR(PARALLEL_PORT_2_BASE, 0, 0);
- uchwytLCD = alt_up_character_lcd_open_dev("/dev/character_lcd_0");
- alt_up_character_lcd_init(uchwytLCD);
- alt_up_character_lcd_set_cursor_pos(uchwytLCD, 0, 0);
- alt_up_character_lcd_string(uchwytLCD, "P1 P2 P3 P4 P5");
- SWBox1 = OSMboxCreate((void*) 0);
- OSTaskCreateExt(pokoj1, NULL, (void *) &pokoj1_stk[TASK_STACKSIZE - 1], 1,
- 1, pokoj1_stk, TASK_STACKSIZE, NULL, 0);
- OSTaskCreateExt(pokoj2, NULL, (void *) &pokoj2_stk[TASK_STACKSIZE - 1], 2,
- 2, pokoj2_stk, TASK_STACKSIZE, NULL, 0);
- OSTaskCreateExt(pokoj3, NULL, (void *) &pokoj3_stk[TASK_STACKSIZE - 1], 3,
- 3, pokoj3_stk, TASK_STACKSIZE, NULL, 0);
- OSTaskCreateExt(pokoj4, NULL, (void *) &pokoj4_stk[TASK_STACKSIZE - 1], 4,
- 4, pokoj4_stk, TASK_STACKSIZE, NULL, 0);
- OSTaskCreateExt(pokoj5, NULL, (void *) &pokoj5_stk[TASK_STACKSIZE - 1], 5,
- 5, pokoj5_stk, TASK_STACKSIZE, NULL, 0);
- OSStart();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement