Check-in [2b79db1dc3]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment: user: pine tags: trunk EDITED soft/JK20/jni/rda/RDA.c EDITED soft/JK20/jni/rda/RDA.java EDITED soft/JK20/src/Config.java EDITED soft/JK20/src/ConfigSaver.java EDITED soft/JK20/src/ControlMessage.java EDITED soft/JK20/src/JK20.java EDITED soft/JK20/src/view/StationListView.java EDITED soft/JK20/src/view/View.java
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | trunk
Files: files | file ages | folders
SHA1:2b79db1dc3ea7d5d411bec949e8906d0bc7a7776
User & Date: pine 2016-05-22 02:24:40
Context
2016-05-22
02:24
user: pine tags: trunk EDITED soft/JK20/jni/rda/RDA.c EDITED soft/JK20/jni/rda/RDA.java EDITED soft/JK20/src/Config.java EDITED soft/JK20/src/ConfigSaver.java EDITED soft/JK20/src/ControlMessage.java EDITED soft/JK20/src/JK20.java EDITED soft/JK20/src/view/StationListView.java EDITED soft/JK20/src/view/View.java Leaf check-in: 2b79db1dc3 user: pine tags: trunk
2016-05-21
03:29
user: pine tags: trunk EDITED soft/JK20/jni/rda/RDA.c EDITED soft/JK20/jni/rda/RDA.java EDITED soft/JK20/src/JK20.java EDITED soft/JK20/src/view/StationListView.java EDITED soft/firmware/lib/sys/uart.c EDITED soft/firmware/src/main.c DELETED soft/firmware/src/main_.c EDITED soft/firmware/src/u2s.c check-in: 20cda04cde user: pine tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to soft/JK20/jni/rda/RDA.c.

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdlib.h>
#include <stdint.h>
#include <libusb.h>
#include <RDA.h> 
#include <arpa/inet.h> /* for htons */
#include "../../../firmware/src/u2s_shared.h"

#define DEBUG_THIS

#define FATAL_ERROR_MESSAGE    "(E) JNI RDA"

#define TIMEOUT_MS   200

#define RDA_PACKAGE   "org/jk20/jni/rda/"
#define USB_EXCEPTION RDA_PACKAGE "USBException"







|







5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdlib.h>
#include <stdint.h>
#include <libusb.h>
#include <RDA.h> 
#include <arpa/inet.h> /* for htons */
#include "../../../firmware/src/u2s_shared.h"

//#define DEBUG_THIS

#define FATAL_ERROR_MESSAGE    "(E) JNI RDA"

#define TIMEOUT_MS   200

#define RDA_PACKAGE   "org/jk20/jni/rda/"
#define USB_EXCEPTION RDA_PACKAGE "USBException"

Changes to soft/JK20/jni/rda/RDA.java.

255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
...
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
...
531
532
533
534
535
536
537


































538
539
540
541
542
543
544
...
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
...
600
601
602
603
604
605
606












