/*-------------------------------------------------------------------------*/ THREAD(StartupInit, arg) { NutThreadSetPriority(5); NetworkInit(); initialized = true; NutThreadExit(); }
/*-----functions-----*/ THREAD(tone, arg){ VsPlayerInit(); VsSetVolume(1, 1); VsBeepStart(2500); NutSleep(1000); VsBeepStop(); NutThreadExit(); NutThreadKill(); NutThreadDestroy(); }
THREAD(AlarmSync, arg) { NutThreadSetPriority(50); while(initialized == false){ NutSleep(1000); } int dayCounter = 0; int counter = 0; while(!NtpSync() && counter < 10) { NutSleep(1000); counter++; } counter = 0; for(;;) { if((initialized == true) && (hasNetworkConnection() == true)) { checkSleep(); isAlarmSyncing = true; char url[49]; sprintf(url, "/getAlarmen.php?radiomac=%s&tz=%d", getMacAdress(), getTimeZone()); httpGet(url, parseAlarmJson); isAlarmSyncing = false; char url2[43]; sprintf(url2, "/getTwitch.php?radiomac=%s", getMacAdress()); httpGet(url2, parseTwitch); char url3[43]; sprintf(url3,"/getTwitter.php?radiomac=%s", getMacAdress()); httpGet(url3,TwitterParser); //Command que (Telegram) sync sprintf(url, "%s%s", "/getCommands.php?radiomac=", getMacAdress()); httpGet(url, parseCommandQue); } while(dayCounter > 28800 && (hasNetworkConnection() == true)) { while(!NtpSync() && counter < 10) { NutSleep(1000); counter++; } dayCounter = 28800; counter = 0; } dayCounter++; NutSleep(3000); } NutThreadExit(); }
THREAD(AlarmCheckerThread, arg) { for (;;) { NutSleep(5000); if(CheckAlarm1()) { printf("Alarm should go off\n"); startStream(); NutSleep(61000); NutThreadExit(); printf("Exit thread and started new one...\n"); NutThreadCreate("Bl", AlarmCheckerThread, NULL, 512); break; }else{ printf("Alarm should not go off\n"); } if(CheckAlarm2()) { printf("Alarm should go off\n"); if(IMCconfig.alarm2.mode ==0) { NutThreadCreate("Bq", AlarmBeep, NULL, 512); } else{ startStream(); } NutSleep(61000); NutThreadExit(); printf("Exit thread and started new one...\n"); NutThreadCreate("Bl", AlarmCheckerThread, NULL, 512); break; }else{ printf("Alarm should not go off\n"); } } }
THREAD(PlayMp3StreamThread, arg) { u_short mss = 1460; u_long rx_to = 3000; u_short tcpbufsiz = 8000; FILE *stream; sock = SocketCreate(sock,mss,rx_to,tcpbufsiz); /*Connect the radio station.*/ u_long radio_ip = inet_addr(RADIO_IPADDR); stream = ConnectStation(sock, radio_ip, RADIO_PORT, &metaint); if(stream) { PlayMp3Stream(stream,metaint); fclose(stream); } NutTcpCloseSocket(sock); NutThreadExit(); for (;;); }
THREAD(FallingAsleep, arg) { printf("falling asleep started\n"); for(;;) { int volumeGoal = 254; //Dont start yet if no sound is playing while(VsGetStatus() != VS_STATUS_RUNNING) NutSleep(50); //Get the amount of seconds from the fallingAsleepTime minutes long totalSeconds = (long)fallingAsleepTime * 1000 * 60; printf("totalseconds = %lu\n", totalSeconds); //Get the time to sleep before "increasing" the volume. Divide the totalSeconds by the volumeGoal to get a smooth transition long sleepTime = totalSeconds / volumeGoal; printf("sleeptime = %lu\n", sleepTime); int currentVolume = VsGetVolume(); //"Increase" the volume until it hits the goal while(currentVolume < volumeGoal) { NutSleep(sleepTime); VsSetVolume(currentVolume + 1, currentVolume + 1); printf("Falling Asleep Mode tick now. Volume lowered %d", VsGetVolume()); currentVolume++; } //Call stopStream in network.c stopStream(); //Turn off falling asleep mode fallingAsleepMode = 0; //Reset volume VsSetVolume(0, 0); //Exit the thread NutThreadExit(); } }
THREAD(StreamPlayer, arg) { FILE *stream = (FILE *) arg; size_t rbytes = 0; char *mp3buf; int result = NOK; int nrBytesRead = 0; unsigned char iflag; // // Init MP3 buffer. NutSegBuf is een globale, systeem buffer // if(NutSegBufInit(8192) != 0) { // Reset global buffer iflag = VsPlayerInterrupts(0); NutSegBufReset(); VsPlayerInterrupts(iflag); result = OK; } // Init the Vs1003b hardware if(result == OK) { if(VsPlayerInit() == -1) { if(VsPlayerReset(0) == -1) { result = NOK; } } } for(;;) { /* * Query number of byte available in MP3 buffer. */ iflag = VsPlayerInterrupts(0); mp3buf = NutSegBufWriteRequest(&rbytes); VsPlayerInterrupts(iflag); // Bij de eerste keer: als player niet draait maak player wakker (kickit) if(VsGetStatus() != VS_STATUS_RUNNING) { VsPlayerKick(); //Why would you wait to start the player...? /*if(rbytes < 1024) { printf("VsPlayerKick()\n"); VsPlayerKick(); }*/ } while(rbytes && !closeStream) { // Copy rbytes (van 1 byte) van stream naar mp3buf. nrBytesRead = fread(mp3buf,1,rbytes,stream); if(nrBytesRead > 0) { iflag = VsPlayerInterrupts(0); mp3buf = NutSegBufWriteCommit(nrBytesRead); VsPlayerInterrupts(iflag); if(nrBytesRead < rbytes && nrBytesRead < 512) { NutSleep(250); } } else { break; } rbytes -= nrBytesRead; if(nrBytesRead <= 0) { break; } } if(closeStream) { closeStream = 0; NutThreadExit(); } } }