THREAD(idle, arg) { NutTimerInit(); NutThreadCreate("main", WebDemo, 0, 768); NutThreadSetPriority(254); for (;;) { #ifdef HEARTBEAT_BIT HeartBeat(); #endif NutThreadYield(); } }
int readAnalog(void* params) { struct analogParams* par = (struct analogParams*) params; ADCSetChannel(par->channel); ADCStartConversion(); uint16_t adc = 0; while (ADCRead(&adc)) { NutThreadYield(); } //TODO zrobić coś z adc //return (adc >> 1) - 183; return adc; }
/* * From RS232 to socket. */ THREAD(Receiver, arg) { CHANNEL *cdp = arg; for (;;) { if (cdp->cd_connected) { NutThreadSetPriority(64); /* * We are reading from the UART without any timeout. So we * won't return immediately when disconnected. */ StreamCopy(cdp->cd_rs232, cdp->cd_tcpip, &cdp->cd_connected); NutThreadSetPriority(128); } NutThreadYield(); } }
THREAD(sensorsReader, arg) { struct sensorNode* list = (struct sensorNode*) arg; struct sensorNode* current; NutThreadSetPriority(70); for (;;) { current = list; while (current != NULL) { current->data.value = current->data.read(current->data.params); current = current->next; NutThreadYield(); } NutSleep(5000); } }
/* \fn NutIdle(void *arg) * \brief Idle thread. * * After initializing the timers, the idle thread switches to priority 254 * and enters an endless loop. */ THREAD(NutIdle, arg) { /* Initialize system timers. */ NutTimerInit(); /* Create the main application thread. */ NutThreadCreate("main", main, 0, 768); /* * Run in an idle loop at the lowest priority. We can still * do something useful here, like killing terminated threads * or putting the CPU into sleep mode. */ NutThreadSetPriority(254); for (;;) { NutThreadYield(); NutThreadDestroy(); } }
/*! * \brief Emulated idle thread. * * After initializing the timers, the idle thread switches to priority 254 * and enters an endless loop. */ THREAD(NutIdle, arg) { /* Initialize system timers. */ NutTimerInit(); /* Create the main application thread. */ NutThreadCreate("main", NutAppMain, 0, NUT_THREAD_MAINSTACK); // printf("main task created, idling now..\n"); /* * Run in an idle loop at the lowest priority. We can still * do something useful here, like killing terminated threads * or putting the CPU into sleep mode. */ NutThreadSetPriority(254); for (;;) { NutThreadYield(); NutThreadDestroy(); // sleep(); ... sleeping would be fine. } }
/*! * \brief Temporarily suspends the current thread. * * Causes the current thread to wait for a specified interval or, * if the specified interval is zero, to give up the CPU for * another thread with higher or same priority. * * This function may switch to another application thread, that * got the same or a higher priority and is ready to run. * * \note Threads may sleep longer than the specified number of * milliseconds, depending on the number of threads * with higher or equal priority, which are ready to run. * * \param ms Milliseconds to sleep. If 0, the current thread will not * sleep, but may give up the CPU. The resolution is limited * to the granularity of the system timer. * * \todo Code size can be reduced by trying to create the timer before * removing the thread from the run queue. */ void NutSleep(uint32_t ms) { if (ms) { /* remove running thread from runQueue */ NutThreadRemoveQueue(runningThread, &runQueue); runningThread->td_state = TDS_SLEEP; if ((runningThread->td_timer = NutTimerStart(ms, NutThreadWake, runningThread, TM_ONESHOT)) != 0) { #ifdef NUTTRACER TRACE_ADD_ITEM(TRACE_TAG_THREAD_SLEEP,(int)runningThread); #endif NutThreadResume(); } else { /* timer creation failed, restore queues */ runningThread->td_queue = &runQueue; runningThread->td_qnxt = runQueue; runningThread->td_state = TDS_RUNNING; runQueue = runningThread; } } else NutThreadYield(); }
/* * \brief Play MP3 stream. * * \param stream Socket stream to read MP3 data from. */ void PlayMp3Stream(FILE *stream, u_long metaint) { size_t rbytes; u_char *mp3buf; u_char ief; int got = 0; u_long last; u_long mp3left = metaint; /* * Initialize the MP3 buffer. The NutSegBuf routines provide a global * system buffer, which works with banked and non-banked systems. */ if (NutSegBufInit(8192) == 0) { puts("Error: MP3 buffer init failed"); return; } /* * Initialize the MP3 decoder hardware. */ if (VsPlayerInit() || VsPlayerReset(0)) { puts("Error: MP3 hardware init failed"); return; } VsSetVolume(0, 0); /* * Reset the MP3 buffer. */ ief = VsPlayerInterrupts(0); NutSegBufReset(); VsPlayerInterrupts(ief); last = NutGetSeconds(); for (; ;) { if(STOP_THREAD){ STOP_THREAD = 0; return; } /* * Query number of byte available in MP3 buffer. */ ief = VsPlayerInterrupts(0); mp3buf = NutSegBufWriteRequest(&rbytes); VsPlayerInterrupts(ief); /* * If the player is not running, kick it. */ if (VsGetStatus() != VS_STATUS_RUNNING) { if (rbytes < 1024 || NutGetSeconds() - last > 4UL) { last = NutGetSeconds(); puts("Kick player"); VsPlayerKick(); } } /* * Do not read pass metadata. */ if (metaint && rbytes > mp3left) { rbytes = mp3left; } /* * Read data directly into the MP3 buffer. */ while (rbytes) { if ((got = fread(mp3buf, 1, rbytes, stream)) > 0) { ief = VsPlayerInterrupts(0); mp3buf = NutSegBufWriteCommit(got); VsPlayerInterrupts(ief); if (metaint) { mp3left -= got; if (mp3left == 0) { ProcessMetaData(stream); mp3left = metaint; } } if (got < rbytes && got < 512) { printf("%lu buffered\n", NutSegBufUsed()); NutSleep(250); } else { NutThreadYield(); } } else { break; } rbytes -= got; } if (got <= 0) { break; } NutSleep(100); } }
THREAD(SoundFFT, arg) { printf("SoundFFT started\n"); fflush(stdout); SoundStruct* ss = arg; int i, j; //short x = 10; short * melBuffer = malloc( WIN_SIZE/2 * sizeof(short)); // 64 short * cepsBuffer = malloc( CEPS_COUNT * WIN_SIZE * sizeof(short) ); // 4096 short * real; short * im = malloc( WIN_SIZE * sizeof(short) ); // 128 for(i = 0; i < WIN_SIZE; i++) im[i] = 0; int ** ceps = malloc(CEPS_COUNT * sizeof(int*)); // 32 for( i = 0; i< CEPS_COUNT; i++) ceps[i] = malloc(SIGNIFICANTS * sizeof(int)); // 20 if(ss->recording) { // wait for sample NutEventWait( &recordBufferReadyRead, 0 ); memcpy( cepsBuffer, ss->buffer, ss->ceps_count * WIN_SIZE * sizeof(short) ); real = cepsBuffer; SR_PreFilter(real, ss->ceps_count * WIN_SIZE); int ** sampleceps = malloc(CEPS_COUNT * sizeof(int*)); for( i = 0; i< ss->ceps_count; i++) sampleceps[i] = malloc(SIGNIFICANTS * sizeof(int)); for(i = 0; i < ss->ceps_count; i++) { SR_generateMFC(real, im, melBuffer, WIN_SIZE_E, sampleceps[i]); real += WIN_SIZE; } printf("Sample tallessa!\n\n%d\n", ss->ceps_count); // Tulostaa tallennetun samplen ja stoppaa printf("{\n"); for( i = 0; i< ss->ceps_count; i++) { printf("{"); for( j = 0; j < SIGNIFICANTS; j++) { printf("%d, ", sampleceps[i][j]); } printf("},\n"); } printf("},\n"); for(;;) NutThreadYield(); } int minDist, minDistKey, distance, ceps_count; for(;;) { minDist = INT_MAX; minDistKey = 10; NutEventWait( &recordBufferReadyRead, 0 ); //copy samples from ss->buffer to cepsBuffer memcpy( cepsBuffer, ss->buffer, ss->ceps_count * WIN_SIZE * sizeof(short) ); real = cepsBuffer; ceps_count = ss->ceps_count; //SR_PreFilter(real, ss->ceps_count * WIN_SIZE); for(i = 0; i < ceps_count; i++) { SR_generateMFC(real, im, melBuffer, WIN_SIZE_E, ceps[i]); real += WIN_SIZE; } //recognition for(i = 0; i < SAMPLE_LIB_SIZE; i++) { distance = SR_DTWDistance(ss, ceps, i, ceps_count); if( distance < minDist ) { minDist = distance; minDistKey = i; } } //printf("%d - %d\n", minDistKey, minDist); if ( minDist < DTW_TRESHOLD ) { short com = 0; switch ( minDistKey ) { case 0: com = ARROW_UP; break; case 1: com = ARROW_DOWN; break; case 2: com = ARROW_LEFT; break; case 3: com = ARROW_RIGHT; break; case 4: com = ENTER_BUTTON; break; case 5: com = UNDO_BUTTON; break; default: break; } if(com != 0) UI_CQpush(ss->ui, com); } //printf("MinDistance: %d - %d\n", minDistKey, minDist); //fflush(stdout); } for(;;) NutThreadYield(); }
/* * Process client requests. */ void ProcessRequests(FILE * stream) { char buff[128]; char *cp; int stat = -1; fputs("200 Welcome to portdio. Type help to get help.\r\n", stream); for (;;) { fflush(stream); /* * Read a line from the client. Ignore * blank lines. */ if (fgets(buff, sizeof(buff), stream) == 0) break; if ((cp = strchr(buff, '\r')) != 0) *cp = 0; if ((cp = strchr(buff, '\n')) != 0) *cp = 0; if (buff[0] == 0) continue; /* * Memory info. */ if (strncmp(buff, "memory", strlen(buff)) == 0) { fprintf(stream, "210 %u bytes RAM free\r\n", (unsigned int)NutHeapAvailable()); continue; } #ifdef OUTBANK /* * Reset output bit. */ if (strlen(buff) > 1 && strncmp(buff, "reset", strlen(buff) - 1) == 0) { int ok = 1; switch (buff[strlen(buff) - 1]) { #ifdef OUTPIN1 case '1': GpioPinSetLow(OUTBANK, OUTPIN1); break; #endif #ifdef OUTPIN2 case '2': GpioPinSetLow(OUTBANK, OUTPIN2); break; #endif #ifdef OUTPIN3 case '3': GpioPinSetLow(OUTBANK, OUTPIN3); break; #endif #ifdef OUTPIN4 case '4': GpioPinSetLow(OUTBANK, OUTPIN4); break; #endif default: ok = 0; break; } if (ok) { fputs("210 OK\r\n", stream); } else fputs("410 Bad pin\r\n", stream); continue; } /* * Set output bit. */ if (strlen(buff) > 1 && strncmp(buff, "set", strlen(buff) - 1) == 0) { int ok = 1; switch (buff[strlen(buff) - 1]) { #ifdef OUTPIN1 case '1': GpioPinSetHigh(OUTBANK, OUTPIN1); break; #endif #ifdef OUTPIN2 case '2': GpioPinSetHigh(OUTBANK, OUTPIN2); break; #endif #ifdef OUTPIN3 case '3': GpioPinSetHigh(OUTBANK, OUTPIN3); break; #endif #ifdef OUTPIN4 case '4': GpioPinSetHigh(OUTBANK, OUTPIN4); break; #endif default: ok = 0; break; } if (ok) { fputs("210 OK\r\n", stream); } else fputs("410 Bad pin\r\n", stream); continue; } #endif /* OUTBANK */ #ifdef INBANK /* * Port status. */ if (strncmp(buff, "query", strlen(buff)) == 0) { stat = PortStatus(); fprintf(stream, "210 %02X\r\n", stat); continue; } /* * wait for status change. */ if (strncmp(buff, "wait", strlen(buff)) == 0) { while (stat == PortStatus()) NutThreadYield(); stat = PortStatus(); fprintf(stream, "210 %02X\r\n", stat); continue; } #endif /* INBANK */ /* * Help. */ fputs("400 List of commands follows\r\n", stream); fputs("memory\tQueries number of RAM bytes free\r\n", stream); #if OUTBANK fputs("reset#\tSet output bit 1..4 low\r\n", stream); fputs("set#\tSet output bit 1..4 high\r\n", stream); #endif #if INBANK fputs("query\tQuery digital i/o status\r\n", stream); fputs("wait\tWaits for digital i/o change\r\n", stream); #endif fputs(".\r\n", stream); } }