607
608
609
610
611
612
613
        regs[0x07] |= (wTH_SOFRBLEND & 0b11111 ) << 10;

        write();
    }
    /**
     * Take state of device (read data from device).
     */
    public void take() throws Exception {
        read();

        for (int i = READ_REG_START; i <  READ_REG_START + REG_NUMBER; i++)
            System.out.format("REG %02X %04X%n", i, (int)regs[i]);

        rREADCHAN = (char)((regs[0x0A] >>> 0 ) & 0b1111111111);
        rST       = (char)((regs[0x0A] >>> 10) & 0b1         );
................................................................................
        double space;
        double value;

        switch (wSPACE) {
            case wcSPACE_100_KHZ: space = 0.1;  break;
            case wcSPACE_200_KHZ: space = 0.2;  break;
            case wcSPACE_50_KHZ : space = 0.05; break;
            case wcSPACE_25_KHZ : space = 0.25; break;
            default:
                /* NOTREACHED */
                throw new StateInvalidException("Space has invalid value");
        }

        switch (wBAND) {
            case wcBAND_87_108_MHZ:
................................................................................
                /* NOTREACHED */
                throw new StateInvalidException("Band has invalid value");
        }

        wCHAN = (char)value;
        System.out.format("Set CHAN to %d%n", (int)wCHAN);
    }


































    /**
     * Softmute enable.
     */
    public void softMuteEnable() {
        wSOFTMUTE_EN = 1;
    }
    /**
................................................................................
        wSEEKTH = value;
    }

    /**
     * Get frequency of current channel
     * @return Frequency value in MHZ.
     */
    public double getChannel() throws ArgumentInvalidException, StateInvalidException {
        double space;
        double freq;

        switch (wSPACE) {
            case wcSPACE_100_KHZ: space = 0.1;  break;
            case wcSPACE_200_KHZ: space = 0.2;  break;
            case wcSPACE_50_KHZ : space = 0.05; break;
            case wcSPACE_25_KHZ : space = 0.25; break;
            default:
                /* NOTREACHED */
                throw new StateInvalidException("Space has invalid value");
        }

        switch (wBAND) {
            case wcBAND_87_108_MHZ:
................................................................................
                break;
            default:
                /* NOTREACHED */
                throw new StateInvalidException("Band has invalid value");
        }
        return freq;
    }












    /**
     * Get stereo indicator value.
     * @return true if stereo, false otherwise.
     */
    public boolean isStereo() {
        if (rST == 0)
            return false;







|







 







|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|







|







 







>
>
>
>
>
>
>
>
>
>
>
>







255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
...
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
...
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
...
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
...
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
        regs[0x07] |= (wTH_SOFRBLEND & 0b11111 ) << 10;

        write();
    }
    /**
     * Take state of device (read data from device).
     */
    public synchronized void take() throws Exception {
        read();

        for (int i = READ_REG_START; i <  READ_REG_START + REG_NUMBER; i++)
            System.out.format("REG %02X %04X%n", i, (int)regs[i]);

        rREADCHAN = (char)((regs[0x0A] >>> 0 ) & 0b1111111111);
        rST       = (char)((regs[0x0A] >>> 10) & 0b1         );
................................................................................
        double space;
        double value;

        switch (wSPACE) {
            case wcSPACE_100_KHZ: space = 0.1;  break;
            case wcSPACE_200_KHZ: space = 0.2;  break;
            case wcSPACE_50_KHZ : space = 0.05; break;
            case wcSPACE_25_KHZ : space = 0.025; break;
            default:
                /* NOTREACHED */
                throw new StateInvalidException("Space has invalid value");
        }

        switch (wBAND) {
            case wcBAND_87_108_MHZ:
................................................................................
                /* NOTREACHED */
                throw new StateInvalidException("Band has invalid value");
        }

        wCHAN = (char)value;
        System.out.format("Set CHAN to %d%n", (int)wCHAN);
    }
    /**
     *
     */
    public void setChannelExt(double freq) {
        try {
            if (freq >= 65 && freq <= 76)
                selectBand(wcBAND_65_76_MHZ);
            else if (freq >= 76 && freq <= 91)
                selectBand(wcBAND_76_91_MHZ);
            else if (freq >= 87 && freq <= 108)
                selectBand(wcBAND_87_108_MHZ);
            else
                throw new Exception("Invalid arguemnt");

            setChannelSpacing(wcSPACE_50_KHZ); 
            setChannel(freq);
        } catch (Exception e) {
            System.out.format("Invalid frequency %f, %s%n", freq, e);
        }
    }
    /**
     *
     */
    public void seekSpace(boolean up) {
        double space = 0;
        switch (wSPACE) {
            case wcSPACE_100_KHZ: space = 0.1;  break;
            case wcSPACE_200_KHZ: space = 0.2;  break;
            case wcSPACE_50_KHZ : space = 0.05; break;
            case wcSPACE_25_KHZ : space = 0.025; break;
        }
        setChannelExt(getChannelExt() + (up ? space : -space));
    }

    /**
     * Softmute enable.
     */
    public void softMuteEnable() {
        wSOFTMUTE_EN = 1;
    }
    /**
................................................................................
        wSEEKTH = value;
    }

    /**
     * Get frequency of current channel
     * @return Frequency value in MHZ.
     */
    public double getChannel() throws StateInvalidException {
        double space;
        double freq;

        switch (wSPACE) {
            case wcSPACE_100_KHZ: space = 0.1;  break;
            case wcSPACE_200_KHZ: space = 0.2;  break;
            case wcSPACE_50_KHZ : space = 0.05; break;
            case wcSPACE_25_KHZ : space = 0.025; break;
            default:
                /* NOTREACHED */
                throw new StateInvalidException("Space has invalid value");
        }

        switch (wBAND) {
            case wcBAND_87_108_MHZ:
................................................................................
                break;
            default:
                /* NOTREACHED */
                throw new StateInvalidException("Band has invalid value");
        }
        return freq;
    }
    /**
     *
     */
    public double getChannelExt() {
        try {
            return getChannel();
        } catch (Exception e) {
        }

        return 0;
    } 

    /**
     * Get stereo indicator value.
     * @return true if stereo, false otherwise.
     */
    public boolean isStereo() {
        if (rST == 0)
            return false;

Changes to soft/JK20/src/Config.java.

67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
...
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250

        windowWidth  = 640;
        windowHeight = 480;
    }
    /**
     *
     */
    public Config load() {
        Map<String, String> env = System.getenv();

        String homeEnv = env.get("HOME");
        if (homeEnv == null) {
            System.err.println("(E) Failed to get HOME environment variable");
            return this;
        }
................................................................................
                fgColor = new Color(value);
            }
        }
    }
    /**
     *
     */
    public void save() {
        try {
            writeXML();
        } catch (Exception e) {
            System.err.println("(E) Failed to write XML (" + configPath.toString() + "), " + e);
        }
    }
    /**







|







 







|







67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
...
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250

        windowWidth  = 640;
        windowHeight = 480;
    }
    /**
     *
     */
    public synchronized Config load() {
        Map<String, String> env = System.getenv();

        String homeEnv = env.get("HOME");
        if (homeEnv == null) {
            System.err.println("(E) Failed to get HOME environment variable");
            return this;
        }
................................................................................
                fgColor = new Color(value);
            }
        }
    }
    /**
     *
     */
    public synchronized void save() {
        try {
            writeXML();
        } catch (Exception e) {
            System.err.println("(E) Failed to write XML (" + configPath.toString() + "), " + e);
        }
    }
    /**

Changes to soft/JK20/src/ConfigSaver.java.

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38





39
40
41
42
43
44
45
..
74
75
76
77
78
79
80
81
82
83
84
85
86
        this.config = config;
    }
    /**
     *
     */
    public ConfigSaver start() {
        thread = new Thread(this);
        thread.setDaemon(true);
        thread.start();

        return this;
    }
    /**
     *
     */
    public void terminate() {
        thread.interrupt();





    }
    @Override
    public void run() {
        boolean interrupt = false;

        synchronized (this) {
            saveTime = Instant.now();
................................................................................
        save = true;
    }
    /**
     *
     */
    private void saveProceed() {
        System.out.println(getClass().getName() + " saveProceed");
        synchronized (config) {
            config.save();
        }
    }
}








|









>
>
>
>
>







 







<
|
<



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
..
79
80
81
82
83
84
85

86

87
88
89
        this.config = config;
    }
    /**
     *
     */
    public ConfigSaver start() {
        thread = new Thread(this);
//        thread.setDaemon(true);
        thread.start();

        return this;
    }
    /**
     *
     */
    public void terminate() {
        thread.interrupt();
        try {
            thread.join();
        } catch (Exception e) {
            System.out.println("Interrupted, " + e);
        }
    }
    @Override
    public void run() {
        boolean interrupt = false;

        synchronized (this) {
            saveTime = Instant.now();
................................................................................
        save = true;
    }
    /**
     *
     */
    private void saveProceed() {
        System.out.println(getClass().getName() + " saveProceed");

        config.save();

    }
}

Changes to soft/JK20/src/ControlMessage.java.

5
6
7
8
9
10
11
12





13
14
15
16
17
18
19

/**
 *
 */
public class ControlMessage {
    public enum Id {
        SWITCH_TO_STATION,
        SAVE_CONFIG,





    }
    public Id id;
    public Object object;
    /**
     *
     */
    public ControlMessage(Id id, Object object) {







|
>
>
>
>
>







5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

/**
 *
 */
public class ControlMessage {
    public enum Id {
        SWITCH_TO_STATION,
        TIMER,
        SEEK_UP,
        SEEK_DOWN,
        SEEK_SPACE_UP,
        SEEK_SPACE_DOWN,
        TUNE,
    }
    public Id id;
    public Object object;
    /**
     *
     */
    public ControlMessage(Id id, Object object) {

Changes to soft/JK20/src/JK20.java.

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

26
27
28
29
30
31
32
..
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
...
114
115
116
117
118
119
120
121


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
...
172
173
174
175
176
177
178
179

180
181

182










183












184
185





186




















187












188
189
190
191
192
193
194
...
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218









219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237

238




















































import org.jk20.view.View;
import org.jk20.jni.rda.RDA;

/**
 *
 */
public class JK20 {
    private static RDA rda;
    private static JK20 jk20;
    public static Config config;
    private static ConfigSaver configSaver;
    public static View view;
    private static boolean terminate = false;
    private final int MESSAGE_QUEUE_CAPACITY = 16;
    private static BlockingQueue<ControlMessage> messageQueue;
    private static Thread jk20Thread;

    /**
     *
     */
    public static void main(String[] args) {
        new JK20().start(args);
    }
    /**
................................................................................
        jk20 = this;
        jk20Thread = Thread.currentThread();

        messageQueue = new ArrayBlockingQueue<ControlMessage>(MESSAGE_QUEUE_CAPACITY);

        config = new Config().load();
        configSaver = new ConfigSaver(config).start();
//        SwingUtilities.invokeLater(() -> {
//            view = new View().make();
//        });

        rda = new RDA();
        try {
            rda.init();
        } catch (Exception e) {
            System.out.println("RDA init error: " + e);
            terminate();
            return;
        }







        rda.powerEnable();
        rda.audioHighZDisable();
        try {
//            rda.selectBand(RDA.wcBAND_65_76_MHZ); 
            rda.selectBand(RDA.wcBAND_76_108_MHZ);
            

//            rda.setChannel(67.22);
            rda.setChannel(88.5);
//            rda.setChannel(105.4);
//            rda.setChannel(102.5);
//            rda.setChannel(106.6);
        } catch (Exception e) {
            System.out.println("RDA invalid argument: " + e);
        }
        rda.muteDisable();
        rda.softMuteDisable();

        rda.setVolume(RDA.wcVOLUME_MAX * 1 / 5);

        rda.tuneEnable();
//        rda.setSeekUp();
//        rda.seekEnable();
//        rda.setSeekModeWrap();
//        rda.setSeekModeStop();

//        /* apply current settings */
//        try {
//            rda.apply();
//            System.out.format("freq %f%n", rda.getChannel());
//        } catch (Exception e) {
//            System.out.println("RDA apply error: " + e);
//        }

        boolean first = true;
        while (!needTerminate()) {
            if (first) {
                first = false;
            } else {
                rda.close();
................................................................................
            try {
                rda.apply();
            } catch (Exception e) {
                System.out.println("RDA apply error: " + e);
                continue;
            }

            while (true) {


                try {
                    rda.take();
                    System.out.format("freq %f%n", rda.getChannel());
                } catch (Exception e) {
                    System.out.println("RDA take error: " + e);
                    break;
                }


                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e2) {
                    System.out.println(getClass().getSimpleName() + " interrupted, " + e2);
                    terminate = true;
                    break;
                }


            }


//            messageQueue.clear();
//            while (!needTerminate()) {
//                ControlMessage message;
//                try {
//                    message = messageQueue.take();
//                } catch (InterruptedException e) {
//                    System.out.println(getClass().getSimpleName() + " interrupted, " + e);
//                    terminate = true;
//                    break;
//                }
//                processMessage(message);
//            }
//
//            /* show "---.- MHz" if connection to device was lost */
//            view.setFrequencyIndicator(0);
        }
        terminate();
    }
    /**
     *
     */
    private boolean needTerminate() {
................................................................................
            terminate = true;
            jk20Thread.interrupt();
        }
    }
    /**
     *
     */
    private void processMessage(ControlMessage message) {

        switch (message.id) {
            case SWITCH_TO_STATION:

                System.out.format("Switch to station: %.2f MHz%n", (Double)message.object);










                break;












            case SAVE_CONFIG:
                configSaver.save();





                break;




















        }












    }
    /**
     * Put message to queue of control thread
     */
    public void putMessage(ControlMessage.Id id) {
        putMessage(new ControlMessage(id));
    }
................................................................................
        putMessage(new ControlMessage(id, object));
    }
    /**
     * Put message to queue of control thread
     */
    public void putMessage(ControlMessage message) {
        if (!messageQueue.offer(message)) {
            System.out.println("(W) " + getClass().getName() + ", queue overflow");
//            try {
//                messageQueue.put(message);
//            } catch (InterruptedException e) {};
        }
    }
    /**
     *
     */
    public void terminate() {
        System.out.println(getClass().getName() + " terminate");

        if (rda != null) {









            rda.close();
            rda.destroy();
        }
        configSaver.terminate();
        System.exit(0);
    }
//    /**
//     *
//     */
//    private boolean sleep() {
//
//    }
    /**
     *
     */
    public static JK20 getInstance() {
        return jk20;
    }
}





























































|








>







 







<
<
<









>
>
>
>
>
>



|
<
<
<
<
<
|
<
<
<
<
<



>

<

<
<
<
<
<
<
<
<
<
<
<
<







 







|
>
>

|
<
<
<
<
<
<
<
<
<
|
|



>
>


<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|







 







|
>


>

>
>
>
>
>
>
>
>
>
>

>
>
>
>
>
>
>
>
>
>
>
>
|
<
>
>
>
>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>







 







|
<
<
<









>
>
>
>
>
>
>
>
>






<
<
<
<
<
<






|
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
..
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
..
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
...
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171

172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
...
222
223
224
225
226
227
228
229



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253






254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import org.jk20.view.View;
import org.jk20.jni.rda.RDA;

/**
 *
 */
public class JK20 {
    public static RDA rda;
    private static JK20 jk20;
    public static Config config;
    private static ConfigSaver configSaver;
    public static View view;
    private static boolean terminate = false;
    private final int MESSAGE_QUEUE_CAPACITY = 16;
    private static BlockingQueue<ControlMessage> messageQueue;
    private static Thread jk20Thread;
    private static MessageTimer timer1s;
    /**
     *
     */
    public static void main(String[] args) {
        new JK20().start(args);
    }
    /**
................................................................................
        jk20 = this;
        jk20Thread = Thread.currentThread();

        messageQueue = new ArrayBlockingQueue<ControlMessage>(MESSAGE_QUEUE_CAPACITY);

        config = new Config().load();
        configSaver = new ConfigSaver(config).start();




        rda = new RDA();
        try {
            rda.init();
        } catch (Exception e) {
            System.out.println("RDA init error: " + e);
            terminate();
            return;
        }

        new MessageTimer(1000).exec();

        SwingUtilities.invokeLater(() -> {
            view = new View().make();
        });

        rda.powerEnable();
        rda.audioHighZDisable();
        synchronized (config) {





            rda.setChannelExt(config.currentStation);





        }
        rda.muteDisable();
        rda.softMuteDisable();
        rda.setSeekModeStop();
        rda.setVolume(RDA.wcVOLUME_MAX * 1 / 5);

        rda.tuneEnable();













        boolean first = true;
        while (!needTerminate()) {
            if (first) {
                first = false;
            } else {
                rda.close();
................................................................................
            try {
                rda.apply();
            } catch (Exception e) {
                System.out.println("RDA apply error: " + e);
                continue;
            }

            messageQueue.clear();
            while (!needTerminate()) {
                ControlMessage message;
                try {
                    message = messageQueue.take();









                } catch (InterruptedException e) {
                    System.out.println(getClass().getSimpleName() + " interrupted, " + e);
                    terminate = true;
                    break;
                }
                if (!processMessage(message))
                    break;
            }















            /* show "---.- MHz" if connection to device was lost */
            view.setFrequencyIndicator(0);
        }
        terminate();
    }
    /**
     *
     */
    private boolean needTerminate() {
................................................................................
            terminate = true;
            jk20Thread.interrupt();
        }
    }
    /**
     *
     */
    private boolean processMessage(ControlMessage message) {
        boolean apply = false;
        switch (message.id) {
            case SWITCH_TO_STATION:
                double freq = (Double)message.object;
                System.out.format("Switch to station: %.2f MHz%n", (Double)message.object);
                rda.setChannelExt(freq);
                rda.seekDisable();
                rda.tuneEnable();

                synchronized (config) {
                    config.currentStation = freq;
                }

                configSaver.save();
                apply = true;
                break;
            case SEEK_SPACE_UP:
                rda.seekDisable();
                rda.tuneEnable();
                rda.seekSpace(true);
                apply = true;
                break;
            case SEEK_SPACE_DOWN:
                rda.seekDisable();
                rda.tuneEnable();
                rda.seekSpace(false);
                apply = true;
                break;
            case SEEK_UP:

                rda.setChannelExt(rda.getChannelExt());
                rda.tuneDisable();
                rda.setSeekUp();
                rda.seekEnable();
                apply = true;
                break;
            case SEEK_DOWN:
                rda.setChannelExt(rda.getChannelExt());
                rda.tuneDisable();
                rda.setSeekDown();
                rda.seekEnable();
                apply = true;
                break;
            case TUNE:
                rda.setChannelExt(rda.getChannelExt());
                rda.seekDisable();
                rda.tuneEnable();
                apply = true;
                break;
            case TIMER:
                try {
                    rda.take();
                    view.drawRDAState(rda);
                } catch (Exception e) {
                    System.out.println("RDA take error: " + e);
                    return false;
                }
                break;
        }

        if (apply) {
            try {
                rda.apply();
            } catch (Exception e) {
                System.out.println("RDA apply error: " + e);
                return false;
            }
        }
        return true;
    }
    /**
     * Put message to queue of control thread
     */
    public void putMessage(ControlMessage.Id id) {
        putMessage(new ControlMessage(id));
    }
................................................................................
        putMessage(new ControlMessage(id, object));
    }
    /**
     * Put message to queue of control thread
     */
    public void putMessage(ControlMessage message) {
        if (!messageQueue.offer(message)) {
//            System.out.println("(W) " + getClass().getName() + ", queue overflow");



        }
    }
    /**
     *
     */
    public void terminate() {
        System.out.println(getClass().getName() + " terminate");

        if (rda != null) {
            try {
                rda.muteEnable();
                rda.powerDisable();
                rda.audioHighZEnable();
                rda.apply();
            } catch (Exception e) {
                System.out.println("RDA invalid argument: " + e);
            }

            rda.close();
            rda.destroy();
        }
        configSaver.terminate();
        System.exit(0);
    }






    /**
     *
     */
    public static JK20 getInstance() {
        return jk20;
    }

    /**
     *
     */
    private class MessageTimer implements Runnable {
        private int ms;
        /**
         *
         */
        public MessageTimer(int ms) {
            this.ms = ms;
        }
        /**
         *
         */
        public void exec() {
            Thread thread = new Thread(this);
            thread.setDaemon(true);
            thread.start();
        }
        /**
         *
         */
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(ms);
                    putMessage(ControlMessage.Id.TIMER, this);
                } catch (Exception e) {}
            }
        }
    }
}

