예제 #1
0
mir_sdr_ErrT SDRPlay::setGR(int gainReduction, bool absolute, bool syncUpdate)
{
	return mir_sdr_SetGr(gainReduction, absolute?1:0, syncUpdate?1:0);
}
예제 #2
0
int TSDRPLUGIN_API __stdcall tsdrplugin_readasync(tsdrplugin_readasync_function cb, void *ctx) {
    working = 1;

    int err, sps, grc, rfc, fsc, i, id;
    unsigned int fs;
    unsigned int frame = 0;
    int inited = 0 ;

    double freq = desiredfreq;
    double gainred = desiredgainred;
    err = mir_sdr_Init(gainred, 8, freq / 1000000.0, mir_sdr_BW_8_000, mir_sdr_IF_Zero, &sps);
    if (err != 0) {
        mir_sdr_Uninit();
        RETURN_EXCEPTION("Can't access the Mirics SDR dongle. Make sure it is properly plugged in, drivers are installed and the mir_sdr_api.dll is in the executable's folder and try again. Please, refer to the TSDRPlugin_Miricis readme file for more information.", TSDR_CANNOT_OPEN_DEVICE);
    } else
        inited = 1;

    short * xi = (short *)malloc(sps * SAMPLES_TO_PROCESS_AT_ONCE * sizeof(short));
    short * xq = (short *)malloc(sps * SAMPLES_TO_PROCESS_AT_ONCE * sizeof(short));

    int outbufsize =  2 * sps * SAMPLES_TO_PROCESS_AT_ONCE;
    float * outbuf = (float *) malloc(sizeof(float) * outbufsize);

    while (working) {
        unsigned int dropped = 0;

        for (id = 0; id < SAMPLES_TO_PROCESS_AT_ONCE; id++) {
            err = mir_sdr_ReadPacket(&xi[id*sps], &xq[id*sps], &fs, &grc, &rfc, &fsc);
            if (fs > frame)
                dropped += fs - frame;
//			else if (fs < frame) {
//				// wrapped around
//				dropped += 2147483647 - frame;
//				dropped += fs;
//			}
            frame = fs + sps;
            if (err != 0) break;
        }

        // if desired frequency is different and valid
        if (err == 0 && freq != desiredfreq && !(desiredfreq < 60e6 || (desiredfreq > 245e6 && desiredfreq < 420e6) || desiredfreq > 1000e6)) {

            err = mir_sdr_SetRf(desiredfreq, 1, 0);
            if (err == 0)
                freq = desiredfreq; // if OK, frequency has been changed
            else if (err == mir_sdr_RfUpdateError) {
                // if an error occurs, try to reset the device
                if (mir_sdr_Uninit() == 0) inited = 0;
                gainred = desiredgainred;
                if ((err = mir_sdr_Init(gainred, 8, desiredfreq / 1000000.0, mir_sdr_BW_8_000, mir_sdr_IF_Zero, &sps))==0) inited = 1;
                if (err == mir_sdr_OutOfRange) {
                    // if the desired frequency is outside operational range, go back to the working frequency
                    if (mir_sdr_Uninit() == 0) inited = 0;
                    if (mir_sdr_Init(gainred, 8, freq / 1000000.0, mir_sdr_BW_8_000, mir_sdr_IF_Zero, &sps) == 0) inited = 1;
                    desiredfreq = freq;
                    err = 0;
                } else if (err == 0)
                    freq = desiredfreq;
            } else if (err == mir_sdr_OutOfRange) {
                mir_sdr_SetRf(freq, 1, 0);
                desiredfreq = freq;
                err = 0;
            }
        }

        if (err == 0 && gainred != desiredgainred) {
            gainred = desiredgainred;
            mir_sdr_SetGr(gainred, 1, 0);
        }

        if (err == 0)
            for (i = 0; i < outbufsize; i++) {
                const short val = (i & 1) ? (xq[i >> 1]) : (xi[i >> 1]);
                outbuf[i] = val / 32767.0;
            }

        if (err != 0)
        {
            working = 0;
            break;
        }
        else
            cb(outbuf, outbufsize, ctx, dropped);
    }



    free(outbuf);
    free(xi);
    free(xq);

    if (inited && mir_sdr_Uninit() != 0) RETURN_EXCEPTION("Cannot properly close the Mirics USB dongle. If you experience any further issues, please disconnect it and reconnect it again.", TSDR_CANNOT_OPEN_DEVICE);

    RETURN_EXCEPTION("The Mirics SDR dongle stopped responding.", (err == 0) ? TSDR_OK : TSDR_ERR_PLUGIN);
}