forked from pe1jpd/23cm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smeter.c
129 lines (102 loc) · 2.59 KB
/
smeter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <avr/io.h>
#include <stdio.h>
#include <avr/eeprom.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#include "23nbfm.h"
#include <util/delay.h>
extern int tx;
extern int squelchlevel;
extern int mode;
// define S-meter chars
unsigned char smeterChars[4][8] = { // wm, [3][8]
{0b00000,0b00000,0b10000,0b10000,0b10000,0b10000,0b00000,0b00000},
{0b00000,0b00000,0b10100,0b10100,0b10100,0b10100,0b00000,0b00000},
{0b00000,0b00000,0b10101,0b10101,0b10101,0b10101,0b00000,0b00000},
{0b00000,0b00000,0b00100,0b00100,0b00100,0b01110,0b00100,0b00000} // wm
};
void createSmeterChars()
{
// define custom chars for s-meter
int i, j;
lcdCmd(0x40);
for (i=0; i<4; i++) { // wm, i<3
for (j=0; j<8; j++) {
lcdData(smeterChars[i][j]);
}
}
}
int readRSSI()
{
register int rssi;
int rssi_;
ADCSRA |= (1<<ADSC)|(1<<ADEN);
while ((ADCSRA & (1<<ADSC))!=0); // wait for result of conversion
// calculate Smeter
rssi = (1024-ADC); // -44, RSSIoff moves RSSI
// if (rssi<0) rssi=0; // wm moved
// mute when tx or squelched
rssi_ = rssi-RSSIoff; // wm
if (rssi_<0) rssi_ = 0; // wm
if (tx || rssi_<squelchlevel || mode==SPECTRUM) { // wm
sbi(PORTC, MUTE);
}
else {
cbi(PORTC, MUTE);
// analog S output on Timer1, not implemented yet! // wm not used anymore
// OCR1B = rssi;
}
return rssi;
}
void displaySmeter(int rssi)
{
short n = 16;
int s = rssi-RSSIoff; // wm
if (s<0) s=0; // wm
#ifdef LCD_20x4 // wm
// goto fourth line on lcd
lcdCursor(0,3);
lcdStr("S-M:");
#else
// goto second line on lcd
lcdCursor(0,1);
#endif
// chars in the full bar are 3 vertical lines
while(s>=3 && n>0) {
lcdData(2);
s -= 3;
n--;
}
// last char 0, 1 or 2 lines
switch (s) {
case 2:
lcdData(1);
break;
case 1:
lcdData(0);
break;
default:
lcdData(' ');
break;
}
// clear any chars to the right (when tx, clear all)
n--; // wm, Line overflow to 0,0!
while (n-->0) lcdData(' ');
}
void displayRSSI(int rssi) // wm
{
int s = rssi-44; // wm, RSSIoff, "rssi-44" in dBm calibrated for original value
char str[20];
if (s<0) s=0; // wm
lcdCursor(0, 2);
if (s==0) {
lcdStr("RSSI: ");
}
else
{
s = 9835 - (173 * s); // all values*100 --> Integer
sprintf(str, "RSSI: -%d.%d dBm", (int)(s/100), (int)(s%100));
lcdCursor(0,2);
lcdStr(str);
}
}