//            while (true) {
//                try {
//                    rda.take();
//                    System.out.format("freq %f%n", rda.getChannel());
//                } catch (Exception e) {
//                    System.out.println("RDA take error: " + e);
//                    break;
//                }
//
//                try {
//                    Thread.sleep(1000);
//                } catch (InterruptedException e2) {
//                    System.out.println(getClass().getSimpleName() + " interrupted, " + e2);
//                    terminate = true;
//                    break;
//                }
//            }



Changes to soft/JK20/src/view/StationListView.java.

83
84
85
86
87
88
89



90



91
92
93
94
95
96
97
        synchronized (config) {
            for (Config.Station station: config.stationList) {
                if (config.currentStation.equals(station.value)) {
                    table.setRowSelectionInterval(0, config.stationList.indexOf(station));
                    return;
                }
            }



            table.clearSelection();



        }
    }
    /**
     *
     */
    public JTable getComponent() {
        return table;







>
>
>
|
>
>
>







83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
        synchronized (config) {
            for (Config.Station station: config.stationList) {
                if (config.currentStation.equals(station.value)) {
                    table.setRowSelectionInterval(0, config.stationList.indexOf(station));
                    return;
                }
            }
            if (table.getSelectedRowCount() > 0)
            {
                try {
                    table.clearSelection();
                } catch (Exception e) {
                }
            }
        }
    }
    /**
     *
     */
    public JTable getComponent() {
        return table;

Changes to soft/JK20/src/view/View.java.

16
17
18
19
20
21
22

23
24
25
26
27
28
29
..
32
33
34
35
36
37
38

39
40
41
42
43
44
45
..
96
97
98
99
100
101
102


103
104
105
106
107
108
109
...
111
112
113
114
115
116
117

118
119
120
121
122
123
124
...
202
203
204
205
206
207
208
209



210
211
212
213






































214
215
216
217
218
219
220
...
265
266
267
268
269
270
271



























272
273
274
275
276
277
278
import javax.swing.ScrollPaneConstants;
import javax.swing.ImageIcon;
/* */
import org.jk20.JK20;
import org.jk20.ControlMessage;
import org.jk20.Config;
import org.jk20.Version;


/**
 *
 */
public class View extends JFrame {
    private final int PADDING = 4;
    private JPanel mainPanel;
................................................................................
    private JScrollPane listScrollPane;
    private StationListView stationListView;
    private LevelView rssiLevel;
    private ImageIcon[] redBulb;
    private ImageIcon[] greenBulb;

    private JLabel freqIndicator;

    private JLabel stereoIndicator;
    private JLabel seekIndicator;
    private JLabel stationIndicator;
    
    /**
     *
     */
................................................................................
//            rssiLevel = new LevelView();
//            rssiLevel.setMinimumSize(new Dimension(sideWidth, 12));
//            rssiLevel.setMaximumSize(new Dimension(sideWidth, 64));
//            rssiLevel.setPreferredSize(new Dimension(sideWidth, 24));

            freqIndicator = new JLabel();
            freqIndicator.setBorder(makePadding());



            JPanel freqFrame = new JPanel();
            freqFrame.setBorder(BorderFactory.createLineBorder(Color.GRAY, PADDING));
            freqFrame.setAlignmentX(LEFT_ALIGNMENT);
            runLater(() -> {
                final int height = freqIndicator.getFont().getSize() * 2 + 8;
                freqFrame.setPreferredSize(new Dimension(freqFrame.getWidth(), height));
................................................................................
                freqFrame.setMinimumSize  (new Dimension(freqFrame.getWidth(), height));
            });
//            freqFrame.setBackground(Color.BLUE);
            freqFrame.setBackground(new Color(0x25, 0x20, 0x05));
            freqFrame.setOpaque(true);

            freqFrame.add(freqIndicator);


            stereoIndicator   = new JLabel("Stereo" , JLabel.LEFT);
            stationIndicator  = new JLabel("Station", JLabel.LEFT);
            seekIndicator     = new JLabel("Seek"   , JLabel.LEFT);

            setStereoIndicator(false);
            setStationIndicator(false);
................................................................................

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                JK20.getInstance().doTerminate();
            }
        });
        



        setContentPane(mainPanel);        
        setVisible(true);
        return this;
    }






































    /**
     *
     */
    public void setStereoIndicator(boolean stereo) {
        runLater(() -> {
            Icon icon = greenBulb[stereo ? 1 : 0];
            if (icon != stereoIndicator.getIcon()) {
................................................................................
                sval = "---.-- MHz";
            else
                sval = String.format(Locale.US, "%03.2f MHz", value);

            freqIndicator.setText(sval);
        });
    }




























    /**
     *
     */
    private Border makePadding() {
        return BorderFactory.createEmptyBorder(PADDING, PADDING, PADDING, PADDING);
    }







>







 







>







 







>
>







 







>







 







|
>
>
>




>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
..
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
..
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
...
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
...
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
...
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import javax.swing.ScrollPaneConstants;
import javax.swing.ImageIcon;
/* */
import org.jk20.JK20;
import org.jk20.ControlMessage;
import org.jk20.Config;
import org.jk20.Version;
import org.jk20.jni.rda.RDA;

/**
 *
 */
public class View extends JFrame {
    private final int PADDING = 4;
    private JPanel mainPanel;
................................................................................
    private JScrollPane listScrollPane;
    private StationListView stationListView;
    private LevelView rssiLevel;
    private ImageIcon[] redBulb;
    private ImageIcon[] greenBulb;

    private JLabel freqIndicator;
    private JLabel bandIndicator;
    private JLabel stereoIndicator;
    private JLabel seekIndicator;
    private JLabel stationIndicator;
    
    /**
     *
     */
................................................................................
//            rssiLevel = new LevelView();
//            rssiLevel.setMinimumSize(new Dimension(sideWidth, 12));
//            rssiLevel.setMaximumSize(new Dimension(sideWidth, 64));
//            rssiLevel.setPreferredSize(new Dimension(sideWidth, 24));

            freqIndicator = new JLabel();
            freqIndicator.setBorder(makePadding());
            bandIndicator = new JLabel();
            bandIndicator.setBorder(makePadding());

            JPanel freqFrame = new JPanel();
            freqFrame.setBorder(BorderFactory.createLineBorder(Color.GRAY, PADDING));
            freqFrame.setAlignmentX(LEFT_ALIGNMENT);
            runLater(() -> {
                final int height = freqIndicator.getFont().getSize() * 2 + 8;
                freqFrame.setPreferredSize(new Dimension(freqFrame.getWidth(), height));
................................................................................
                freqFrame.setMinimumSize  (new Dimension(freqFrame.getWidth(), height));
            });
//            freqFrame.setBackground(Color.BLUE);
            freqFrame.setBackground(new Color(0x25, 0x20, 0x05));
            freqFrame.setOpaque(true);

            freqFrame.add(freqIndicator);
            freqFrame.add(bandIndicator);

            stereoIndicator   = new JLabel("Stereo" , JLabel.LEFT);
            stationIndicator  = new JLabel("Station", JLabel.LEFT);
            seekIndicator     = new JLabel("Seek"   , JLabel.LEFT);

            setStereoIndicator(false);
            setStationIndicator(false);
................................................................................

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                JK20.getInstance().doTerminate();
            }
        });

        KeyboardFocusManager.getCurrentKeyboardFocusManager().
            addKeyEventDispatcher(new ViewKeyDispatcher()); 
        
        setContentPane(mainPanel);        
        setVisible(true);
        return this;
    }
    private class ViewKeyDispatcher implements KeyEventDispatcher {
        @Override
        public boolean dispatchKeyEvent(KeyEvent e) {
            if (e.getID() == KeyEvent.KEY_PRESSED) {
                switch(e.getKeyCode()) {
                    case KeyEvent.VK_L:
                        if ((e.getModifiers() & KeyEvent.SHIFT_MASK) != 0)
                            JK20.getInstance().putMessage(ControlMessage.Id.SEEK_SPACE_UP);
                        else
                            JK20.getInstance().putMessage(ControlMessage.Id.SEEK_UP);
                        return true;
                    case KeyEvent.VK_H:
                        if ((e.getModifiers() & KeyEvent.SHIFT_MASK) != 0)
                            JK20.getInstance().putMessage(ControlMessage.Id.SEEK_SPACE_DOWN);
                        else
                            JK20.getInstance().putMessage(ControlMessage.Id.SEEK_DOWN);
                        return true;
                    case KeyEvent.VK_B:
                        /* TODO select BAND */
                        return true;
                    case KeyEvent.VK_T:
                        JK20.getInstance().putMessage(ControlMessage.Id.TUNE);
                        return true;
                    default:
                        return true;
                }
            }
            switch (e.getKeyCode()) {
                case KeyEvent.VK_UP:
                case KeyEvent.VK_DOWN:
                case KeyEvent.VK_PAGE_UP:
                case KeyEvent.VK_PAGE_DOWN:
                    return false;
            }
            return true;
        }
    }

    /**
     *
     */
    public void setStereoIndicator(boolean stereo) {
        runLater(() -> {
            Icon icon = greenBulb[stereo ? 1 : 0];
            if (icon != stereoIndicator.getIcon()) {
................................................................................
                sval = "---.-- MHz";
            else
                sval = String.format(Locale.US, "%03.2f MHz", value);

            freqIndicator.setText(sval);
        });
    }
    /**
     *
     */
    public void drawRDAState(RDA rda) {
        runLater(() -> {
            double channel;

            synchronized (rda) {
                try {
                    channel = rda.getChannel();
                } catch (Exception e) {
                    System.err.println("RDA invalid state, " + e);
                    return;
                }
                setFrequencyIndicator(channel);
                setStereoIndicator(rda.isStereo());
                setStationIndicator(rda.isChannelStation());
                setSeekIndicator(rda.isSeekFail(), rda.isSeekComplete());
            }

            Config config = JK20.getInstance().config;
            synchronized (config) {
                config.currentStation = channel;
            }
            stationListView.selectCurrent();
        });
    }

    /**
     *
     */
    private Border makePadding() {
        return BorderFactory.createEmptyBorder(PADDING, PADDING, PADDING, PADDING);
    }