Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <system.h>
- #include "includes.h"
- #include <io.h>
- #define SEGA 0x00001
- #define SEGB 0x00002
- #define SEGC 0x00004
- #define SEGD 0x00008
- #define SEGE 0x00010
- #define SEGF 0x00020
- #define SEGG 0x00040
- #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
- // leds
- #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 ZERO SEGA | SEGB | SEGC | SEGD |SEGE | SEGF
- #define ONE SEGB | SEGC
- #define TWO SEGA | SEGB | SEGG | SEGE | SEGD
- #define THREE SEGA | SEGB | SEGC | SEGD | SEGG
- #define FOUR SEGB | SEGC | SEGF | SEGG
- #define FIVE SEGA | SEGC | SEGD | SEGF | SEGG
- #define SIX SEGA | SEGC | SEGD | SEGE | SEGF | SEGG
- #define SEVEN SEGA | SEGB | SEGC | SEGG
- #define EIGHT SEGA | SEGB | SEGC | SEGD | SEGE | SEGF | SEGG
- #define NINE SEGA | SEGB | SEGC | SEGD | SEGF | SEGG
- #define E SEGA | SEGD | SEGE | SEGF | SEGG
- #define R SEGE | SEGG
- #define A SEGB | SEGC
- #define M SEGA | SEGB | SEGC | SEGE | SEGF
- #define F SEGA | SEGB | SEGD | SEGE | SEGG
- int AM = 0;
- int FM = 0;
- int max = 99;
- int state = 0;
- void VGA_text(int, int, char *);
- int zmienZakres(int swstate, int liczba) { // zwraca true, jesli zmieni zakres poprawnie
- if ((swstate & SW0)) { // AM
- if ((swstate & SW6)) { // dodawanie
- if (AM + liczba < max)
- AM += liczba;
- else
- return 0;
- } else if ((swstate & SW7)) { // odejmowanie
- if (AM - liczba > 0)
- AM -= liczba;
- else
- return 0;
- }
- }
- if ((swstate & SW1)) { // FM
- if ((swstate & SW6)) { // dodawanie
- if (FM + liczba < max)
- FM += liczba;
- else
- return 0;
- } else if ((swstate & SW7)) { // odejmowanie
- if (FM - liczba > 0)
- FM -= liczba;
- else
- return 0;
- }
- }
- return 1;
- }
- struct interrupt_data {
- int myLEDS;
- int mySLIDERS;
- int myHEX;
- };
- struct interrupt_data *data;
- /* Definition of Task Stacks */
- #define TASK_STACKSIZE 2048
- OS_STK task1_stk[TASK_STACKSIZE];
- OS_STK task2_stk[TASK_STACKSIZE];
- OS_STK task3_stk[TASK_STACKSIZE];
- OS_STK task4_stk[TASK_STACKSIZE];
- OS_STK task5_stk[TASK_STACKSIZE];
- OS_STK task6_stk[TASK_STACKSIZE];
- OS_STK task7_stk[TASK_STACKSIZE];
- OS_STK task8_stk[TASK_STACKSIZE];
- OS_STK task9_stk[TASK_STACKSIZE];
- OS_EVENT *SWBox0;
- OS_EVENT *SWBox1;
- OS_EVENT *SWBox2;
- OS_EVENT *SWBox3;
- OS_EVENT *SWBox4;
- OS_EVENT *SWBox5;
- OS_EVENT *SWBox6;
- OS_EVENT *SWBox7;
- OS_EVENT *SWBox8;
- OS_EVENT *SWBox9;
- int numer1 = 0;
- int numer2 = 0;
- int numer3 = 0;
- int numer4 = 0;
- alt_alarm error_alarm;
- int errorWorking = 0;
- alt_u32 led_alarm_proc(void *context) {
- //printf("WYWOLUJE ALARM %d", (int)context);
- errorWorking = 0;
- IOWR(data->myLEDS, 0, 0); // gasimy wszystkie diody
- IOWR(data->myLEDS, 0, SW9);
- return 0;
- }
- int time_delay = 2;
- void taskError(void *pdata) {
- while (1) {
- int err;
- OSTimeDlyHMSM(0, 0, 0, 10);
- int *num = OSMboxPend(SWBox1, 0, &err);
- int swstate = *num;
- if (((swstate & SW0) && (swstate & SW1))
- || ((swstate & SW6) && (swstate & SW7))) {
- //IOWR(data->myLEDS, 0, 0); // gasimy wszystkie diody
- //IOWR(data->myLEDS, 0, SW9);
- if (errorWorking == 0) {
- errorWorking = 1;
- alt_alarm_start(&error_alarm, alt_ticks_per_second() * time_delay,
- led_alarm_proc, swstate);
- }
- IOWR(data->myHEX, 0, R);
- IOWR(data->myHEX, 1, R);
- IOWR(data->myHEX, 2, E);
- IOWR(data->myHEX, 3, ZERO);
- IOWR(data->myHEX, 4, ZERO);
- char text1_1_row[40] = "***ERROR***";
- VGA_text(30, 15, text1_1_row);
- }
- }
- }
- void task3(void *pdata) {
- data->myLEDS = LEDS_BASE;
- data->mySLIDERS = SW_SLIDERS_BASE;
- data->myHEX = HEX_BASE;
- while (1) {
- int err;
- OSTimeDlyHMSM(0, 0, 2, 10);
- int *num = OSMboxPend(SWBox1, 0, &err);
- int swstate = *num;
- if (!((swstate & SW0) && (swstate & SW1))
- || ((swstate & SW6) && (swstate & SW7))) {
- if (swstate & SW0) {
- IOWR(NEW_COMPONENT_0_BASE, 0, 0x0001);
- IOWR(data->myLEDS, 0, LED0);
- IOWR(data->myHEX, 0, M);
- IOWR(data->myHEX, 1, A);
- char text1_1_row[40] = "***Grzanie gora***";
- VGA_text(30, 15, text1_1_row);
- }
- if (swstate & SW1) {
- IOWR(data->myLEDS, 0, LED1);
- IOWR(data->myHEX, 0, M);
- IOWR(data->myHEX, 1, F);
- char text1_1_row[40] = "***Grzanie dol***";
- VGA_text(30, 15, text1_1_row);
- }
- }
- }
- }
- void task4(void *pdata) {
- data->myLEDS = LEDS_BASE;
- data->mySLIDERS = SW_SLIDERS_BASE;
- data->myHEX = HEX_BASE;
- while (1) {
- int err;
- int *num = OSMboxPend(SWBox1, 0, &err);
- int swstate = *num;
- if (!((swstate & SW0) && (swstate & SW1))
- || ((swstate & SW6) && (swstate & SW7))) {
- if (swstate & SW0) {
- IOWR(data->myHEX, 1, A);
- char text1_1_row[40] = "***Grzenie gora***";
- VGA_text(30, 15, text1_1_row);
- }
- if (swstate & SW1) {
- IOWR(data->myHEX, 1, F);
- char text1_1_row[40] = "***Grzanie dol***";
- VGA_text(30, 15, text1_1_row);
- }
- if ((swstate & SW2)) {
- char text1_1_row[40] = "Dodano 1";
- VGA_text(45, 15, text1_1_row);
- if (swstate & SW0) {
- if (swstate & SW6)
- IOWR(data->myLEDS, 0, LED0 | LED2 | LED6);
- if (swstate & SW7)
- IOWR(data->myLEDS, 0, LED0 | LED2 | LED7);
- } else {
- if (swstate & SW6)
- IOWR(data->myLEDS, 0, LED1 | LED2 | LED6);
- if (swstate & SW7)
- IOWR(data->myLEDS, 0, LED1 | LED2 | LED7);
- }}
- else {
- if (swstate & SW0) {
- if (swstate & SW6)
- IOWR(data->myLEDS, 0, LED0 | LED6);
- if (swstate & SW7)
- IOWR(data->myLEDS, 0, LED0 | LED7);
- } else {
- if (swstate & SW6)
- IOWR(data->myLEDS, 0, LED1 | LED6);
- if (swstate & SW7)
- IOWR(data->myLEDS, 0, LED1 | LED7);
- }
- }
- }
- }
- }
- void task5(void *pdata) {
- data->myLEDS = LEDS_BASE;
- data->mySLIDERS = SW_SLIDERS_BASE;
- data->myHEX = HEX_BASE;
- while (1) {
- int err;
- int *num = OSMboxPend(SWBox1, 0, &err);
- int swstate = *num;
- if (swstate & SW8) {
- if (swstate & SW0) {
- IOWR(data->myHEX, 1, A);
- }
- if (swstate & SW1) {
- IOWR(data->myHEX, 0, M);
- IOWR(data->myHEX, 1, F);
- }
- if ((swstate & SW2)) {
- IOWR(data->myHEX, 3, ONE);
- IOWR(data->myHEX, 4, ZERO);
- if (swstate & SW0) {
- numer1 = AM;
- IOWR(data->myLEDS, 0, LED0 | LED2 | LED8);
- } else {
- numer1 = FM;
- IOWR(data->myLEDS, 0, LED1 | LED2 | LED8);
- }
- }
- if ((swstate & SW3)) {
- IOWR(data->myHEX, 3, TWO);
- IOWR(data->myHEX, 4, ZERO);
- if (swstate & SW0) {
- numer2 = AM;
- IOWR(data->myLEDS, 0, LED0 | LED3 | LED8);
- } else {
- numer2 = FM;
- IOWR(data->myLEDS, 0, LED1 | LED3 | LED8);
- }
- }
- if ((swstate & SW4)) {
- IOWR(data->myHEX, 3, THREE);
- IOWR(data->myHEX, 4, ZERO);
- if (swstate & SW0) {
- numer3 = AM;
- IOWR(data->myLEDS, 0, LED0 | LED4 | LED8);
- } else {
- numer3 = FM;
- IOWR(data->myLEDS, 0, LED1 | LED4 | LED8);
- }
- }
- if ((swstate & SW5)) {
- IOWR(data->myHEX, 3, FOUR);
- IOWR(data->myHEX, 4, ZERO);
- if (swstate & SW0) {
- numer4 = AM;
- IOWR(data->myLEDS, 0, LED0 | LED5 | LED8);
- } else {
- numer4 = FM;
- IOWR(data->myLEDS, 0, LED1 | LED5 | LED8);
- }
- }
- }
- }
- }
- void task6(void *pdata) {
- }
- void task7(void *pdata) {
- }
- void task8(void *pdata) {
- }
- void task9(void *pdata) {
- }
- /* Definition of Task Priorities */
- #define TASK1_PRIORITY 2
- #define TASK2_PRIORITY 3
- #define TASK3_PRIORITY 11
- #define TASK4_PRIORITY 4
- #define TASK5_PRIORITY 5
- #define TASK6_PRIORITY 6
- #define TASK7_PRIORITY 7
- #define TASK8_PRIORITY 8
- #define TASK9_PRIORITY 9
- //alt_up_parallel_port_dev const *uchwytLEDR; = alt_up_parallel_port_open_dev("/dev/parallel_port_2");
- void clearScreen() {
- char text1_1_row[40] =
- " ";
- VGA_text(30, 15, text1_1_row);
- }
- int startTicksTime = 0;
- int startTicksTime2 = 0;
- void task2(void* pdata) {
- int oldState = -1;
- data->myLEDS = LEDS_BASE;
- data->mySLIDERS = SW_SLIDERS_BASE;
- data->myHEX = HEX_BASE;
- int oldValue = -1;
- int swstate = 0;
- int *num;
- do {
- swstate = IORD(SW_SLIDERS_BASE, 0);
- do {
- switch (state) {
- case 0: // dla nieustalonego stanu
- if (((swstate & SW0) && (swstate & SW1))
- || ((swstate & SW6) && (swstate & SW7))) {
- state = 1;
- } else if (((swstate & SW0) || (swstate & SW1))
- && ((swstate & SW6) || (swstate & SW7))) {
- state = 9;
- } else if (((swstate & SW0) || (swstate & SW1))
- && (swstate & SW8)) {
- state = 8;
- } else if ((swstate & SW0) || (swstate & SW1)) {
- state = 2;
- } else {
- IOWR(NEW_COMPONENT_0_BASE, 0, 0x0000);
- clearScreen();
- IOWR(data->myLEDS, 0, 0);
- for (int i = 0; i <= 5; i++) {
- IOWR(data->myHEX, i, 0);
- }
- }
- break;
- case 1: // ERROR
- IOWR(NEW_COMPONENT_0_BASE, 0, 3);
- OSMboxPostOpt(SWBox1, &swstate, OS_POST_OPT_BROADCAST);
- OSTimeDlyHMSM(0, 0, 0, 10);
- state = 0;
- break;
- case 2:
- OSTimeDlyHMSM(0, 0, 1, 0);
- OSMboxPostOpt(SWBox1, &swstate, OS_POST_OPT_BROADCAST);
- state = 0;
- break;
- case 8:
- if (startTicksTime2 == 0) {
- startTicksTime2 = alt_nticks();
- } else if (((alt_nticks() - startTicksTime2)
- > (2 * alt_ticks_per_second()))) {
- OSMboxPostOpt(SWBox1, &swstate, OS_POST_OPT_BROADCAST);
- OSTimeDlyHMSM(0, 0, 0, 10);
- startTicksTime2 = 0;
- }
- break;
- case 9:
- if (startTicksTime == 0) {
- startTicksTime = alt_nticks();
- } else if (((alt_nticks() - startTicksTime)
- > (2 * alt_ticks_per_second()))) {
- OSMboxPostOpt(SWBox1, &swstate, OS_POST_OPT_BROADCAST);
- OSTimeDlyHMSM(0, 0, 0, 10);
- startTicksTime = 0;
- }
- state = 0;
- break;
- }
- } while (state != 0);
- oldValue = swstate;
- } while (1);
- //oldValue = swstate;
- }
- /* Prints "Hello World" and sleeps for three seconds */
- void task1(void* pdata) {
- while (1) {
- int sw;
- int * msg;
- sw = IORD(SW_SLIDERS_BASE, 0);
- printf("%d", sw);
- //OSMboxPostOpt(SWBox0, &sw, OS_POST_OPT_BROADCAST);
- OSTimeDlyHMSM(0, 0, 0, 10);
- }
- }
- /* Prints "Hello World" and sleeps for three seconds */
- /* The main function creates two task and starts multi-tasking */
- int main(void) {
- /* create a message to be displayed on the VGA display */
- /* output text message near the middle of the VGA monitor */
- SWBox1 = OSMboxCreate((void*) 0); // error
- OSTaskCreateExt(task1,
- NULL, (void *) &task1_stk[TASK_STACKSIZE - 1],
- TASK1_PRIORITY,
- TASK1_PRIORITY, task1_stk,
- TASK_STACKSIZE,
- NULL, 0);
- OSTaskCreateExt(task2,
- NULL, (void *) &task2_stk[TASK_STACKSIZE - 1],
- TASK2_PRIORITY,
- TASK2_PRIORITY, task2_stk,
- TASK_STACKSIZE,
- NULL, 0);
- OSTaskCreateExt(taskError,
- NULL, (void *) &task3_stk[TASK_STACKSIZE - 1],
- TASK3_PRIORITY,
- TASK3_PRIORITY, task3_stk,
- TASK_STACKSIZE,
- NULL, 0);
- OSTaskCreateExt(task3,
- NULL, (void *) &task4_stk[TASK_STACKSIZE - 1],
- TASK4_PRIORITY,
- TASK4_PRIORITY, task4_stk,
- TASK_STACKSIZE,
- NULL, 0);
- OSTaskCreateExt(task4,
- NULL, (void *) &task5_stk[TASK_STACKSIZE - 1],
- TASK5_PRIORITY,
- TASK5_PRIORITY, task5_stk,
- TASK_STACKSIZE,
- NULL, 0);
- OSTaskCreateExt(task5,
- NULL, (void *) &task6_stk[TASK_STACKSIZE - 1],
- TASK6_PRIORITY,
- TASK6_PRIORITY, task6_stk,
- TASK_STACKSIZE,
- NULL, 0);
- OSStart();
- return 0;
- }
- void VGA_text(int x, int y, char * text_ptr) {
- int offset;
- /* Declare volatile pointer to char buffer (volatile means that IO load
- and store instructions will be used to access these pointer locations,
- instead of regular memory loads and stores) */
- volatile char * character_buffer = (char *) 0x80000; // VGA character buffer
- /* assume that the text string fits on one line */
- offset = (y << 7) + x;
- while (*(text_ptr)) {
- *(character_buffer + offset) = *(text_ptr); // write to the character buffer
- ++text_ptr;
- ++offset;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement