void LibIO_Demo_FPrintf_FScanf(void) { u32 printCharCount; LibFileIoClass ioFile("Test_DeleteMe.txt", "w+b"); ioFile.FileOpen(); ASSERT_CHK( rc, LibIO_FPrintf(printCharCount, ioFile.fp, "123 %d %d\n", 33, 44) ); DUMPD(printCharCount); rewind(ioFile.fp); char tempStr[10]; u32 scanCount; ASSERT_CHK( rc, LibIO_FScanf(scanCount, ioFile.fp, "%s", tempStr) ); DUMPD(scanCount); DUMPS(tempStr); ASSERT_CHK( rc, LibIO_FScanf(scanCount, ioFile.fp, "%s", tempStr) ); DUMPD(scanCount); DUMPS(tempStr); ASSERT_CHK( rc, LibIO_FScanf(scanCount, ioFile.fp, "%s", tempStr) ); DUMPD(scanCount); DUMPS(tempStr); rc = LibIO_FScanf(scanCount, ioFile.fp, "%s", tempStr); LibError_PrintErrorMessage(rc); }
void MD_MIDIFile::processEvents(uint16_t ticks) { uint8_t n; if (_format != 0) { DUMP("\n-- [", ticks); DUMPS("] TRK "); } #if TRACK_PRIORITY // process all events from each track first - TRACK PRIORITY for (uint8_t i = 0; i < _trackCount; i++) { if (_format != 0) DUMPX("", i); // Limit n to be a sensible number of events in the loop counter // When there are no more events, just break out // Other than the first event, the other have an elapsed time of 0 (ie occur simultaneously) for (n=0; n < 100; n++) { if (!_track[i].getNextEvent(this, (n==0 ? ticks : 0))) break; } if ((n > 0) && (_format != 0)) DUMPS("\n-- TRK "); } #else // EVENT_PRIORITY // process one event from each track round-robin style - EVENT PRIORITY bool doneEvents; // Limit n to be a sensible number of events in the loop counter for (n = 0; (n < 100) && (!doneEvents); n++) { doneEvents = false; for (uint8_t i = 0; i < _trackCount; i++) // cycle through all { bool b; if (_format != 0) DUMPX("", i); // Other than the first event, the other have an elapsed time of 0 (ie occur simultaneously) b = _track[i].getNextEvent(this, (n==0 ? ticks : 0)); if (b && (_format != 0)) DUMPS("\n-- TRK "); doneEvents = (doneEvents || b); } // When there are no more events, just break out if (!doneEvents) break; } #endif // EVENT/TRACK_PRIORITY }
uint32_t MD_TCS230::readSingle(void) // blocking read of a single sensor value (ie, not rgb) { DUMPS("\nreadSingle"); FreqCount.begin(1000/_readDiv); // start while (!FreqCount.available()) // wait { DUMPS("."); } FreqCount.end(); // stop DUMP("VALUE ", FreqCount.read()); return(FreqCount.read() * _readDiv); }
void LibIO_Demo_SPrintf_SScanf(void) { char tempStr[15]; char scanTempStr0[10]; char scanTempStr1[10]; char scanTempStr2[10]; char scanTempStr3[10]; u32 printCharCount; u32 scanCount; ASSERT_CHK( rc, LibIO_SPrintf(printCharCount, tempStr, "123 %d %d\n", 33, 44) ); DUMPD(printCharCount); DUMPS(tempStr); ASSERT_CHK( rc, LibIO_SScanf(scanCount, tempStr, "%s", scanTempStr0) ); DUMPD(scanCount); DUMPS(scanTempStr0); ASSERT_CHK( rc, LibIO_SScanf(scanCount, tempStr, "%s%s", scanTempStr0, scanTempStr1) ); DUMPD(scanCount); DUMPS(scanTempStr0); DUMPS(scanTempStr1); ASSERT_CHK( rc, LibIO_SScanf(scanCount, tempStr, "%s%s%s%s", scanTempStr0, scanTempStr1, scanTempStr2, scanTempStr3) ); DUMPD(scanCount); DUMPS(scanTempStr0); DUMPS(scanTempStr1); DUMPS(scanTempStr2); DUMPS(scanTempStr3); DUMPD(rc); perror("perror()"); }
uint8_t MD_TCS230::readFSM(uint8_t s) // Finite State Machine to read a value (internal function) { static const uint8_t seq[] = { TCS230_RGB_R, TCS230_RGB_G, TCS230_RGB_B }; static uint8_t currCol; // index for seq above switch(s) { case 0: // enable the hardware for reading DUMPS("\n0"); currCol = 0; // RGB_R but we don't care setEnable(true); s++; // fall through to the next state case 1: // select a filter and start a reading DUMPS("\n1"); setFilter(seq[currCol]); FreqCount.begin(1000/_readDiv); s++; break; case 2: // see if a value is available DUMPS("2"); if (FreqCount.available()) { DUMP(" VALUE ", FreqCount.read()); // read the value and save it _Fo.value[seq[currCol++]] = FreqCount.read() * _readDiv; if (currCol < RGB_SIZE) { // loop around again on next call to available() s--; } else { // end this reading session FreqCount.end(); setEnable(false); RGBTransformation(); s = 0; } } break; } return(s); }
void dumpBuffer(uint8_t *p, int len) // Formatted dump of a buffer of data { for (int i=0; i<len; i++, p++) { if ((i!=0) && ((i & 0x0f) == 0)) // every 16 characters DUMPS("\n"); DUMPS(" "); if (*p<=0xf) DUMPX("0", *p) else DUMPX("", *p); } }
int main(int argc, char **argv) { size_t dim = 4; char *lex; for (int c; (c = getopt(argc, argv, "n:")) != -1;) { switch(c) { case 'n': dim= atoi(optarg); break; } } TALLOCS(lex,dim,exit(1)); int i; for(i = 0; i < dim; i++) { lex[i] = '0' + dim - i - 1; } i = 1; swap_iter(lex, dim, dim-1, &i); DUMPD(i); DUMPS(lex); return 0; }
bool MD_MFTrack::getNextEvent(MD_MIDIFile *mf, uint16_t tickCount) // track_event = <time:v> + [<midi_event> | <meta_event> | <sysex_event>] { uint32_t deltaT; // is there anything to process? if (_endOfTrack) return(false); // move the file pointer to where we left off mf->_fd.seekSet(_startOffset+_currOffset); // Work out new total elapsed ticks - include the overshoot from // last event. _elapsedTicks += tickCount; // Get the DeltaT from the file in order to see if enough ticks have // passed for the event to be active. deltaT = readVarLen(&mf->_fd); // If not enough ticks, just return without saving the file pointer and // we will go back to the same spot next time. if (_elapsedTicks < deltaT) return(false); // Adjust the total elapsed time to the error against actual DeltaT to avoid // accumulation of errors, as we only check for _elapsedTicks being >= ticks, // giving positive biased errors every time. _elapsedTicks -= deltaT; DUMP("\ndT: ", deltaT); DUMP(" + ", _elapsedTicks); DUMPS("\t"); parseEvent(mf); // remember the offset for next time _currOffset = mf->_fd.curPosition() - _startOffset; // catch end of track when there is no META event _endOfTrack = _endOfTrack || (_currOffset >= _length); if (_endOfTrack) DUMPS(" - OUT OF TRACK"); return(true); }
void MD_MFTrack::dump(void) { DUMP("\n[Track ", _trackId); DUMPS(" Header]"); DUMP("\nLength:\t\t\t", _length); DUMP("\nFile Location:\t\t", _startOffset); DUMP("\nEnd of Track:\t\t", _endOfTrack); DUMP("\nCurrent buffer offset:\t", _currOffset); }
void MD_TCS230::setEnable(bool b) // enable the sensor output (b=true) // can be done by toggling OE (preferred) or by setting the frequency output { if (b) DUMPS("\nEn"); else DUMPS("\nDis"); DUMPS("abling using "); if (_OE != NO_PIN) { DUMPS("OE"); digitalWrite(_OE, (b) ? LOW : HIGH); // reverse logic } else { DUMPS("FREQ"); setFrequency2((b) ? _freqSet : TCS230_FREQ_OFF); } }
void MD_TCS230::setWhiteCal(sensorData *d) // set white calibration data for rgb calculations { if (d == NULL) return; DUMPS("\nsetWhiteCal"); for (uint8_t i=0; i<RGB_SIZE; i++) { DUMP(" ", d->value[i]); _Fw.value[i] = d->value[i]; } }
void MD_TCS230::getRGB(colorData *rgb) // get the rgb value of the current reading { if (rgb == NULL) return; DUMPS("\ngetRGB"); for (uint8_t i=0; i<RGB_SIZE; i++) { rgb->value[i] = _rgb.value[i]; DUMP(" ", rgb->value[i]); } }
int main(int argc, char *argv[]) { LRM_EVT_EvtPara_t evtPara = {"zzTest_input.txt"}; EXIT_CHK( rc, Lrm_StateMachine(LRM_EVT_E5_SetInputFile, &evtPara) ); EXIT_CHK( rc, Lrm_StateMachine(LRM_EVT_E6_GetLineObj, &evtPara) ); LRM_t st = Lrm_StateMachine(LRM_EVT_E1_ReadLine); DUMPD(st); DUMPS((*(evtPara.lineObj)).lineStr); while (LRM_S6_Line_Read_LAST != Lrm_StateMachine(LRM_EVT_E1_ReadLine)) { DUMPS((*(evtPara.lineObj)).lineStr); } Lrm_StateMachine(LRM_EVT_E2_Exit); while (LRM_S8_Line_Read_EOF != Lrm_StateMachine(LRM_EVT_E1_ReadLine)) { DUMPS((*(evtPara.lineObj)).lineStr); } return 0; }
void MD_TCS230::getRaw(sensorData *d) // get the raw data of the current reading // useful to set dark and white calibration data { if (d == NULL) return; DUMPS("\ngetRAW"); for (uint8_t i=0; i<RGB_SIZE; i++) { d->value[i] = _Fo.value[i]; DUMP(" ", d->value[i]); } }
int main(int argc, char **argv) { for (int c; (c = getopt(argc, argv, "n:p:")) != -1;) { switch(c) { case 'n': DUMPD(calc_quadratic(atoi(optarg), 1, 41)); exit(EXIT_SUCCESS); case 'p': DUMPS(is_prime(atoi(optarg)) ? "yes" : "no"); exit(EXIT_SUCCESS); } } int coeff_bs[1000] = {}; int num_bs = 0; int i,j,n; j = 0; for(i = 0; i < 1000; i++) { if(is_prime(i)) { coeff_bs[j++] = i; } } num_bs = j; int result = -1; int mn = 0; for(i = -999; i < 999; i++) { for(j = 0; j < num_bs; j++) { n = 0; while(is_prime(calc_quadratic(n, i, coeff_bs[j]))) { ++n; } if(n > mn) { mn = n; result = coeff_bs[j] * i; } } printf("."); } DUMPD(result); return 0; }
void MD_TCS230::begin() { #define SET_OUTPUT(x) if (x!=NO_PIN) pinMode(x,OUTPUT) SET_OUTPUT(_S0); SET_OUTPUT(_S1); SET_OUTPUT(_S2); SET_OUTPUT(_S3); SET_OUTPUT(_OE); setEnable(false); setFrequency2(_freqSet); #undef SET_OUTPUT DUMPS("\nLibrary begin initialised"); }
static bool check_dump(void) { DCHECK; DUMPC('a'); DUMPP(&check_dump); DUMPS(__FUNCTION__); DUMPD((uint64_t)(-1)); DUMPZ((uint64_t)(-1)); DUMP8((uint64_t)(-1)); DUMP16((uint64_t)(-1)); DUMP32((uint64_t)(-1)); DUMP64((uint64_t)(-1)); return true; }
bool MD_MIDIFile::isEOF(void) { bool bEof = true; // check if each track has finished for (uint8_t i=0; i<_trackCount && bEof; i++) { bEof = (_track[i].getEndOfTrack() && bEof); // breaks at first false } if (bEof) DUMPS("\n! EOF"); // if looping and all tracks done, reset to the start if (bEof && _looping) { restart(); bEof = false; } return(bEof); }
void MD_TCS230::setFilter(uint8_t f) // set the sensor color filter { if ((_S2 == NO_PIN) || (_S3 == NO_PIN)) return; DUMPS("\nsetFilter "); switch (f) { case TCS230_RGB_R: DUMPS("R"); digitalWrite(_S2, LOW); digitalWrite(_S3, LOW); break; case TCS230_RGB_G: DUMPS("G"); digitalWrite(_S2, HIGH); digitalWrite(_S3, HIGH); break; case TCS230_RGB_B: DUMPS("B"); digitalWrite(_S2, LOW); digitalWrite(_S3, HIGH); break; case TCS230_RGB_X: DUMPS("X"); digitalWrite(_S2, HIGH); digitalWrite(_S3, LOW); break; default: DUMPS("????"); break; } }
void MD_TCS230::setFrequency2(uint8_t f) // set the sensor frequency prescaler (internal function) { if ((_S0 == NO_PIN) || (_S0 == NO_PIN)) return; DUMPS("\nsetFrequency "); switch (f) { case TCS230_FREQ_HI: DUMPS("HI"); digitalWrite(_S0, HIGH); digitalWrite(_S1, HIGH); break; case TCS230_FREQ_MID: DUMPS("MID"); digitalWrite(_S0, HIGH); digitalWrite(_S1, LOW); break; case TCS230_FREQ_LO: DUMPS("LO"); digitalWrite(_S0, LOW); digitalWrite(_S1, HIGH); break; case TCS230_FREQ_OFF: DUMPS("OFF"); digitalWrite(_S0, LOW); digitalWrite(_S1, LOW); break; default: DUMPS("????"); break; } }
main(int argc, char **argv) { if (argc != 5) { fprintf(stderr, "usage: %s xreal ximag yreal yimag\n", argv[0]); exit(2); } ComplexNumber x(atof(argv[1]), atof(argv[2])); DUMPS("x is ..."); x.dump(); ComplexNumber y(atof(argv[3]), atof(argv[4])); DUMPS("y is ..."); y.dump(); ComplexNumber r(0.0, 0.0); DUMPS("r = x"); r = x; r.dump(); DUMPS("r = x + y"); r = x + y; r.dump(); DUMPS("r = x * y"); r = x * y; r.dump(); DUMPS("r = x / y"); r = x / y; r.dump(); DUMPS("r = x - y"); r = x - y; r.dump(); DUMPS("x == y"); if (x == y) DUMPS("x equals y"); else DUMPS("x NOT equals y"); DUMPS("x != y"); if (x != y) DUMPS("x NOT equals y"); else DUMPS("x equals y"); DUMPS("r == y"); r = y; if (r == y) DUMPS("r equals y"); else DUMPS("r NOT equals y"); DUMPS("r != y"); r = y; if (r != y) DUMPS("r NOT equals y"); else DUMPS("r equals y"); DUMPS("x < y"); if (x < y) DUMPS("x lt y"); else DUMPS("x NOT lt y"); DUMPS("x > y"); if (x > y) DUMPS("x gt y"); else DUMPS("x NOT gt y"); DUMPS("exp(x)"); r = exp(x); r.dump(); DUMPS("log(x)"); r = log(x); r.dump(); DUMPS("log10(x)"); r = log10(x); r.dump(); DUMPS("pow(x, y)"); r = pow(x, y); r.dump(); DUMPS("sqrt(x)"); r = sqrt(x); r.dump(); DUMPS("sin(x)"); r = sin(x); r.dump(); DUMPS("cos(x)"); r = cos(x); r.dump(); DUMPS("tan(x)"); r = tan(x); r.dump(); DUMPS("conj(x)"); r = conj(x); r.dump(); DUMPS("abs(x)"); r = abs(x); r.dump(); DUMPS("arg(x)"); r = arg(x); r.dump(); DUMPS("norm(x)"); r = norm(x); r.dump(); DUMPS("real(x)"); r = real(x); r.dump(); DUMPS("imag(x)"); r = imag(x); r.dump(); exit(0); }
void MD_MFTrack::parseEvent(MD_MIDIFile *mf) // process the event from the physical file { uint8_t eType; uint32_t eLen, mLen; sysex_event sev; // used for sysex callback function // now we have to process this event eType = mf->_fd.read(); switch (eType) { // ---------------------------- MIDI // midi_event = any MIDI channel message, including running status // Midi events (status bytes 0x8n - 0xEn) The standard Channel MIDI messages, where 'n' is the MIDI channel (0 - 15). // This status byte will be followed by 1 or 2 data bytes, as is usual for the particular MIDI message. // Any valid Channel MIDI message can be included in a MIDI file. case 0x80 ... 0xBf: // MIDI message with 2 parameters case 0xe0 ... 0xef: _mev.size = 3; _mev.data[0] = eType; _mev.channel = _mev.data[0] & 0xf; // mask off the channel _mev.data[0] = _mev.data[0] & 0xf0; // just the command byte _mev.data[1] = mf->_fd.read(); _mev.data[2] = mf->_fd.read(); DUMP("[MID2] Ch: ", _mev.channel); DUMPX(" Data: ", _mev.data[0]); DUMPX(" ", _mev.data[1]); DUMPX(" ", _mev.data[2]); #if !DUMP_DATA if (mf->_midiHandler != NULL) (mf->_midiHandler)(&_mev); #endif // !DUMP_DATA break; case 0xc0 ... 0xdf: // MIDI message with 1 parameter _mev.size = 2; _mev.data[0] = eType; _mev.channel = _mev.data[0] & 0xf; // mask off the channel _mev.data[0] = _mev.data[0] & 0xf0; // just the command byte _mev.data[1] = mf->_fd.read(); DUMP("[MID1] Ch: ", _mev.channel); DUMPX(" Data: ", _mev.data[0]); DUMPX(" ", _mev.data[1]); #if !DUMP_DATA if (mf->_midiHandler != NULL) (mf->_midiHandler)(&_mev); #endif break; case 0x00 ... 0x7f: // MIDI run on message { // If the first (status) byte is less than 128 (0x80), this implies that MIDI // running status is in effect, and that this byte is actually the first data byte // (the status carrying over from the previous MIDI event). // This can only be the case if the immediately previous event was also a MIDI event, // ie SysEx and Meta events clear running status. This means that the _mev structure // should contain the info from the previous message in the structure's channel member // and data[0] (for the MIDI command). // Hence start saving the data at byte data[1] with the byte we have just read (eType) // and use the size member to determine how large the message is (ie, same as before). _mev.data[1] = eType; for (uint8_t i = 2; i < _mev.size; i++) { _mev.data[i] = mf->_fd.read(); // next byte } DUMP("[MID+] Ch: ", _mev.channel); DUMPS(" Data:"); for (uint8_t i = 0; i<_mev.size; i++) { DUMPX(" ", _mev.data[i]); } #if !DUMP_DATA if (mf->_midiHandler != NULL) (mf->_midiHandler)(&_mev); #endif } break; // ---------------------------- SYSEX case 0xf0: // sysex_event = 0xF0 + <len:1> + <data_bytes> + 0xF7 case 0xf7: // sysex_event = 0xF7 + <len:1> + <data_bytes> + 0xF7 { uint8_t c, i; sysex_event sev; // collect all the bytes until the 0xf7 - boundaries are included in the message sev.track = _trackId; sev.data[0] = eType; sev.size = mf->_fd.read(); // The length parameter includes the 0xF7 but not the start boundary. // However, it may be bigger than our buffer will allow us to store. sev.size = (sev.size > BUF_SIZE(sev.data) - 2 ? BUF_SIZE(sev.data) - 2 : sev.size + 1); for (i = 1; (i < sev.size) && (c != 0xf7); i++) { c = mf->_fd.read(); // next char sev.data[i] = c; } // check if we had an overflow if (c != 0xf7) { while ((c = mf->_fd.read()) != 0xf7) ; // skip read all data sev.data[sev.size] = 0xf7; // terminate properly - data is probably nuked anyway } DUMPS("[SYSX] Data:"); for (uint8_t i = 0; i<sev.size; i++) { DUMPX(" ", sev.data[i]); } #if !DUMP_DATA if (mf->_sysexHandler != NULL) (mf->_sysexHandler)(&sev); #endif } break; // ---------------------------- META case 0xff: // meta_event = 0xFF + <meta_type:1> + <length:v> + <event_data_bytes> eType = mf->_fd.read(); mLen = readVarLen(&mf->_fd); DUMPX("[META] Type: 0x", eType); DUMP("\tLen: ", mLen); DUMPS("\t"); switch (eType) { case 0x2f: // End of track _endOfTrack = true; DUMPS("END OF TRACK"); break; case 0x51: // set Tempo - really the microseconds per tick mf->setMicrosecondPerQuarterNote(readMultiByte(&mf->_fd, MB_TRYTE)); DUMP("SET TEMPO to ", mf->getTickTime()); DUMP(" us/tick or ", mf->getTempo()); DUMPS(" beats/min"); break; case 0x58: // time signature mf->setTimeSignature(mf->_fd.read(), (1 << mf->_fd.read())); // denominator is 2^n mf->_fd.seekCur(mLen-2); DUMP("SET TIME SIGNATURE to ", mf->getTimeSignature()>>8); DUMP("/", mf->getTimeSignature()&0xf); break; #if SHOW_UNUSED_META case 0x0: // Sequence Number DUMP("SEQUENCE NUMBER ", readMultiByte(&mf->_fd, MB_WORD)); break; case 0x1: // Text DUMPS("TEXT "); for (int i=0; i<mLen; i++) DUMP("", (char)mf->_fd.read()); break; case 0x2: // Copyright Notice DUMPS("COPYRIGHT "); for (uint8_t i=0; i<mLen; i++) DUMP("", (char)mf->_fd.read()); break; case 0x3: // Sequence or Track Name DUMPS("SEQ/TRK NAME "); for (uint8_t i=0; i<mLen; i++) DUMP("", (char)mf->_fd.read()); break; case 0x4: // Instrument Name DUMPS("INSTRUMENT "); for (uint8_t i=0; i<mLen; i++) DUMP("", (char)mf->_fd.read()); break; case 0x5: // Lyric DUMPS("LYRIC "); for (uint8_t i=0; i<mLen; i++) DUMP("", (char)mf->_fd.read()); break; case 0x6: // Marker DUMPS("MARKER "); for (uint8_t i=0; i<mLen; i++) DUMP("", (char)mf->_fd.read()); break; case 0x7: // Cue Point DUMPS("CUE POINT "); for (uint8_t i=0; i<mLen; i++) DUMP("", (char)mf->_fd.read()); break; case 0x20:// Channel Prefix DUMP("CHANNEL PREFIX ", readMultiByte(&mf->_fd, MB_BYTE)); break; case 0x21:// Port Prefix DUMP("PORT PREFIX ", readMultiByte(&mf->_fd, MB_BYTE)); break; case 0x54:// SMPTE Offset DUMPS("SMPTE OFFSET"); for (uint8_t i=0; i<mLen; i++) { DUMP(" ", mf->_fd.read()); } break; case 0x59:// Key Signature DUMPS("KEY SIGNATURE"); for (uint8_t i=0; i<mLen; i++) { DUMP(" ", mf->_fd.read()); } break; case 0x7F: // Sequencer Specific Metadata DUMPS("SEQ SPECIFIC"); for (uint8_t i=0; i<mLen; i++) { DUMPX(" ", mf->_fd.read()); } break; #endif // SHOW_UNUSED_META default: mf->_fd.seekCur(mLen); DUMPS("IGNORED"); } break; // ---------------------------- UNKNOWN default: // stop playing this track as we cannot identify the eType _endOfTrack = true; DUMPX("[UKNOWN 0x", eType); DUMPS("] Track aborted"); } }