Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "nnxt.h"
- #define q0 0
- #define q1 1
- #define q2 2
- #define q3 3
- #define q4 4
- #define q5 5
- #define q6 6
- #define q7 7
- #define q8 8
- #define q9 9
- #define q10 10
- #define q11 11
- #define q12 12
- #define tasterR 13
- #define tasterL 14
- #define goback 15
- volatile uint32_t events = 0;
- volatile tasterL = 0;
- volatile tasterR = 0;
- //Events
- void SetEvent (uint32_t shift)
- {
- taskENTER_CRITICAL();
- uint32_t maske = 0x0001;
- maske = maske << shift;
- events = events | maske;
- taskEXIT_CRITICAL();
- }
- int EventIsSet (uint32_t shift)
- {
- taskENTER_CRITICAL();
- uint32_t tempevents = 0;
- uint32_t maske = 0x00001;
- maske <<= shift;
- tempevents = events;
- tempevents &= maske;
- tempevents >>= shift;
- taskEXIT_CRITICAL();
- if (tempevents==1)
- return 1;
- else
- return 0;
- }
- void ClearEvent(uint32_t shift)
- {
- taskENTER_CRITICAL();
- uint32_t maske = 0x0001;
- maske <<= shift;
- maske ^= 0xFFFF;
- events &= maske;
- taskEXIT_CRITICAL();
- }
- //Timer
- volatile uint16_t timeamount;
- volatile uint16_t event_timer;
- volatile uint32_t timerstart;
- volatile uint16_t customsystime;
- void SetTimer (int time, uint32_t event){
- taskENTER_CRITICAL();
- timeamount = time;
- event_timer = event;
- timerstart = 0;
- taskEXIT_CRITICAL();
- }
- void StartTimer(){
- taskENTER_CRITICAL();
- timerstart = 1;
- customsystime = GetSysTime();
- taskEXIT_CRITICAL();
- }
- void CancelTimer()
- {
- taskENTER_CRITICAL();
- timerstart = 0;
- taskEXIT_CRITICAL();
- }
- void TimerTask(){
- while(1){
- if(timerstart == 1)
- {
- if(customsystime+timeamount <= GetSysTime())
- {
- CancelTimer();
- SetEvent(event_timer);
- }
- }
- }
- Delay(10);
- }
- //Funktions
- void driving(){
- motorDir = Motor_dir_forward;
- Motor_Drive(Port_A,motorDir,35);
- Motor_Drive(Port_B,motorDir,35);
- Delay(1000);
- }
- void rotateL(){
- Motor_Drive(Port_A,Motor_drive_forward,35);
- Motor_Drive(Port_B,Motor_drive_backward,35);
- Delay(330);
- }
- void rotateR(){
- Motor_Drive(Port_A,Motor_drive_backward,35);
- Motor_Drive(Port_B,Motor_drive_forward,35);
- Delay(330);
- }
- //Tasks
- void task1()
- {
- SetEvent(q0);
- while(1)
- {
- if(EventIsSet(tasterR) && EventIsSet(q0)){
- ClearEvent(q0);
- ClearEvent(tasterR);
- rotateR();
- driving();
- SetTimer(5000, goback);
- StartTimer();
- SetEvent(q10);
- }
- if(EventisSet(tasterL) && EventIsSet(q0)){
- ClearEvent(q0);
- ClearEvent(tasterL);
- rotateL();
- driving();
- SetTimer(5000, goback);
- StartTimer();
- SetEvent(q3);
- }
- if(tasterR && EventIsSet(q1)){
- CancelTimer();
- ClearEvent(q1);
- ClearEvent(tasterR);
- rotateR();
- rotateR();
- driving();
- SetTimer(5000, goback);
- StartTimer();
- SetEvent(q7);
- }
- if(EventIsSet(tasterL) && EventIsSet(q1)){
- CancelTimer();
- ClearEvent(q1);
- ClearEvent(tasterL);
- SetTimer(5000, goback);
- StartTimer();
- SetEvent(q1);
- }
- if(EventIsSet(goback) && EventIsSet(q1)){
- CancelTimer();
- ClearEvent(q1);
- ClearEvent(goback);
- rotateL();
- rotateL();
- driving();
- driving();
- driving();
- rotateL();
- SetEvent(q0);
- }
- if(tasterR && EventIsSet(q2)){
- CancelTimer();
- ClearEvent(q2);
- ClearEvent(tasterR);
- rotateR();
- rotateR();
- driving();
- SetTimer(5000, goback);
- StartTimer();
- SetEvent(q8);
- }
- if(EventIsSet(tasterL) && EventIsSet(q2)){
- CancelTimer();
- ClearEvent(q2);
- ClearEvent(tasterL);
- driving();
- SetTimer(5000, goback);
- StartTimer();
- SetEvent(q1);
- }
- if(EventIsSet(goback) && EventIsSet(q2)){
- CancelTimer();
- ClearEvent(q2);
- ClearEvent(goback);
- rotateL();
- rotateL();
- driving();
- driving();
- rotateL();
- SetEvent(q0);
- }
- if(tasterR && EventIsSet(q3)){
- CancelTimer();
- ClearEvent(q3);
- ClearEvent(tasterR);
- rotateR();
- rotateR();
- driving();
- SetTimer(5000, goback);
- StartTimer();
- SetEvent(q9);
- }
- if(EventIsSet(tasterL) && EventIsSet(q3)){
- CancelTimer();
- ClearEvent(q3);
- ClearEvent(tasterL);
- driving();
- SetTimer(5000, goback);
- StartTimer();
- SetEvent(q2);
- }
- if(EventIsSet(goback) && EventIsSet(q3)){
- CancelTimer();
- ClearEvent(q3);
- ClearEvent(goback);
- rotateL();
- rotateL();
- driving();
- rotateL();
- SetEvent(q0);
- }
- if(tasterR && EventIsSet(q4)){
- CancelTimer();
- ClearEvent(q4);
- ClearEvent(tasterR);
- rotateR();
- rotateR();
- driving();
- SetTimer(5000, goback);
- StartTimer();
- SetEvent(q10);
- }
- if(EventIsSet(tasterL) && EventIsSet(q4)){
- CancelTimer();
- ClearEvent(q4);
- ClearEvent(tasterL);
- driving();
- SetTimer(5000, goback);
- StartTimer();
- SetEvent(q3);
- }
- if(EventIsSet(goback) && EventIsSet(q4)){
- CancelTimer();
- ClearEvent(q1);
- ClearEvent(goback);
- rotateR();
- SetEvent(q0);
- }
- if(tasterR && EventIsSet(q5)){
- CancelTimer();
- ClearEvent(q5);
- ClearEvent(tasterR);
- rotateR();
- rotateR();
- driving();
- SetTimer(5000, goback);
- StartTimer();
- SetEvent(q11);
- }
- if(EventIsSet(tasterL) && EventIsSet(q5)){
- CancelTimer();
- ClearEvent(q5);
- ClearEvent(tasterL);
- driving();
- SetTimer(5000, goback);
- StartTimer();
- SetEvent(q4);
- }
- if(EventIsSet(goback) && EventIsSet(q5)){
- CancelTimer();
- ClearEvent(q1);
- ClearEvent(goback);
- driving();
- rotateR();
- SetEvent(q0);
- }
- if(tasterR && EventIsSet(q6)){
- CancelTimer();
- ClearEvent(q6);
- ClearEvent(tasterR);
- rotateR();
- rotateR();
- driving();
- SetTimer(5000, goback);
- StartTimer();
- SetEvent(q12);
- }
- if(EventIsSet(tasterL) && EventIsSet(q6)){
- CancelTimer();
- ClearEvent(q6);
- ClearEvent(tasterL);
- driving();
- SetTimer(5000, goback);
- StartTimer();
- SetEvent(q5);
- }
- if(EventIsSet(goback) && EventIsSet(q6)){
- CancelTimer();
- ClearEvent(q1);
- ClearEvent(goback);
- driving();
- driving();
- rotateR();
- SetEvent(q0);
- }
- if(tasterR && EventIsSet(q7)){
- CancelTimer();
- ClearEvent(q7);
- ClearEvent(tasterR);
- driving();
- SetTimer(5000, goback);
- StartTimer();
- SetEvent(q8);
- }
- if(EventIsSet(tasterL) && EventIsSet(q7)){
- CancelTimer();
- ClearEvent(q7);
- ClearEvent(tasterL);
- rotateL();
- rotateL();
- driving();
- SetTimer(5000, goback);
- StartTimer();
- SetEvent(q1);
- }
- if(EventIsSet(goback) && EventIsSet(q7)){
- CancelTimer();
- ClearEvent(q7);
- ClearEvent(goback);
- driving();
- driving();
- rotateL();
- SetEvent(q0);
- }
- if(tasterR && EventIsSet(q8)){
- CancelTimer();
- ClearEvent(q8);
- ClearEvent(tasterR);
- driving();
- SetTimer(5000, goback);
- StartTimer();
- SetEvent(q9);
- }
- if(EventIsSet(tasterL) && EventIsSet(q8)){
- CancelTimer();
- ClearEvent(q8);
- ClearEvent(tasterL);
- rotateL();
- rotateL();
- driving();
- SetTimer(5000, goback);
- StartTimer();
- SetEvent(q2);
- }
- if(EventIsSet(goback) && EventIsSet(q8)){
- CancelTimer();
- ClearEvent(q8);
- ClearEvent(goback);
- driving();
- rotateL();
- SetEvent(q0);
- }
- if(tasterR && EventIsSet(q9)){
- CancelTimer();
- ClearEvent(q9);
- ClearEvent(tasterR);
- driving();
- SetTimer(5000, goback);
- StartTimer();
- SetEvent(q10);
- }
- if(EventIsSet(tasterL) && EventIsSet(q9)){
- CancelTimer();
- ClearEvent(q9);
- ClearEvent(tasterL);
- rotateL();
- rotateL();
- driving();
- SetTimer(5000, goback);
- StartTimer();
- SetEvent(q3);
- }
- if(EventIsSet(goback) && EventIsSet(q9)){
- CancelTimer();
- ClearEvent(q9);
- ClearEvent(goback);
- rotateL();
- SetEvent(q0);
- }
- if(tasterR && EventIsSet(q10)){
- CancelTimer();
- ClearEvent(q10);
- ClearEvent(tasterR);
- driving();
- SetTimer(5000, goback);
- StartTimer();
- SetEvent(q11);
- }
- if(EventIsSet(tasterL) && EventIsSet(q10)){
- CancelTimer();
- ClearEvent(q10);
- ClearEvent(tasterL);
- rotateL();
- rotateL();
- driving();
- SetTimer(5000, goback);
- StartTimer();
- SetEvent(q4);
- }
- if(EventIsSet(goback) && EventIsSet(q10)){
- CancelTimer();
- ClearEvent(q7);
- ClearEvent(goback);
- rotateR();
- rotateR();
- driving();
- rotateR();
- SetEvent(q0);
- }
- if(tasterR && EventIsSet(q11)){
- CancelTimer();
- ClearEvent(q11);
- ClearEvent(tasterR);
- driving();
- SetTimer(5000, goback);
- StartTimer();
- SetEvent(q12);
- }
- if(EventIsSet(tasterL) && EventIsSet(q11)){
- CancelTimer();
- ClearEvent(q11);
- ClearEvent(tasterL);
- rotateL();
- rotateL();
- driving();
- SetTimer(5000, goback);
- StartTimer();
- SetEvent(q5);
- }
- if(EventIsSet(goback) && EventIsSet(q11)){
- CancelTimer();
- ClearEvent(q11);
- ClearEvent(goback);
- rotateR();
- rotateR();
- driving();
- driving();
- rotateR();
- SetEvent(q0);
- }
- if(tasterR && EventIsSet(q12)){
- CancelTimer();
- ClearEvent(q12);
- ClearEvent(tasterR);
- SetTimer(5000, goback);
- StartTimer();
- SetEvent(q12);
- }
- if(EventIsSet(tasterL) && EventIsSet(q12)){
- CancelTimer();
- ClearEvent(q12);
- ClearEvent(tasterL);
- rotateL();
- rotateL();
- driving();
- SetTimer(5000, goback);
- StartTimer();
- SetEvent(q6);
- }
- if(EventIsSet(goback) && EventIsSet(q12)){
- CancelTimer();
- ClearEvent(q12);
- ClearEvent(goback);
- rotateR();
- rotateR();
- driving();
- driving();
- driving();
- rotateR();
- SetEvent(q0);
- }
- }
- }
- void task2()
- {
- uint8_t flankrecognitionR = 0;
- sensor_touch_clicked_t touch1;
- SensorConfig(Port_0, SensorTouch);
- while(1)
- {
- Touch_Clicked(Port_0, &touch1);
- if(touch1 == SensorTouch_clicked || flankrecognitionR == 1)
- {
- flankrecognitionR = 1;
- Touch_Clicked(Port_0, &touch1);
- if(touch1 == SensorTouch_released)
- {
- flankrecognitionR = 0;
- setEvent(tasterR);
- }
- }
- Delay(100);
- }
- }
- void task3()
- {
- uint8_t flankrecognitionL = 0;
- sensor_touch_clicked_t touch2;
- SensorConfig(Port_1, SensorTouch);
- while(1)
- {
- Touch_Clicked(Port_1, &touch2);
- if(touch2 == SensorTouch_clicked || flankrecognitionL == 1)
- {
- flankrecognitionL = 1;
- Touch_Clicked(Port_1, &touch2);
- if(touch2 == SensorTouch_released)
- {
- flankrecognitionL = 0;
- SetEvent(tasterL);
- }
- }
- Delay(100);
- }
- }
- int main()
- {
- CreateAndStartTask(task1);
- CreateAndStartTask(task2);
- CreateAndStartTask(task3);
- CreateAndStartTask(TimerTask);
- StartScheduler();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement