Skip to content

Commit dea56f2

Browse files
committed
Better ExtIO handling in same thread
1 parent 8be9273 commit dea56f2

6 files changed

Lines changed: 65 additions & 228 deletions

File tree

TSDRPlugin_ExtIO/makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
PLUGNAME=TSDRPlugin_ExtIO
44

55
# Dependencies
6-
OBJS=$(PLUGNAME).o ExtIOPluginLoader.o threading.o
7-
DEPS=TSDRPlugin.h TSDRCodes.h ExtIOPluginLoader.h osdetect.h errors.h threading.h
6+
OBJS=$(PLUGNAME).o ExtIOPluginLoader.o
7+
DEPS=TSDRPlugin.h TSDRCodes.h ExtIOPluginLoader.h osdetect.h errors.h
88

99
# Flags
1010
CFLAGS+=-O3

TSDRPlugin_ExtIO/src/ExtIOPluginLoader.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,9 @@ int extio_load(extiosource_t * plugin, const char *dlname)
4949
// mandatory functions that rtlsdr expects
5050
if ((plugin->GetHWSR = extio_getfunction(plugin, "GetHWSR")) == 0) return TSDR_ERR_PLUGIN;
5151

52+
// completely optional functions
53+
if ((plugin->SetAttenuator = extio_getfunction(plugin, "SetAttenuator")) == 0) plugin->SetAttenuator = NULL;
54+
if ((plugin->GetAttenuators = extio_getfunction(plugin, "GetAttenuators")) == 0) plugin->GetAttenuators = NULL;
55+
5256
return TSDR_OK;
5357
}

TSDRPlugin_ExtIO/src/ExtIOPluginLoader.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
// mandatory functions that tsdrrequires
3232
long (__stdcall * GetHWSR) (void);
3333

34+
// completely optional functions
35+
int (__stdcall * SetAttenuator) ( int atten_idx );
36+
int (__stdcall * GetAttenuators) ( int atten_idx, float * attenuation );
3437

3538
} typedef extiosource_t;
3639

TSDRPlugin_ExtIO/src/TSDRPlugin_ExtIO.c

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include "TSDRPlugin.h"
66
#include "TSDRCodes.h"
77
#include "ExtIOPluginLoader.h"
8-
#include "threading.h"
98

109
#include "errors.h"
1110

@@ -14,15 +13,27 @@
1413
#define EXTIO_HWTYPE_32B 6
1514
#define EXTIO_HWTYPE_FLOAT 7
1615

17-
mutex_t * running = NULL;
16+
void thread_sleep(uint32_t milliseconds) {
17+
#if WINHEAD
18+
Sleep(milliseconds);
19+
#else
20+
usleep(1000 * milliseconds);
21+
#endif
22+
}
23+
1824
extiosource_t * source = NULL;
19-
uint32_t init_freq = 100000000;
2025
int hwtype;
2126
tsdrplugin_readasync_function tsdr_cb;
2227
void * tsdr_ctx;
2328
volatile int is_running = 0;
2429
float * outbuf = NULL;
2530
int outbuf_size = 0;
31+
int max_att_id = -1;
32+
33+
uint32_t req_freq = 100000000;
34+
uint32_t act_freq = -1;
35+
float req_gain = 0.5;
36+
float act_gain = -1;
2637

2738
void closeextio(void) {
2839
if (source == NULL) return;
@@ -47,27 +58,20 @@ uint32_t tsdrplugin_getsamplerate() {
4758
}
4859

4960
int tsdrplugin_setbasefreq(uint32_t freq) {
50-
51-
init_freq = freq;
52-
if (source != NULL)
53-
source -> SetHWLO(freq);
61+
req_freq = freq;
5462

5563
RETURN_OK();
5664
}
5765

5866
int tsdrplugin_stop(void) {
59-
60-
if (source != NULL) {
61-
is_running = 0;
62-
source->StopHW();
63-
if (running != NULL) mutex_signal(running);
64-
}
67+
is_running = 0;
6568

6669
RETURN_OK();
6770
}
6871

6972
int tsdrplugin_setgain(float gain) {
70-
// TODO! can we really set the gain?
73+
req_gain = gain;
74+
7175
RETURN_OK();
7276
}
7377

@@ -124,11 +128,6 @@ int tsdrplugin_init(const char * params) {
124128
outbuf_size = 1;
125129
}
126130

127-
if (running == NULL) {
128-
running = (mutex_t *) malloc(sizeof(mutex_t));
129-
mutex_init(running);
130-
}
131-
132131
// if an extio was already initialized before, now change
133132
if (source != NULL)
134133
closeextio();
@@ -147,9 +146,16 @@ int tsdrplugin_init(const char * params) {
147146
RETURN_EXCEPTION("The sample format of the ExtIO plugin is not supported.", TSDR_CANNOT_OPEN_DEVICE);
148147
}
149148

150-
if (source->OpenHW())
151-
RETURN_OK()
152-
else {
149+
if (source->OpenHW()) {
150+
// list attenuators
151+
if (source->GetAttenuators != NULL) {
152+
max_att_id = 0;
153+
float att;
154+
while (source->GetAttenuators(max_att_id++,&att) == 0) {};
155+
}
156+
157+
RETURN_OK();
158+
} else {
153159
closeextio();
154160
RETURN_EXCEPTION("The ExtIO driver failed to open a device. Make sure your device is plugged in and its drivers are installed correctly.", TSDR_CANNOT_OPEN_DEVICE);
155161
}
@@ -164,6 +170,15 @@ int tsdrplugin_init(const char * params) {
164170
}
165171
}
166172

173+
void attenuate(float gain) {
174+
if (max_att_id > 0 && source->SetAttenuator != NULL) {
175+
int att_id = gain * max_att_id;
176+
if (att_id >= max_att_id) att_id = max_att_id - 1;
177+
if (att_id < 0) att_id = 0;
178+
source->SetAttenuator(att_id);
179+
}
180+
}
181+
167182
int tsdrplugin_readasync(tsdrplugin_readasync_function cb, void *ctx) {
168183
if (source == NULL)
169184
RETURN_EXCEPTION("Please provide a full path to a valid ExtIO dll.", TSDR_PLUGIN_PARAMETERS_WRONG);
@@ -173,22 +188,32 @@ int tsdrplugin_readasync(tsdrplugin_readasync_function cb, void *ctx) {
173188
tsdr_ctx = ctx;
174189
is_running = 1;
175190

176-
source -> StartHW(init_freq);
191+
act_freq = req_freq;
192+
source -> StartHW(act_freq);
193+
act_gain = req_gain;
194+
attenuate(act_gain);
177195

178-
while (is_running)
179-
mutex_wait(running);
196+
while (is_running) {
197+
thread_sleep(50);
198+
199+
if (req_freq != act_freq) {
200+
act_freq = req_freq;
201+
source -> SetHWLO(act_freq);
202+
}
203+
204+
if (req_gain != act_gain) {
205+
act_gain = req_gain;
206+
attenuate(act_gain);
207+
}
208+
}
209+
210+
source->StopHW();
180211

181212
RETURN_OK();
182213
}
183214

184215
void tsdrplugin_cleanup(void) {
185216

186-
if (running != NULL) {
187-
mutex_free(running);
188-
free (running);
189-
running = NULL;
190-
}
191-
192217
if (outbuf != NULL) {
193218
free (outbuf);
194219
outbuf = NULL;

TSDRPlugin_ExtIO/src/threading.c

Lines changed: 0 additions & 160 deletions
This file was deleted.

TSDRPlugin_ExtIO/src/threading.h

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)