Skip to content

Commit

Permalink
merge pga and digital gain, integrate showing manual gain into power …
Browse files Browse the repository at this point in the history
…display
  • Loading branch information
edy555 committed Jan 18, 2018
1 parent e211696 commit f41e411
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 91 deletions.
76 changes: 47 additions & 29 deletions display.c
Expand Up @@ -589,7 +589,8 @@ typedef struct {

// update_flag
#define FLAG_SPDISP (1<<0)
#define FLAG_UI (1<<1)
#define FLAG_POWER (1<<1)
#define FLAG_UI (1<<2)


spectrumdisplay_t spdispinfo;
Expand Down Expand Up @@ -1142,6 +1143,24 @@ draw_channel_freq(void)
#define FG_VOLUME 0xfffe
#define FG_MOD 0xffe0


void
draw_db(int db, int x, int y, int16_t fg, int16_t bg)
{
char str[10];
ili9341_drawfont(10, &NF20x24, x+80-5, y, fg, bg); // period
int d = db >> 8;
if (d < 0 && (db & 0xff))
d++;
itoap(d, str, 4, ' ');
ili9341_drawfont_string(str, &NF20x24, x, y, fg, bg);
if (db < 0) db = 0x100 - (db & 0xff);
itoap(((db & 0xff) * 10) >> 8, str, 1, ' ');
ili9341_drawfont_string(str, &NF20x24, x+100-10, y, fg, bg);
x += 120-10;
ili9341_drawfont(13, &NF20x24, x, y, fg, bg); // dB
}

void
draw_info(void)
{
Expand Down Expand Up @@ -1170,37 +1189,25 @@ draw_info(void)
x += 48+4;

bg = uistat.mode == RFGAIN ? BG_ACTIVE : BG_NORMAL;
if (uistat.agcmode) {
ili9341_drawfont(10, &NF20x24, x+80-2, y, 0xffff, bg); // period
itoap(measured_power_dbm >> 8, str, 4, ' ');
ili9341_drawfont_string(str, &NF20x24, x, y, 0xffff, bg);
itoap(((measured_power_dbm & 0xff) * 10) >> 8, str, 1, ' ');
ili9341_drawfont_string(str, &NF20x24, x+100-4, y, 0xffff, bg);
x += 120-4;
ili9341_drawfont(13, &NF20x24, x, y, 0xffff, bg); // dB
} else {
if (uistat.dgain == 0) {
ili9341_drawfont(15, &NF20x24, x, y, 0x07ff, bg); // ANT Mark
x += 20;
itoap(uistat.rfgain / 2, str, 3, ' ');
strcat(str, " ");
ili9341_drawfont_string(str, &NF20x24, x, y, 0x07ff, bg);
x += 60;
ili9341_drawfont(13, &NF20x24, x, y, 0x07ff, bg); // dB
x += 20;
} else {
ili9341_drawfont(15, &NF20x24, x, y, 0x070f, bg);
x += 20;
itoap(uistat.dgain / 2, str, 3, ' ');
strcat(str, " ");
ili9341_drawfont_string(str, &NF20x24, x, y, 0x070f, bg);
x += 60;
ili9341_drawfont(13, &NF20x24, x, y, 0x070f, bg);
x += 20;
}
if (!uistat.agcmode) {
uint16_t fg = 0x07ff;
if (uistat.rfgain < 0 || uistat.rfgain >= 96)
fg = 0x070f;
draw_db(uistat.rfgain << 7, x, y, fg, bg);
ili9341_drawfont(15, &NF20x24, x, y, fg, bg); // ANT Mark
}
}

void
draw_power(void)
{
int x = 184;
int y = 48;
if (uistat.agcmode != 0) {
draw_db(measured_power_dbm, x, y, 0xffff, 0x0000);
}
}

void
clear_background(void)
{
Expand Down Expand Up @@ -1230,6 +1237,11 @@ disp_process(void)
draw_info();
spdispinfo.update_flag &= ~FLAG_UI;
}

if (spdispinfo.update_flag & FLAG_POWER) {
draw_power();
spdispinfo.update_flag &= ~FLAG_POWER;
}
}

void
Expand All @@ -1238,6 +1250,12 @@ disp_update(void)
spdispinfo.update_flag |= FLAG_UI;
}

