[Tinyos-help] Tricky code segment. For MicDetector
Jacob Cox
jacobcox1974 at yahoo.com
Sun Apr 26 19:24:53 PDT 2009
I'm hopeful someone out there can explain this piece of code to me. My programming skills aren't quite up to par with this.
I see that &recording is an address, I believe m_micdest is a pointer that points to the address buffers first array[0] address.
I'm not positive what function the m_mic1 and m_mic2 functions hold. In entered comments with my understading.
/************** Microphone Task 1 Code ***********************************/
task void recordTask() { /**** Task 1 ****/
call Leds.yellowOn(); --Lights
// Initialize the sample buffer
memset(&recording, 0, REC_BUFF_SIZE); --set memory buffer to 0 for all REC_BUF_SIZE = #
atomic {
m_micdest = &recording[0]; --setting m_micdest is pointing to start of the recording address.
m_micnext = m_mic2; -- ??? not sure what this achieves for me.
}
call Microphone.start(m_mic1, MIC_SAMPLES,
(1024*1024L)/SAMPLING_FREQ, TRUE ); -- starts at address m_mic1, goes for MIC_Sampes at a freq and repeats
}
/*********************Microphone Task 2 Code ******************************/
task void recordingFinishedTask() { /****Task 2 ****/
call Leds.yellowOff(); /* signal end of recording */
//post shareRecordTask(); // Broadcast the recording buffer
}
event void RecordTimer.fired() {
post recordTask(); /*** Call to Task 2 ***/
}
/****Microphone Async If microphone fills small buffers send pointer to beginning*******/
async event result_t Microphone.repeat( void *addr, uint16_t length ) {
void *addrEnd;
call Microphone.repeatStart( m_micnext, length );
m_micnext =( uint16_t*) addr; --???
addrEnd =(( uint16_t*) addr ) + MIC_SAMPLES; --???
while( addr != addrEnd ){
*m_micdest++ = *(( uint16_t*)addr)++ >> 4; --???
}
if( m_micdest >=(&recording[0] + REC_BUFF_SIZE) ){ --???
return FAIL;
}
return SUCCESS;
}
Respectfully,
Jacob H. Cox Jr
(706) 413-3841
"What ever you do, work at it with all your heart, as working for the Lord..." Colossians 3:23
> /***************************Borrowed From Delta's Routing Protocol***********/
> #include "Delta.h"
> #include "circularQueue.h"
> #include "RangingMsg.h"
> module MicDetectM
> {
> provides interface StdControl;
> uses {
> interface Leds;
> interface SensorInterrupt as MicInterrupt;
> interface Potentiometer as MicInterruptThreshold; interface Potentiometer as Vrc;
> interface Potentiometer as Vrg;
> interface Microphone; /****************************Borrowing from Delta *****************/
> interface Send as SendDeltaMsg;
> interface Intercept as SnoopDeltaMsg;
> interface RouteControl;
> interface RouteStatistics;
> interface Timer;
> /******************************************************************/ interface Timer2<TMilli> as RecordTimer;
> interface Timer2<TMilli> as StdTimer; interface Timer2<TMilli> as TimeStamper;
> interface Timer2<TMilli> as SendRecTimer;
> /****************Sending Interfaces*******************************/
> interface SPSend as RecordSend; // Download recording buffer
> //interface SPSend as TriggerRecSend; // Download chirp
> interface SPReceive as RequestRecording; // Ask the mote to send the recording buffer
> }
> }
> implementation
> {
> /******************** Routing Variables from Delta**************/
> uint16_t m_adc;
> uint32_t m_seqno;
> TOS_Msg bmsg[DELTA_QUEUE_SIZE];
> CircularQueue_t queue;
> /***************Other Variables****************************/
> TOS_Msg m_tosmsg;
> sp_message_t m_spmsg;
> sp_message_flags_t m_flags = SP_FLAG_C_URGENT
> | SP_FLAG_C_RELIABLE; //provide timestamping
> // Use this to send/receive the chirp
> chirpDataMsg_t* m_chirpDataMsg;
> // Use this to send the recording
> recordDataMsg_t* m_recordDataMsg;
> // Use this to send a trigger to another mote
> triggerRecordingMsg_t* m_triggerRecMsg;
> // Used to receive configuration updates
> configMsg_t* m_configMsg;
> /****************Declare Constants and variables ********************/
> enum {TIMER_INTERVAL = 5*1024}; /* Timer repeat every 5 seconds */
> enum {MIC_INT_THRESH = -50};
> enum {VRC_SET = 10,
> VRG_SET = 10};
> enum {MIC_SAMPLES = 16, // in bytes
> REC_BUFF_SIZE = 2048, // in bytes
> SAMPLING_FREQ = 8192, // in Hz (16 kHz)
> MSG_SAMPLES = 16,
> SCALED_AMPLITUDE = 16,
> };
> uint16_t recordCounter;
> uint8_t recording[REC_BUFF_SIZE]; // Buffer to store the recording
> uint8_t* m_micdest; // Destination buffer
> uint16_t m_mic1 [MIC_SAMPLES]; // Mic buffer 1
> uint16_t m_mic2 [MIC_SAMPLES]; // Mic buffer 2
> uint16_t* m_micnext; /** more **/
> /***************************Delta Multihop Routing *********************/
> /******Functions to establish Routing *****/
> task void sendData() { // Route Task 1
> uint16_t _length;
> int i;
> uint16_t neighbors[MHOP_PARENT_SIZE];
> uint16_t quality[MHOP_PARENT_SIZE];
>
> if (cqueue_pushBack( &queue ) == SUCCESS) {
> DeltaMsg* dmsg = (DeltaMsg*)call SendDeltaMsg.getBuffer(&bmsg[queue.back], &_length);
> atomic dmsg->reading = m_adc;
> dmsg->parent = call RouteControl.getParent();
> call RouteStatistics.getNeighbors(neighbors, MHOP_PARENT_SIZE);
> call RouteStatistics.getNeighborQuality(quality, MHOP_PARENT_SIZE);
> for (i = 0; i < MHOP_PARENT_SIZE; i++) {
> dmsg->neighbors[i] = neighbors[i];
> dmsg->quality[i] = quality[i];
> }
> dmsg->neighborsize = MHOP_PARENT_SIZE;
> dmsg->retransmissions = call RouteStatistics.getRetransmissions();
> dmsg->seqno = m_seqno;
> if (call SendDeltaMsg.send( &bmsg[queue.back], sizeof(DeltaMsg) ) == SUCCESS) {
> //call Leds.redOn();
> }
> else {
> // remove from queue
> cqueue_popBack( &queue );
> }
> }
> /* increase seqno to determine dropped packets */
> m_seqno++;
> }
> //void blinkBlue() {
> //call Leds.yellowOn();
> //call TimerBlink.start(TIMER_ONE_SHOT, 20);
> //}
> /****************** StdTimer *************************************/
> uint8_t iLed = 1;
> event void StdTimer.fired() {
> /* reset memory if a interupt has not occured */
> iLed += 1;
> if (iLed > 100) {
> iLed=1;}
> if (iLed%2 == 0) {
> call Leds.greenOff();
> call Leds.redOn();
> } else if (iLed%2 == 1) {
> call Leds.greenOff();
> call Leds.redOff();
> }
> }
> /****************** Lets do some routing **************************/
> /***************See Delta Routing Code Below **********************/
> event result_t Timer.fired() {
> //call ADC.getData();
> return SUCCESS;
> }
> /******************** TimeStamp **********************************/
> uint8_t time = 0; event void TimeStamper.fired () {
> uint16_t timestamp;
> time += time;
> timestamp = time; //% (61440);
> if (time <= 5120) { /*( 1024 * 5 )resets timestamp every minute */
> time = 0;}
> }
> /******************** Start Application ***************************/
> command result_t StdControl.init() {
> call Leds.init();
> call MicInterruptThreshold.set(MIC_INT_THRESH);
> call Vrc.set(VRC_SET);
> call Vrg.set(VRG_SET);
> /*******Added from Delta **************/
> cqueue_init( &queue, DELTA_QUEUE_SIZE );
> /***************************************/
> return SUCCESS;
> }
> command result_t StdControl.start() {
> call StdTimer.startPeriodic ( TIMER_INTERVAL );
> call RecordTimer.startOneShot(1024);
> call TimeStamper.startPeriodic (1);
> call Timer.start( TIMER_REPEAT, DELTA_TIME );
> return call MicInterrupt.enable();
> //return SUCCESS;
> }
> /************** Microphone Task 1 Code ***********************************/
> task void recordTask() { /**** Task 1 ****/
> call Leds.yellowOn();
> // Initialize the sample buffer
> memset(&recording, 0, REC_BUFF_SIZE);
> atomic {
> m_micdest = &recording[0];
> m_micnext = m_mic2;
> }
> call Microphone.start(m_mic1, MIC_SAMPLES,
> (1024*1024L)/SAMPLING_FREQ, TRUE );
> }
> /*********************Microphone Task 2 Code ******************************/
> task void recordingFinishedTask() { /****Task 2 ****/
> call Leds.yellowOff(); /* signal end of recording */
> post shareRecordTask(); // Broadcast the recording buffer
> }
> event void RecordTimer.fired() {
> post recordTask(); /*** Call to Task 2 ***/
> }
> /****Microphone Async If microphone fills small buffers send pointer to beginning*******/
> async event result_t Microphone.repeat( void *addr, uint16_t length ) {
> void *addrEnd;
> call Microphone.repeatStart( m_micnext, length );
> m_micnext =( uint16_t*) addr;
> addrEnd =(( uint16_t*) addr ) + MIC_SAMPLES;
> while( addr != addrEnd ){
> *m_micdest++ = *(( uint16_t*)addr)++ >> 4;
> }
> if( m_micdest >=(&recording[0] + REC_BUFF_SIZE) ){
> return FAIL;
> }
> return SUCCESS;
> }
> /**************************** Microphone Task 3 ********************************/
> /* Scales whatever we have in the recording buffer */
> task void scaleAudioTask() { /*** Task 3 ***/
> uint8_t* p;
> const uint8_t* pEnd;
> uint8_t maxval = recording[0];
> uint8_t minval = recording[0];
> for( p =( uint8_t* ) &recording,
> pEnd =( uint8_t* )&recording + REC_BUFF_SIZE;
> p!=pEnd; p++ ) {
> if( *p > maxval ) maxval = *p;
> if( *p < minval ) minval = *p;
> } if( maxval > minval ) {
> uint16_t k =(2U * SCALED_AMPLITUDE * 256U) /(maxval - minval );
> for( p=&recording[0]; p!=pEnd; p++ )
> *p =(( k * (*p - minval ) ) >> 8) +(128 - SCALED_AMPLITUDE);
> }
> post recordingFinishedTask(); /*** Call to Task 2 ***/
> }
> /*********************** Microphone Task 4 **************************************/
> /* * Helper task for sharing the record buffer * */
> /* Posted by shareRecordTask() */
> task void shareRecordHelperTask() {
> uint8_t i = 0;
> uint32_t offset = 0;
> m_recordDataMsg =( recordDataMsg_t*)m_tosmsg.data;
> if( recordCounter <(REC_BUFF_SIZE / MSG_SAMPLES ) ) {
> m_recordDataMsg->seqNo = recordCounter;
> for( i = 0; i < MSG_SAMPLES; i++) {
> offset = recordCounter * MSG_SAMPLES;
> m_recordDataMsg -> data [ i ] =( uint8_t ) recording [ offset + i ];
> }
> /* Send the record datamessage*/
> call RecordSend.send(&m_spmsg, &m_tosmsg, TOS_BCAST_ADDR, recordDataMsg_size );
> }
> recordCounter++;
> }
> /***************************** Microphone Task 5 ******************************/
> /******This task trasmits recording buffer **********/
> task void shareRecordTask() {
> recordCounter = 0;
> post shareRecordHelperTask();
> }
> /************** MICROPHONE EVENTS triggered by Calls in above tasks **************/
> event void RecordSend.sendDone( sp_message_t *msg,
> sp_message_flags_t flags,
> sp_error_t error ){
> call SendRecTimer.startOneShot(SEND_DELAY);
> }
> event void RequestRecording.receive( sp_message_t *spmsg,
> TOS_MsgPtr tosmsg,
> sp_error_t result){
> post shareRecordTask();
> }
> event void SendRecTimer.fired() {
> post shareRecordHelperTask();
> }
> /************************** Delta Send Code *****************************/
> /***************************** SEND *************************************/
> event result_t SendDeltaMsg.sendDone(TOS_MsgPtr _msg, result_t _success) {
> cqueue_popFront( &queue );
> //if (cqueue_isEmpty( &queue )) {call Leds.redOff();}
> return SUCCESS;
> }
> /************************* SEND ************************************/
> event result_t SnoopDeltaMsg.intercept(TOS_MsgPtr _msg, void* payload, uint16_t payloadLen) {
> //blinkBlue();
> return SUCCESS;
> }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://www.millennium.berkeley.edu/pipermail/tinyos-help/attachments/20090426/f3868950/attachment-0001.htm
More information about the Tinyos-help
mailing list