void main(void) { unsigned char difficulty = 0; unsigned char input = 0; P1_4 = 1; /* init hardware */ fb_init(); keyboard_init(); EA = 1; // enable global interrupts /* show our startdisplay */ display_start(difficulty); while(1) { input = readBuf(); /* change difficulty which results in changing speed */ if(input == SHIFT_KEY) { difficulty++; if(difficulty > 2) { difficulty = 0; } display_start(difficulty); } /* start game with space */ if(input == SPACE_KEY) { clearDisplay(); start_game(15000 - difficulty*5000); display_start(difficulty); } } }
int display_sockaddr(struct display_buffer *dspbuf, sockaddr_t *addr) { const char *name = NULL; char ipname[SOCK_NAME_MAX]; int port; int b_left = display_start(dspbuf); if (b_left <= 0) return b_left; switch (addr->ss_family) { case AF_INET: name = inet_ntop(addr->ss_family, &(((struct sockaddr_in *)addr)->sin_addr), ipname, sizeof(ipname)); port = ntohs(((struct sockaddr_in *)addr)->sin_port); break; case AF_INET6: name = inet_ntop(addr->ss_family, &(((struct sockaddr_in6 *)addr)->sin6_addr), ipname, sizeof(ipname)); port = ntohs(((struct sockaddr_in6 *)addr)->sin6_port); break; case AF_LOCAL: return display_cat(dspbuf, ((struct sockaddr_un *)addr)->sun_path); } if (name == NULL) return display_cat(dspbuf, "<unknown>"); else return display_printf(dspbuf, "%s:%d", name, port); }
int main(int argc, char *argv[]) { key_t shmkey; int shmid, shmsize; int i, j, k, t = 0, c = 0; int pause = PAUSE, again = 1; int setupIndex = -1; int minSampleIndex = -1, minMessageIndex = -1; int maxSampleIndex = -1, maxMessageIndex = -1; int minSampleValue = INT_MAX, minMessageValue = INT_MAX; int maxSampleValue = INT_MIN, maxMessageValue = INT_MIN; int maxCountCleared = 0, countCleared = 0, countLoop = 0; int countInvalid = 0, countData = 0, countSetup = 0, countCancel = 0, countUnknown = 0; for (i=0; i<MAXNUMTRIGCHAN; i++) lastValue[i] = 0; /* make the shared memory key * if ((shmkey = ftok(ACQ_MSGQ_SHMPATH, ACQ_MSGQ_SHMPROJID)) == -1) { * perror("ftok"); * exit(1); * } */ /* use the pre-defined shared memory key */ shmkey = ACQ_MSGQ_SHMKEY; /* determine the size of the shared memory buffer */ shmsize = sizeof(ACQ_MessagePacketType) * ACQ_MSGQ_SIZE; /* connect to (and possibly create) the segment */ if ((shmid = shmget(shmkey, shmsize, PERMISSION | IPC_CREAT )) == -1) { perror("shmget"); exit(1); } /* attach to the segment to get a pointer to it */ packet = shmat(shmid, (void *)0, 0); if ((char *)packet == (char *)(-1)) { perror("shmat"); exit(1); } display_start(); display_print("initializing\n"); // clear_all_except_setup(); // clear_all(); display_refresh(); while (again) { clear(); setupIndex = -1; minSampleIndex = -1, minMessageIndex = -1; maxSampleIndex = -1, maxMessageIndex = -1; minSampleValue = INT_MAX, minMessageValue = INT_MAX; maxSampleValue = INT_MIN, maxMessageValue = INT_MIN; countInvalid = 0, countData = 0, countSetup = 0, countCancel = 0, countUnknown = 0; /******************************************************************************* * collect information about all packets in the buffer *******************************************************************************/ for (i=0; i<ACQ_MSGQ_SIZE; i++) { if (i<SHOWPACKET) print_packet(i); switch (packet[i].message_type) { case ACQ_MSGQ_SETUP_COLLECTION: if (countSetup>0) { /* multiple packets with a setup are not allowed, only keep the first */ display_print("clearing superfluous setup packet at %d (setup @ %d)\n", i, setupIndex); clear_packet(i); countCleared++; break; } countSetup++; setupIndex = i; /* update the specifications that relate to trigger maintenance */ numRealChan = packet[i].data[DATASIZE-TRIGSIZE*3-MAXNUMTRIGCHAN-2]; /* update the value */ numTrigChan = packet[i].data[DATASIZE-TRIGSIZE*3-MAXNUMTRIGCHAN-1]; /* update the value */ trigChan = packet[i].data+DATASIZE-TRIGSIZE*3-MAXNUMTRIGCHAN; /* update the pointer */ trigPointer = packet[i].data+DATASIZE-TRIGSIZE*3; /* update the pointer */ if (numRealChan<0) numRealChan = 0; if (numTrigChan<0) numTrigChan = 0; if (numTrigChan>MAXNUMTRIGCHAN) { display_print("cannot maintain more than %d trigger channels in real time\n", MAXNUMTRIGCHAN); numTrigChan = MAXNUMTRIGCHAN; } break; case ACQ_MSGQ_DATA: countData++; if (packet[i].sampleNumber<minSampleValue) { minSampleIndex = i; minSampleValue = packet[i].sampleNumber; } if (packet[i].sampleNumber>maxSampleValue) { maxSampleIndex = i; maxSampleValue = packet[i].sampleNumber; } if (packet[i].messageId<minMessageValue) { minMessageIndex = i; minMessageValue = packet[i].messageId; } if (packet[i].messageId>maxMessageValue) { maxMessageIndex = i; maxMessageValue = packet[i].messageId; } if ((packet[i].sampleNumber/ packet[i].numSamples)>lastPacket) { /* detect the flanks in the trigger channels */ lastPacket = packet[i].sampleNumber/ packet[i].numSamples; for (j=0; j<numTrigChan; j++) { if (trigChan[j]>-1 && trigChan[j]<numRealChan) for (k=0; k<packet[i].numSamples; k++) { int sample = k*numRealChan + trigChan[j]; /* detect changes in the value of the trigger channel */ if (packet[i].data[sample]!=lastValue[j]) { lastValue[j] = packet[i].data[sample]; if (lastValue[j]) { display_print("trigger detected\n"); /* only store it if it is an upgoing flank */ trigPointer[numTrigger+0] = trigChan[j]; /* channel number */ trigPointer[numTrigger+1] = packet[i].sampleNumber+k; /* sample number */ trigPointer[numTrigger+2] = lastValue[j]; /* sample value */ numTrigger = wrapnumtrigger(numTrigger+3); } } } } } break; case ACQ_MSGQ_CLOSE_CONNECTION: countCancel++; break; case ACQ_MSGQ_INVALID: countInvalid++; break; default: countUnknown++; clear_packet(i); countCleared++; break; } /* end switch */ } /* end for */ /******************************************************************************* * print information about all packets in the buffer *******************************************************************************/ display_print("\n"); display_print("buffer size = %d\n", ACQ_MSGQ_SIZE); display_print("shm size = %d\n", shmsize); display_print("shm key = %#x\n", shmkey); display_print("pause = %d\n", pause); if (setupIndex>=0) display_print("dataset = %s @ %d\n", (char *)packet[setupIndex].data, setupIndex); else display_print("dataset = <unknown>\n"); display_print("\n"); display_print("countSetup = %d\n", countSetup); display_print("countData = %d\n", countData); display_print("countCancel = %d\n", countCancel); display_print("countInvalid = %d\n", countInvalid); display_print("countUnknown = %d\n", countUnknown); display_print("countCleared = %d\n", countCleared); display_print("\n"); /* this might look like a weird location to reinitialize */ maxCountCleared = (countCleared>maxCountCleared ? countCleared : maxCountCleared); countCleared = 0; display_print("min(sampleNumber) = %d @ %d\n", minSampleValue, minSampleIndex); display_print("max(sampleNumber) = %d @ %d\n", maxSampleValue, maxSampleIndex); // display_print("min(messageId) = %d @ %d\n", minMessageValue, minMessageIndex); // display_print("max(messageId) = %d @ %d\n", maxMessageValue, maxMessageIndex); display_print("max(countCleared) = %d\n", maxCountCleared); display_print("\n"); if (maxSampleIndex>=0) display_print("current trial = %d @ %d\n", maxSampleValue/packet[maxSampleIndex].numSamples, maxSampleIndex); else display_print("current trial = <unknown>\n"); display_print("\n"); display_print("numRealChan = %d\n", numRealChan); display_print("numTrigChan = %d\n", numTrigChan); display_print("numTrigger = %d\n", numTrigger/3); display_print("lastPacket = %d\n", lastPacket); display_print("trigChan = "); for (i=0; i<numTrigChan; i++) display_print("%d\t", trigChan[i]); display_print("\n"); display_print("lastValue = "); for (i=0; i<numTrigChan; i++) display_print("%d\t", lastValue[i]); display_print("\n"); display_print("\n"); display_refresh(); /******************************************************************************* * do the desired maintenance on the packet buffer (only if a key was pressed) *******************************************************************************/ k = getch(); switch (k) { case 's': write_setup(0); break; case 'p': pause = PAUSE; break; case 'f': while (countInvalid && packet[t].message_type!=ACQ_MSGQ_INVALID) t = wrapnumpacket(t+1); while (packet[t].message_type==ACQ_MSGQ_INVALID) { write_data(t, c); t = wrapnumpacket(t+1); c++; } break; case 'd': while (countInvalid && packet[t].message_type!=ACQ_MSGQ_INVALID) t = wrapnumpacket(t+1); write_data(t, c); t = wrapnumpacket(t+1); c++; break; case 'c': maxCountCleared = 0; clear_all_except_setup(); break; case 'h': showDisplay = (!showDisplay); break; case 'x': maxCountCleared = 0; clear_all(); break; case 'q': again = 0; break; } if (k!=ERR) { display_print("key pressed = %d\n", k); display_refresh(); continue; } /******************************************************************************* * do the regular maintenance on the packet buffer *******************************************************************************/ if (countCancel) { display_print("initializing all packets\n"); clear_all(); countInvalid = ACQ_MSGQ_SIZE; countData = 0; countSetup = 0; countCancel = 0; } // if ((MINFREE-countInvalid)>1 && pause) { // pause/=(MINFREE-countInvalid); // display_print("decreasing pause to %d\n", pause); // } while (countInvalid<MINFREE && countData) /* make more empty packets available */ if (setupIndex>-1) { /* moving the setup one packet at a time may look inefficient, but this only happens at the start of online acquisition */ display_print("moving setup from %d to %d\n", setupIndex, minSampleIndex); memcpy(&packet[minSampleIndex], &packet[setupIndex], sizeof(ACQ_MessagePacketType)); countCleared++; countInvalid++; countData--; // NOTE: don't clear the packet, since Matlab might be reading from it // display_print("clearing packet %d\n", setupIndex); // clear_packet(setupIndex); packet[setupIndex].message_type = ACQ_MSGQ_INVALID; setupIndex = minSampleIndex; minSampleIndex = wrapnumpacket(minSampleIndex+1); /* the next is probably now the oldest */ } else { display_print("clearing packet %d\n", minSampleIndex); clear_packet(minSampleIndex); countCleared++; countInvalid++; countData--; minSampleIndex = wrapnumpacket(minSampleIndex+1); /* the next is probably now the oldest */ } while (setupIndex>-1 && countData && packet[wrapnumpacket(setupIndex+1)].message_type==ACQ_MSGQ_INVALID) { /* move the setup to the next empty packet */ /* moving the setup one packet at a time may look inefficient, but this only happens at the start of online acquisition */ display_print("moving setup from %d to %d\n", setupIndex, wrapnumpacket(setupIndex+1)); memcpy(&packet[wrapnumpacket(setupIndex+1)], &packet[setupIndex], sizeof(ACQ_MessagePacketType)); // NOTE: don't clear the packet, since Matlab might be reading from it // clear_packet(setupIndex); packet[setupIndex].message_type = ACQ_MSGQ_INVALID; countCleared++; setupIndex = wrapnumpacket(setupIndex+1); } display_refresh(); usleep(pause); if (countInvalid>4) printf("ok %d\n", countLoop); else printf("error %d\n", countLoop); countLoop++; } /* detach from the shared memory segment */ if (shmdt(packet) == -1) { perror("shmdt"); exit(1); } /* end curses mode */ display_stop(); /* end of program */ return 0; }
/* * EXPORTED FUNCTION DEFINITIONS **************************************************************************************** */ void app_display_init(void) { /* ***********************************************************************/ /* Screen Allocation */ /* ***********************************************************************/ //Level 1 uint8_t s_prof = display_screen_alloc(); //Profiles uint8_t s_app_mgmt = display_screen_alloc(); //Application Management uint8_t s_info = display_screen_alloc(); //Platform Information //Level 2 - Features uint8_t s_feat_top; #if (BLE_APP_HT) uint8_t s_ht = display_screen_alloc(); //Health Thermometer #endif //BLE_APP_HT #if (BLE_APP_ACCEL) uint8_t s_accel = display_screen_alloc(); //Accelerometer #endif //BLE_APP_ACCEL //Level 2 - Application Management uint8_t s_adv = display_screen_alloc(); //Advertising uint8_t s_con = display_screen_alloc(); //Connection State uint8_t s_pin = display_screen_alloc(); //PIN Code #if RTC_SUPPORT uint8_t s_rtc = display_screen_alloc(); //RTC struct rtc_time alarm = {0,0,0,0,0,0,0,0}; #endif //RTC_SUPPORT #if (BLE_APP_HT) //Level 3 - Health Thermometer uint8_t s_ht_value = display_screen_alloc(); //Temperature Value uint8_t s_ht_type = display_screen_alloc(); //Temperature Type uint8_t s_ht_unit = display_screen_alloc(); //Unit #endif //BLE_APP_HT app_display_env.screen_id_adv = s_adv; app_display_env.screen_id_con = s_con; app_display_env.screen_id_pin = s_pin; #if RTC_SUPPORT app_display_env.screen_id_rtc = s_rtc; #endif //RTC_SUPPORT #if (BLE_APP_HT) app_display_env.screen_id_ht_value = s_ht_value; app_display_env.screen_id_ht_type = s_ht_type; app_display_env.screen_id_ht_unit = s_ht_unit; #endif //BLE_APP_HT /* ***********************************************************************/ /* Screen Insertion */ /* ***********************************************************************/ //Level 1 display_screen_insert(s_app_mgmt, s_prof); display_screen_insert(s_info, s_prof); //Level 2 - Features #if (BLE_APP_HT) s_feat_top = s_ht; #elif BLE_APP_ACCEL s_feat_top = s_accel; #else //No profile s_feat_top = display_screen_alloc(); display_screen_set(s_feat_top, NULL, "< NO PROFILE", ""); #endif #if (BLE_APP_HT) display_screen_insert(s_ht, s_feat_top); #endif //BLE_APP_HT #if (BLE_APP_ACCEL) display_screen_insert(s_accel, s_feat_top); #endif //BLE_APP_ACCEL //Level 2 - Application Management display_screen_insert(s_con, s_adv); display_screen_insert(s_pin, s_adv); #if RTC_SUPPORT display_screen_insert(s_rtc, s_adv); #endif //RTC_SUPPORT //Level 3 - Health Thermometer #if (BLE_APP_HT) display_screen_insert(s_ht_type, s_ht_value); display_screen_insert(s_ht_unit, s_ht_value); #endif //BLE_APP_HT /* ***********************************************************************/ /* Screen List Link */ /* ***********************************************************************/ //Link Level 1 - Feature with Level 2 display_screen_link(s_prof, s_feat_top); //Link Level 1 - Application Management with Level 2 display_screen_link(s_app_mgmt, s_adv); //Link Level 1 - Platform Information with Level 2 display_screen_link(s_info, 0); #if (BLE_APP_HT) //Link Level 2 - Health Thermometer with Level 3 display_screen_link(s_ht, s_ht_value); #endif //BLE_APP_HT /* ***********************************************************************/ /* Screen Set Content */ /* ***********************************************************************/ //Level 1 display_screen_set(s_prof, NULL, " PROFILES >", ""); display_screen_set(s_app_mgmt, NULL, " APPLICATION >", " MANAGEMENT"); display_screen_set(s_info, NULL, " PLATFORM >", " INFORMATION"); //Level 2 - Features #if (BLE_APP_HT) display_screen_set(s_ht, NULL, "< HEALTH >", " THERMOMETER"); #endif //BLE_APP_HT #if (BLE_APP_ACCEL) display_screen_set(s_accel, NULL, "< ACCEL. >", ""); #endif //BLE_APP_ACCEL //Level 2 - Application Management display_screen_set(s_adv, &app_display_hdl_adv, "< ADVERTISING", "s OFF"); display_screen_set(s_con, NULL, "< CONNECTED", " NO"); display_screen_set(s_pin, NULL, "< PIN CODE", " ------"); #if RTC_SUPPORT display_screen_set(s_rtc, NULL, "< No time", ""); #endif //RTC_SUPPORT #if (BLE_APP_HT) //Level 3 - Health Thermometer display_screen_set(s_ht_value, &app_display_hdl_ht_temp_value, "< TEMP VALUE", "s --.--"); display_screen_set(s_ht_type, &app_display_hdl_ht_temp_type, "< TEMP TYPE", "s NONE"); display_screen_set(s_ht_unit, NULL, "< TEMP UNIT", " CELCIUS"); #endif //BLE_APP_HT // Start with Profile screen display_start(s_prof); #if RTC_SUPPORT // Register event to handle 1sec tick ke_event_callback_set(KE_EVENT_RTC_1S_TICK, &app_display_sec_tick_evt_hdl); // Start the periodic RTC interrupt rtc_enable_alarm0(RTC_ALARM_SEC, &alarm , &app_display_sec_tick_cbck); #endif //RTC_SUPPORT }