void
disp_update_power(void)
{
spdispinfo.update_flag |= FLAG_POWER;
}

void
disp_init(void)
{
Expand Down
29 changes: 10 additions & 19 deletions main.c
Expand Up @@ -47,7 +47,7 @@ static __attribute__((noreturn)) THD_FUNCTION(Thread1, arg)

calc_stat();
measure_power_dbm();
disp_update();
disp_update_power();

if (++count == 10) {
stat.fps = stat.fps_count;
Expand Down Expand Up @@ -132,7 +132,8 @@ int16_t mode_freq_offset = AM_FREQ_OFFSET;
int32_t center_frequency;

tlv320aic3204_agc_config_t agc_config = {
.target_level = 6
.target_level = 6,
.maximum_gain = 127
};

static signal_process_func_t demod_funcs[] = {
Expand Down Expand Up @@ -264,37 +265,23 @@ static void
calc_stat(void)
{
int16_t *p = &rx_buffer[0];
int64_t acc0, acc1;
int32_t ave0, ave1;
int16_t min0 = 0, min1 = 0;
int16_t max0 = 0, max1 = 0;
int32_t count = AUDIO_BUFFER_LEN;
int i;
acc0 = acc1 = 0;
float accx0 = 0, accx1 = 0;
for (i = 0; i < AUDIO_BUFFER_LEN*2; i += 2) {
acc0 += p[i];
acc1 += p[i+1];
if (min0 > p[i]) min0 = p[i];
if (min1 > p[i+1]) min1 = p[i+1];
if (max0 < p[i]) max0 = p[i];
if (max1 < p[i+1]) max1 = p[i+1];
}
ave0 = acc0 / count;
ave1 = acc1 / count;
acc0 = acc1 = 0;
float accx0 = 0, accx1 = 0;
for (i = 0; i < AUDIO_BUFFER_LEN*2; i += 2) {
float x0 = p[i] - ave0;
float x1 = p[i+1] - ave1;
float x0 = p[i];
float x1 = p[i+1];
accx0 += x0 * x0;
accx1 += x1 * x1;
//acc0 += ((int32_t)p[i] - ave0)*((int32_t)p[i] - ave0);
//acc1 += ((int32_t)p[i+1] - ave1)*((int32_t)p[i+1] - ave1);
}
stat.rms[0] = sqrtf(accx0 / count);
stat.rms[1] = sqrtf(accx1 / count);
stat.ave[0] = ave0;
stat.ave[1] = ave1;
stat.min[0] = min0;
stat.min[1] = min1;
stat.max[0] = max0;
Expand Down Expand Up @@ -416,6 +403,7 @@ static void cmd_agc(BaseSequentialStream *chp, int argc, char *argv[])
chprintf(chp, "\thysteresis {0-3}\r\n");
chprintf(chp, "\tattack {0-31} [scale:0-7]\r\n");
chprintf(chp, "\tdecay {0-31} [scale:0-7]\r\n");
chprintf(chp, "\tmaxgain {0-116}\r\n");
return;
}

Expand All @@ -440,6 +428,9 @@ static void cmd_agc(BaseSequentialStream *chp, int argc, char *argv[])
if (argc >= 3)
agc_config.decay_scale = atoi(argv[2]);
tlv320aic3204_agc_config(&agc_config);
} else if (strncmp(cmd, "ma", 2) == 0 && argc >= 2) {
agc_config.maximum_gain = atoi(argv[1]);
tlv320aic3204_agc_config(&agc_config);
}
}

Expand Down
6 changes: 3 additions & 3 deletions nanosdr.h
Expand Up @@ -34,6 +34,7 @@ typedef struct {
int attack_scale;
int decay;
int decay_scale;
int maximum_gain;
} tlv320aic3204_agc_config_t;

extern void tlv320aic3204_init(void);
Expand Down Expand Up @@ -148,6 +149,7 @@ void disp_init(void);
void disp_process(void);
void disp_fetch_samples(void);
void disp_update(void);
void disp_update_power(void);

void set_window_function(int wf_type);

Expand All @@ -174,8 +176,7 @@ extern void set_modulation(modulation_t mod);
typedef struct {
uint32_t freq;
modulation_t modulation;
int8_t rfgain;
int8_t dgain;
int16_t rfgain;
} setting_t;

typedef struct {
Expand All @@ -186,7 +187,6 @@ typedef struct {
uint32_t freq;
modulation_t modulation;
int8_t rfgain;
int8_t dgain;

enum { AGC_MANUAL, AGC_SLOW, AGC_MID, AGC_FAST } agcmode;
int8_t digit; /* 0~5 */
Expand Down
4 changes: 4 additions & 0 deletions tlv320aic3204.c
Expand Up @@ -162,6 +162,10 @@ void tlv320aic3204_agc_config(tlv320aic3204_agc_config_t *conf)
ctrl = ((conf->decay & 0x1f) << 3) | (conf->decay_scale & 0x7);
I2CWrite(AIC3204_ADDR, 0x5a, ctrl); /* Left AGC Decay Time */
I2CWrite(AIC3204_ADDR, 0x62, ctrl); /* Right AGC Decay Time */

ctrl = conf->maximum_gain;
I2CWrite(AIC3204_ADDR, 0x58, ctrl); /* Left AGC Maximum Gain */
I2CWrite(AIC3204_ADDR, 0x60, ctrl); /* Right AGC Maximum Gain */
}

// implement HPF of first order IIR
Expand Down
81 changes: 41 additions & 40 deletions ui.c
Expand Up @@ -26,33 +26,29 @@
#include <string.h>

#define set_volume(gain) tlv320aic3204_set_volume(gain)
#define set_gain(gain) tlv320aic3204_set_gain(gain)
#define set_dgain(gain) tlv320aic3204_set_digital_gain(gain)
#define set_agc(mode) set_agc_mode(mode)


int fetch_encoder_tick(void);

#define CHANNEL_MAX 18

const setting_t channel_table[CHANNEL_MAX] = {
{ 567000, MOD_AM, 20*2, 0 },
{ 747000, MOD_AM, 15*2, 0 },
{ 1287000, MOD_AM, 20*2, 0 },
{ 1440000, MOD_AM, 20*2, 0 },
{ 7100000, MOD_LSB, 20*2, 0 },
{ 14100000, MOD_USB, 20*2, 0 },
{ 21100000, MOD_USB, 25*2, 0 },
{ 28500000, MOD_USB, 25*2, 0 },
{ 34930000, MOD_FM, 20*2, 0 },
{ 2932000, MOD_USB, 20*2, 0 },
{ 5628000, MOD_USB, 20*2, 0 },
{ 6655000, MOD_USB, 20*2, 0 },
{ 8951000, MOD_USB, 20*2, 0 },
{ 10048000, MOD_USB, 20*2, 5*2 },
{ 11330000, MOD_USB, 20*2, 5*2 },
{ 13273000, MOD_USB, 20*2, 10*2 },
{ 17904000, MOD_USB, 20*2, 10*2 }
{ 567000, MOD_AM, 20*2 },
{ 747000, MOD_AM, 15*2 },
{ 1287000, MOD_AM, 20*2 },
{ 1440000, MOD_AM, 20*2 },
{ 7100000, MOD_LSB, 20*2 },
{ 14100000, MOD_USB, 20*2 },
{ 21100000, MOD_USB, 25*2 },
{ 28500000, MOD_USB, 25*2 },
{ 34930000, MOD_FM, 20*2 },
{ 2932000, MOD_USB, 20*2 },
{ 5628000, MOD_USB, 20*2 },
{ 6655000, MOD_USB, 20*2 },
{ 8951000, MOD_USB, 20*2 },
{ 10048000, MOD_USB, 20*2 },
{ 11330000, MOD_USB, 20*2 },
{ 13273000, MOD_USB, 20*2 },
{ 17904000, MOD_USB, 20*2 }
};

#define NO_EVENT 0
Expand Down Expand Up @@ -136,6 +132,25 @@ int16_t agc_slowness_table[AGCMODE_MAX] = {
-1, 10, 5, 1
};


static void
set_gain(int gain)
{
int dgain = 0;
if (gain > RFGAIN_MAX) {
dgain = gain - RFGAIN_MAX;
gain = RFGAIN_MAX;
}
if (gain < 0) {
dgain = gain;
gain = 0;
}

tlv320aic3204_set_gain(gain);
tlv320aic3204_set_digital_gain(dgain);
}


void
update_frequency(void)
{
Expand Down Expand Up @@ -230,15 +245,13 @@ ui_init(void)
uistat.modulation = MOD_AM;
uistat.volume = 10;
uistat.rfgain = 60; // 0 ~ 95
uistat.dgain = 0; // -24 ~ 40
//uistat.agcmode = AGC_MANUAL;
uistat.agcmode = AGC_MID;
//ui_update();

set_volume(uistat.volume);
set_gain(uistat.rfgain);
set_dgain(uistat.dgain);
set_agc(uistat.agcmode);
set_agc_mode(uistat.agcmode);
update_frequency();
}

Expand Down Expand Up @@ -284,11 +297,13 @@ ui_process(void)
} else {
if (tick < 0) {
uistat.mode--;
// skip rfgain if agc is enabled
if (uistat.agcmode != 0 && uistat.mode == RFGAIN)
uistat.mode--;
}
if (tick > 0) {
uistat.mode++;
// skip rfgain if agc is enabled
if (uistat.agcmode != 0 && uistat.mode == RFGAIN)
uistat.mode++;
}
Expand All @@ -302,10 +317,8 @@ ui_process(void)
// apply settings at channel
uistat.freq = channel_table[uistat.channel].freq;
uistat.rfgain = channel_table[uistat.channel].rfgain;
uistat.dgain = channel_table[uistat.channel].dgain;
uistat.modulation = channel_table[uistat.channel].modulation;
set_gain(uistat.rfgain);
set_dgain(uistat.dgain);
set_modulation(uistat.modulation);
update_frequency();
} else if (uistat.mode == VOLUME) {
Expand All @@ -320,22 +333,11 @@ ui_process(void)
uistat.freq = freq;
update_frequency();
} else if (uistat.mode == RFGAIN) {
int g = uistat.rfgain + uistat.dgain + tick;
if (g > RFGAIN_MAX) {
uistat.rfgain = RFGAIN_MAX;
uistat.dgain = minmax(g - RFGAIN_MAX, -24, 41);
} else if (g >= 0) {
uistat.rfgain = g;
uistat.dgain = 0;
} else {
uistat.rfgain = 0;
uistat.dgain = minmax(g, -24, 41);
}
uistat.rfgain = minmax(uistat.rfgain + tick, -24, RFGAIN_MAX + 40);
set_gain(uistat.rfgain);
set_dgain(uistat.dgain);
} else if (uistat.mode == AGC) {
uistat.agcmode = minmax(uistat.agcmode + tick, 0, 4);
set_agc(uistat.agcmode);
set_agc_mode(uistat.agcmode);
} else if (uistat.mode == MOD) {
if (tick > 0 && uistat.modulation < MOD_MAX-1) {
uistat.modulation++;
Expand All @@ -352,7 +354,6 @@ ui_process(void)
}
}

//ui_update();
disp_update();
}
}
Expand Down

0 comments on commit f41e411

Please sign in to comment.