diff options
Diffstat (limited to 'map65/libm65')
| -rw-r--r-- | map65/libm65/CMakeLists.txt | 27 | ||||
| -rw-r--r-- | map65/libm65/decode0.f90 | 2 | ||||
| -rw-r--r-- | map65/libm65/decode1a.f90 | 30 | ||||
| -rw-r--r-- | map65/libm65/display.f90 | 283 | ||||
| -rw-r--r-- | map65/libm65/filbig.f90 | 20 | ||||
| -rw-r--r-- | map65/libm65/ftninit.f90 | 1 | ||||
| -rw-r--r-- | map65/libm65/m65_mod.f90 | 21 | ||||
| -rw-r--r-- | map65/libm65/map65a.f90 | 105 | ||||
| -rw-r--r-- | map65/libm65/message_history_mod.f90 | 109 | ||||
| -rw-r--r-- | map65/libm65/npar_ptrs_mod.f90 | 59 | ||||
| -rw-r--r-- | map65/libm65/ptt.c | 138 | ||||
| -rw-r--r-- | map65/libm65/ptt.h | 19 | ||||
| -rw-r--r-- | map65/libm65/ptt_unix.c | 185 | ||||
| -rw-r--r-- | map65/libm65/q65b.F90 | 120 | ||||
| -rw-r--r-- | map65/libm65/tmoonsub.c | 2 |
15 files changed, 649 insertions, 472 deletions
diff --git a/map65/libm65/CMakeLists.txt b/map65/libm65/CMakeLists.txt index c0361b44..9ae45c54 100644 --- a/map65/libm65/CMakeLists.txt +++ b/map65/libm65/CMakeLists.txt @@ -5,6 +5,7 @@ set (libm65_FSRCS decodes_mod.f90 extract_shared_mod.f90 gran_interface.f90 + message_history_mod.f90 m65_mod.f90 map65_polarization_score.f90 four2a_legacy_wrap.f90 @@ -209,6 +210,8 @@ wsjt_link_common_fortran (m65impl) if (WSJT_ENABLE_TESTS) add_subdirectory (${CMAKE_SOURCE_DIR}/tests/unit/map65/decode_history ${CMAKE_CURRENT_BINARY_DIR}/decode_history) + add_subdirectory (${CMAKE_SOURCE_DIR}/tests/unit/map65/messages_retention + ${CMAKE_CURRENT_BINARY_DIR}/messages_retention) add_executable (test_map65_deep_search_cache ${CMAKE_SOURCE_DIR}/tests/unit/map65/test_map65_deep_search_cache.f90) @@ -240,6 +243,16 @@ if (WSJT_ENABLE_TESTS) add_test (NAME test_map65_jt65_handoff COMMAND test_map65_jt65_handoff) set_tests_properties (test_map65_jt65_handoff PROPERTIES TIMEOUT 180) + add_executable (test_map65_filbig_boundary + ${CMAKE_SOURCE_DIR}/tests/unit/map65/test_map65_filbig_boundary.f90) + target_include_directories (test_map65_filbig_boundary PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) + target_link_libraries (test_map65_filbig_boundary PRIVATE m65impl) + foreach (rate IN ITEMS 96000 192000) + add_test (NAME test_map65_filbig_boundary_${rate} COMMAND test_map65_filbig_boundary ${rate}) + set_tests_properties (test_map65_filbig_boundary_${rate} PROPERTIES + PASS_REGULAR_EXPRESSION "MAP65 filter boundary tests passed" TIMEOUT 30) + endforeach () + add_executable (test_q65_waveform_generator ${CMAKE_SOURCE_DIR}/tests/unit/q65/test_q65_waveform_generator.f90) target_include_directories (test_q65_waveform_generator PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) @@ -314,6 +327,20 @@ if (WSJT_ENABLE_TESTS) RUN_SERIAL TRUE TIMEOUT 180) + add_executable (test_map65_symspec_contract + ${CMAKE_SOURCE_DIR}/tests/unit/map65/test_map65_symspec_contract.f90) + target_include_directories (test_map65_symspec_contract PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) + target_link_libraries (test_map65_symspec_contract PRIVATE m65impl) + add_test (NAME test_map65_symspec_96 COMMAND test_map65_symspec_contract 96000) + add_test (NAME test_map65_symspec_192 COMMAND test_map65_symspec_contract 192000) + + add_executable (test_map65_q65_handoff + ${CMAKE_SOURCE_DIR}/tests/unit/map65/test_map65_q65_handoff.f90) + target_include_directories (test_map65_q65_handoff PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) + target_link_libraries (test_map65_q65_handoff PRIVATE m65impl) + add_test (NAME test_map65_q65_handoff COMMAND test_map65_q65_handoff) + set_tests_properties (test_map65_q65_handoff PROPERTIES TIMEOUT 180) + endif () if (WIN32 AND CMAKE_SIZEOF_VOID_P EQUAL 4) diff --git a/map65/libm65/decode0.f90 b/map65/libm65/decode0.f90 index 26a90366..cdb8799b 100644 --- a/map65/libm65/decode0.f90 +++ b/map65/libm65/decode0.f90 @@ -60,7 +60,7 @@ contains ' nhsym0=' // itoa(nhsym0) // ' nagain=' // itoa(nagain)) endif - nkeep = 20 + nkeep = min(max(ntimeout, 10), 40) call sec0(0, tquick) call timer('decode0 ', 0) diff --git a/map65/libm65/decode1a.f90 b/map65/libm65/decode1a.f90 index 691593fd..e55a760b 100644 --- a/map65/libm65/decode1a.f90 +++ b/map65/libm65/decode1a.f90 @@ -1,7 +1,23 @@ module decode1a_mod + use iso_fortran_env, only: real64 implicit none + + real(real64), parameter :: jt65_demod_sample_rate = 1378.125_real64 + integer, parameter :: jt65_fft_size = 512 + integer, parameter :: jt65c_center_bin_offset = 132 + real(real64), parameter :: jt65c_center_offset = & + jt65c_center_bin_offset * jt65_demod_sample_rate / jt65_fft_size contains +pure integer function jt65_demod_bin(mode65, tone) result(bin) + integer, intent(in) :: mode65, tone + + bin = tone + if(mode65.eq.2) bin=2*tone-1 + if(mode65.eq.4) bin=modulo(4*(tone-1)-jt65c_center_bin_offset, & + jt65_fft_size)+1 +end function jt65_demod_bin + subroutine decode1a(dd,newdat,f0,nflip,mode65,nfsample,xpol, & mycall,hiscall,hisgrid,neme,ndepth,nqd,dphi,ndphi, & nutc,nkhz,ndf,ipol,ntol,sync2,a,dt,pol,nkv,nhist,nsum,nsave, & @@ -145,6 +161,16 @@ call afc65b(c5x(i0), c5y(i0), nz, fsample, nflip, ipol, xpol, ndphi, a, ccfbest, sq0=aa*aa*sqa + bb*bb*sqb sync2=3.7*ccfbest/sq0 + ! AFC follows the sync-centered stream; JT65C message tones need the + ! separately centered stream to keep its full 700 Hz bandwidth. + if(mode65.eq.4) then + call timer('filbig ',0) + call filbig(dd, nsmax_active, f0+jt65c_center_offset, newdat, & + nfsample, xpol, cx, cy, n5) + call timer('filbig ',1) + if(xpol) cy(:n5)=z*cy(:n5) + endif + ! TEMP diagnostic 2026-09-10 for the freq/time-sync-vs-false-decode investigation. call dbg('decode1a: afc65b fit at t=' // rtoa(sec_midn()) // & ' f0=' // rtoa(real(f0)) // ' dt00=' // rtoa(dt00) // ' dtbest=' // rtoa(dtbest) // & @@ -185,9 +211,7 @@ call afc65b(c5x(i0), c5y(i0), nz, fsample, nflip, ipol, xpol, ndphi, a, ccfbest, if(n.eq.1) then do i=1,66 ! s2(i,k)=real(c5a(i))**2 + aimag(c5a(i))**2 - jj=i - if(mode65.eq.2) jj=2*i-1 - if(mode65.eq.4) jj=4*i-3 + jj=jt65_demod_bin(mode65,i) s2(i,k)=real(c5a(jj))**2 + aimag(c5a(jj))**2 enddo else diff --git a/map65/libm65/display.f90 b/map65/libm65/display.f90 index 901313c8..182384de 100644 --- a/map65/libm65/display.f90 +++ b/map65/libm65/display.f90 @@ -2,255 +2,116 @@ module display_mod implicit none contains -subroutine display(nkeep, ftol) +subroutine display(nkeep, ftol, nutc) use stdout_channel_mod, only: write_stdout - use iso_fortran_env, only: real64 + use message_history_mod, only: message_record, expire_messages, recent_messages use indexx_mod, only: indexx - use debug_log, only: dbg, itoa, rtoa - use sec_midn_mod, only: sec_midn implicit none ! Arguments integer, intent(in) :: nkeep real, intent(in) :: ftol + integer, intent(in) :: nutc - ! Parameters - integer, parameter :: MAXLINES = 400 - integer, parameter :: MX = 400 - integer, parameter :: MAXCALLS = 500 + ! A busy band may retain more history than the Messages window can display. + integer, parameter :: MAXLINES = 800 - ! Locals - integer :: indx(MAXLINES), indx2(MX) - character(len=83) :: line(MAXLINES), line2(MX), line3(MAXLINES) + ! Keep large buffers off the decoder thread's stack. + integer, save :: indx(MAXLINES), group_order(MAXLINES), call_order(MAXLINES) + character(len=83), save :: line(MAXLINES) character(len=63) :: out, out0 - character(len=3) :: cfreq0 - character(len=6) :: callsign, callsign0 - ! N6NU 2026-05-24: bumped 12 ? 18 to carry the 5-char ndf (signed - ! Hz offset within kHz) alongside cfreq0 in the "&" bandmap line. - ! The C++ overlay handler (mainwindow.cpp processStdOut) reads - ! both fields and places the tick at nkHz + ndf/1000 instead of - ! integer kHz, which was up to �500 Hz off on signals with - ! non-zero ndf. - character(len=18) :: freqcall(MAXCALLS) - - real :: freqkHz(MAXLINES) - integer :: utc(MAXLINES), utc2(MX), utcz - real(real64) :: f0 + ! 12 characters hold the longest compound calls (e.g. ER/EA8DBM, up to 11) + ! plus a trailing space. + character(len=12) :: callsign, seen_calls(MAXLINES) + ! Then the record's mode (# JT65, : Q65) and UTC (hhmm), so the GUI can + ! color the waterfall callsign label and age it from the call's latest + ! decode rather than from each redisplay. + character(len=28) :: freqcall(MAXLINES) + + real, save :: freqkHz(MAXLINES), call_freqkHz(MAXLINES) + type(message_record), allocatable :: records(:) + integer :: now character(len=83) :: livecq2, livecq3 character(len=128) :: linenew character(len=64) :: linenew2 - integer :: io_status - integer :: ndf, nh, nm - integer :: i, j, j0, k, k3, kz + integer :: i, j, group_first, group_last, group_size integer :: nz, nage, iage, nquad - integer :: i0, i1, i2, len, nc, m, nstart + integer :: i1, i2, len, nc ! Initialize some scalars/arrays defensively out0 = ' ' - cfreq0 = ' ' - callsign0 = ' ' freqcall = ' ' - ! TEMP diagnostic 2026-09-10 for the "decode reaches map65_rx.log but not - ! the Messages window" investigation -- this is the entry point for the - ! ENTIRE "@"/"&" (Messages/Band Map window) pipeline; nothing gets to - ! those windows except through here. - call dbg('display: ENTRY at t=' // rtoa(sec_midn()) // ' nkeep=' // itoa(nkeep)) - - !------------------ Read and filter valid lines --------------------- - rewind(26) - nz = 0 - - do i = 1, MAXLINES - read(26,1010,end=10) line(i) -1010 format(a83) - read(line(i),1020,iostat=io_status) f0, ndf, nh, nm -1020 format(f8.3,i5,25x,i3,i2) - - if (io_status /= 0) cycle - - utc(i) = 60*nh + nm - freqkHz(i) = 1000.d0*(f0 - 144.d0) + 0.001d0*ndf - enddo - -10 backspace(26) - nz = i -1 - call dbg('display: read from unit 26, raw record count nz=' // itoa(nz)) - if (nz >= 1) utcz = utc(nz) - nz=nz-1 + call expire_messages(nutc, nkeep) + call recent_messages(MAXLINES, records) + call write_stdout('<Map65DisplayBegin>'//new_line('a')) + nz = size(records) if (nz < 1) then - call dbg('display: EARLY RETURN, nz<1 after decrement -- no records to emit') return endif + + now = 60*(nutc/100) + mod(nutc, 100) nquad = max(nkeep/4, 3) do i = 1, nz - nage = utcz - utc(i) - if (nage < 0) nage = nage + 1440 + line(i) = records(i)%line + freqkHz(i) = records(i)%frequency_khz + nage = modulo(now - records(i)%utc, 1440) iage = nage / nquad write(line(i)(79:80),1021) iage 1021 format(i2) enddo - - nage = utcz - utc(1) - if (nage < 0) nage = nage + 1440 - if (nage > nkeep) then - do i = 1, nz - nage = utcz - utc(i) - if (nage < 0) nage = nage + 1440 - if (nage <= nkeep) go to 20 - enddo -20 i0 = i - nz = nz - i0 + 1 - rewind(26) - if (nz < 1) return - do i = 1, nz - j = i + i0 - 1 - line(i) = line(j) - utc(i) = utc(j) - freqkHz(i) = freqkHz(j) - write(26,1022) line(i) -1022 format(a83) - enddo - ! Rewriting fewer records than the file previously held does not - ! shrink it -- the old, longer tail stays on disk past this point. - ! Without truncating here, a later rewind+read pass reads straight - ! through into that stale leftover data and resurrects entries this - ! trim was supposed to drop for good, causing unbounded growth of - ! the Messages window over a long session regardless of nkeep. - ! endfile leaves the file positioned after the EOF marker it just - ! wrote, and Fortran forbids further sequential I/O from there - ! without repositioning first -- backspace moves back onto that - ! marker so the next append (map65a.f90/q65b.F90) can write there. - endfile(26) - backspace(26) - endif - - flush(26) call indexx(freqkHz, nz, indx) - nstart = 1 - k3 = 0 - k = 1 - m = indx(1) + group_first = 1 + do while (group_first <= nz) + group_last = group_first + do while (group_last < nz) + if (freqkHz(indx(group_last+1)) - freqkHz(indx(group_last)) > 2.0*ftol) exit + group_last = group_last + 1 + enddo - if (m < 1 .or. m > MAXLINES) then - write(linenew,'(A,1X,I0,1X,I0)') 'Error in display.f90:', nz, m - call write_stdout(trim(linenew)//new_line('a')) - m = 1 - endif - line2(1) = line(m) - utc2(1) = utc(m) - do i = 2, nz - j0 = indx(i-1) - j = indx(i) - if (freqkHz(j) - freqkHz(j0) > 2.0*ftol) then - if (nstart == 0) then - k = k + 1 - line2(k) = "" - utc2(k) = -1 - endif - kz = k - if(nstart.eq.1) then - call indexx(float(utc2(1:kz)), kz, indx2) - k3=0 - do k = 1, kz - k3 = min(k3+1, MAXLINES) - line3(k3) = line2(indx2(k)) - enddo - nstart = 0 - else - call indexx(float(utc2(1:kz)),kz,indx2) - do k=1,kz - k3=min(k3+1,400) - line3(k3)=line2(indx2(k)) - enddo - endif - k= 0 - endif - if (i == nz) then - k = k + 1 - line2(k) = "" - utc2(k) = -1 - endif - k = k + 1 - line2(k) = line(j) - utc2(k) = utc(j) - j0=j - enddo - kz = k - call indexx(float(utc2(1:kz)), kz, indx2) - do k = 1, kz - k3 = min(k3+1, MAXLINES) - line3(k3) = line2(indx2(k)) + group_size = group_last - group_first + 1 + call indexx(real(indx(group_first:group_last)), group_size, group_order) + do i = 1, group_size + j = indx(group_first + group_order(i) - 1) + out = line(j)(1:13)//line(j)(28:31)//line(j)(39:45)// & + line(j)(35:38)//line(j)(46:80) + if (out(6:8) == ' ') cycle + if (out(19:22) == out0(19:22) .and. out(31:55) == out0(31:55) .and. & + out(6:8) == out0(6:8)) cycle + livecq2 = line(j) + livecq3 = out(1:61)//' '//livecq2(23:27)//' '//livecq2(79:83) + write(linenew,'("@",A)') trim(livecq3) + call write_stdout(trim(linenew)//new_line('a')) + out0 = out + enddo + group_first = group_last + 1 enddo - rewind 19 - rewind 20 - cfreq0=' ' nc = 0 - callsign0=' ' - call dbg('display: about to walk k3=' // itoa(k3) // ' sorted records for "@" emission') - do k = 1, k3 - out = line3(k)(1:13)//line3(k)(28:31)//line3(k)(39:45)// & - line3(k)(35:38)//line3(k)(46:80) - livecq2 = line3(k) - if (out(6:8) /= ' ') then - cfreq0 = out(6:8) - -! Suppress listing duplicate (same time, decoded message, and frequency) - if (out(19:22) /= out0(19:22) .or. out(31:55) /= out0(31:55) .or. & - out(6:8) /= out0(6:8)) then - livecq3 = out(1:61)//' '//livecq2(23:27)//' '//livecq2(79:83) - write(linenew,'("@",A)') trim(livecq3) - call write_stdout(trim(linenew)//new_line('a')) - call dbg('display: k=' // itoa(k) // ' "@" EMITTED freq=' // out(6:8) // & - ' utc=' // out(19:22) // ' msg="' // trim(out(31:55)) // '"') - out0 = out - else - call dbg('display: k=' // itoa(k) // ' "@" SUPPRESSED (matches out0) freq=' // out(6:8) // & - ' utc=' // out(19:22) // ' msg="' // trim(out(31:55)) // & - '" -- out0 was freq=' // out0(6:8) // ' utc=' // out0(19:22) // & - ' msg="' // trim(out0(31:55)) // '"') - endif - - i1 = index(out(31:), ' ') - callsign = out(i1+31:) - i2 = index(callsign, ' ') - if (i2 > 1) callsign(i2:) = ' ' - if (callsign /= ' ' .and. callsign /= callsign0) then - len = i2 - 1 - if (len < 0) len = 6 - if (len >= 3) then - if (nc < MAXCALLS) nc = nc + 1 - freqcall(nc) = cfreq0//line3(k)(9:13)//' '//callsign//line3(k)(79:80) - callsign0=callsign - endif - endif - if (callsign /= ' ' .and. callsign == callsign0) then - if (nc > 0 .and. nc <= MAXCALLS) then - freqcall(nc) = cfreq0//line3(k)(9:13)//' '//callsign//line3(k)(79:80) - endif - endif - else - call dbg('display: k=' // itoa(k) // ' SKIPPED entirely (out(6:8) blank -- not a frequency record)') - endif + do i = nz, 1, -1 + out = line(i)(1:13)//line(i)(28:31)//line(i)(39:45)// & + line(i)(35:38)//line(i)(46:80) + if (out(6:8) == ' ') cycle + i1 = index(out(31:), ' ') + if (i1 == 0) cycle + callsign = out(i1+31:) + i2 = index(callsign, ' ') + if (i2 > 1) callsign(i2:) = ' ' + len = i2 - 1 + if (len < 0) len = 6 + if (len < 3 .or. callsign == ' ') cycle + if (any(seen_calls(1:nc) == callsign)) cycle + nc = nc + 1 + seen_calls(nc) = callsign + freqcall(nc) = out(6:8)//line(i)(9:13)//' '//callsign//line(i)(79:81)//line(i)(40:43) + call_freqkHz(nc) = freqkHz(i) enddo - call dbg('display: done, nc(bandmap entries)=' // itoa(nc)) - -if(nc.lt.MAXCALLS) nc=nc+1 -freqcall(nc)=' ' - -if(nc.lt.MAXCALLS) nc=nc+1 -freqcall(nc)=' ' - -if (nc+1 <= MAXCALLS) freqcall(nc+1) = ' ' -if (nc+2 <= MAXCALLS) freqcall(nc+2) = ' ' - - + if (nc > 0) call indexx(call_freqkHz, nc, call_order) do i = 1, nc - write(linenew2,'("&",A)') trim(freqcall(i)) + write(linenew2,'("&",A)') trim(freqcall(call_order(i))) call write_stdout(trim(linenew2)//new_line('a')) enddo diff --git a/map65/libm65/filbig.f90 b/map65/libm65/filbig.f90 index bfbaf7a5..2e967034 100644 --- a/map65/libm65/filbig.f90 +++ b/map65/libm65/filbig.f90 @@ -172,21 +172,19 @@ contains endif enddo - ! Upper half of filter, with wrap-around and bounds check + ! Upper half of filter, wrapping negative-frequency bins do i = nh + 1, nfft2 j = i0 + i - 1 - nfft2 if (j .lt. 1) j = j + nfft1 - if (j < 1 .or. j > size(ca)) then - write (dbg_unit, *) 'FILBIG OOB: i=', i, ' j=', j, ' nh=', nh, & - ' nfft1=', nfft1, ' nfft2=', nfft2, ' i0=', i0 - flush(dbg_unit) - stop 'FILBIG index OOB' - end if - - filtval = real(cfilt_buf(i)) - base - c4a_buf(i) = filtval*ca(j) - if (xpol) c4b_buf(i) = filtval*cb(j) + if (j .ge. 1 .and. j .le. nfft1) then + filtval = real(cfilt_buf(i)) - base + c4a_buf(i) = filtval*ca(j) + if (xpol) c4b_buf(i) = filtval*cb(j) + else + c4a_buf(i) = 0.0 + if (xpol) c4b_buf(i) = 0.0 + endif enddo ! Do the short reverse transform, to go back to time domain. diff --git a/map65/libm65/ftninit.f90 b/map65/libm65/ftninit.f90 index 28f95fd4..789d7f93 100644 --- a/map65/libm65/ftninit.f90 +++ b/map65/libm65/ftninit.f90 @@ -19,7 +19,6 @@ subroutine ftninit(appd) open(13,file=appd//'/map65.log',status='unknown') ! open(19,file=appd//'/livecq.txt',status='unknown') ! not written with new livecq code open(21, file=appd//'/map65_rx.log', status='unknown', position='append', err=950) - open(26,file=appd//'/tmp26.txt',status='unknown') ! Import FFTW wisdom, if available: iret=fftwf_init_threads() !Initialize FFTW threading diff --git a/map65/libm65/m65_mod.f90 b/map65/libm65/m65_mod.f90 index 7710293f..a7b8b581 100644 --- a/map65/libm65/m65_mod.f90 +++ b/map65/libm65/m65_mod.f90 @@ -11,6 +11,7 @@ subroutine m65c() bind(C) use decode0_mod use npar_ptrs_mod, only: nhsym, nrxlog, datetime, active_input_generation, nagain, ndiskdat, manualDecodeFlag use debug_log, only: dbg, itoa, rtoa + use message_history_mod, only: clear_messages use sec_midn_mod, only: sec_midn integer :: npatience, nstandalone integer, save :: nhsym_prev_m65c = -1 @@ -32,30 +33,16 @@ subroutine m65c() bind(C) nrxlog = ibclr(nrxlog,1) endif if(iand(nrxlog,4).ne.0) then - ! rewind alone only repositions to record 1 for writing; it does not - ! shrink the file, so old history beyond whatever gets written this - ! cycle would stay on disk and could resurface via display.f90's own - ! rewind+read the same way the missing endfile there did (see - ! set_map65RxLog in npar_ptrs_mod.f90 for how Erase reaches here). - ! endfile makes this an immediate, real truncation to empty -- but it - ! leaves the file positioned after the EOF marker it just wrote, and - ! Fortran forbids further sequential I/O from there without - ! repositioning first, so backspace right away to land back on that - ! marker, ready for the next append. - ! ! An expired final request must not hide the next accumulation cycle. if(nhsym.eq.nhsym1 .and. (nhsym_prev_m65c.ne.nhsym1 .or. & previous_input_generation.ne.active_input_generation)) then - call dbg('m65c: UNIT26 TRUNCATED (endfile) at t=' // rtoa(sec_midn()) // & + call dbg('m65c: message history cleared at t=' // rtoa(sec_midn()) // & ' nhsym=' // itoa(nhsym) // ' nrxlog=' // itoa(nrxlog)) - rewind(26) - endfile(26) - backspace(26) + call clear_messages() else if (nhsym.eq.nhsym1) then - call dbg('m65c: UNIT26 truncation SKIPPED (repeat call at nhsym1) at t=' // & + call dbg('m65c: message history clear skipped (repeat call at nhsym1) at t=' // & rtoa(sec_midn()) // ' nrxlog=' // itoa(nrxlog)) endif - if(nhsym.eq.nhsym2) backspace(26) endif nhsym_prev_m65c = nhsym if(nagain.eq.0 .and. ndiskdat.eq.0 .and. manualDecodeFlag.eq.0) & diff --git a/map65/libm65/map65a.f90 b/map65/libm65/map65a.f90 index cece6ec9..90326477 100644 --- a/map65/libm65/map65a.f90 +++ b/map65/libm65/map65a.f90 @@ -23,6 +23,7 @@ contains use stdout_channel_mod, only: write_stdout use decodes_mod, only: nhsym1, ldecoded, ljt65decoded, ndecodes, mcall3a, decodes_init use display_mod + use message_history_mod, only: append_message, has_message use timf2_mod use getdphi_mod use datcom_ptrs_mod, only: ss_old, savg_old @@ -86,6 +87,7 @@ contains type(candidate) :: cand(MAX_CANDIDATES) real(real64) :: f00 character(len=256) :: line + character(len=83) :: history_line integer :: n_rms, i2 real*8 :: rms, sumsq, v real*4 :: tsec_mod @@ -99,6 +101,9 @@ contains integer :: best_i, best_ipol2 real :: sync1_tmp, dt_tmp, flipk_tmp, syncshort_tmp, snr2_tmp, dt2_tmp integer :: ipol_tmp, ipol2_tmp,ftol_bins, manualDecodeFlag_initial + integer :: i_click, il, iu, jsep, sh_j, sh_il, sh_iu, sh_ipol2 + real :: sh_sync, sh_snr2, sh_dt2, u_snr2, u_dt2 + integer :: u_ipol2 real :: freq_q65 integer :: nhsym_prev_call integer(c_int64_t) :: previous_input_generation = -1 @@ -221,6 +226,7 @@ contains i = nint(freq*1000.0/df) + icenter ! bin corresponding to that RF i = max(51, min(nfft_active - 51, i)) + i_click = i ! --- local search around clicked bin within ftol --- ftol = real(ntol) @@ -284,25 +290,59 @@ contains !=========================== ! SHORTHAND DETECTION (JT65) !=========================== + ! Same rule as the wideband quick decode (nqd=1). shorthand_detected = .false. - thresh0 = 1.0 - if (syncshort > thresh0 .and. mode65 > 0) then + if (ntol .le. 100) thresh0 = 0. + sh_j = 0 + if (mode65 > 0) then + sh_sync = thresh0 + do j = 2, 4 + jsep = nint(j*mode65*10.0*(11025.0/4096.0)/df) + do ii = -ftol_bins, ftol_bins + il = i_click + ii + iu = il + jsep + if (il < 51 .or. iu > nfft_active - 51) cycle + ssmax = 1.e30 + call ccf65(ss_dec(:,:,iu), nhsym, ssmax, sync1_tmp, ipol_tmp, jpz, dt_tmp, flipk_tmp, & + syncshort_tmp, u_snr2, u_ipol2, u_dt2) + if (syncshort_tmp <= sh_sync) cycle + syncshort = syncshort_tmp + ssmax = 1.e30 + call ccf65(ss_dec(:,:,il), nhsym, ssmax, sync1_tmp, ipol_tmp, jpz, dt_tmp, flipk_tmp, & + syncshort_tmp, snr2_tmp, ipol2_tmp, dt2_tmp) + if (syncshort_tmp <= thresh0) cycle + sh_sync = syncshort + sh_j = j + sh_il = il + sh_iu = iu + sh_snr2 = u_snr2 + sh_ipol2 = u_ipol2 + sh_dt2 = u_dt2 + enddo + enddo + endif + call dbg('map65a manual: shorthand search thresh0=' // rtoa(thresh0) // ' found=' // itoa(sh_j) // & + ' syncshort=' // rtoa(merge(sh_sync, -99.0, sh_j > 0))) + + if (sh_j > 0) then shorthand_detected = .true. km = 1 sig(1,1) = nfile sig(1,2) = nutc - sig(1,3) = freq - sig(1,4) = syncshort - sig(1,5) = dt2 - sig(1,6) = 45*(ipol2 - 1)/57.2957795 + sig(1,3) = 0.001*(sh_il - icenter)*df + 0.5*(nfa + nfb) + sig(1,4) = sh_sync + sig(1,5) = sh_dt2 + sig(1,6) = 45*(sh_ipol2 - 1)/57.2957795 sig(1,7) = 0 - sig(1,8) = snr2 + sig(1,8) = sh_snr2 sig(1,9) = 0 sig(1,10)= 0 - sig(1,12)= savg_dec(ipol2,i) - msg(1) = shmsg0(1) + sig(1,12)= savg_dec(sh_ipol2, sh_iu) + sig(1,13:16) = 0 + sig(1,18) = 0 + msg(1) = shmsg0(sh_j) endif ! Bin frequency for this click (for decode1a’s f00) @@ -313,11 +353,12 @@ contains noffset = nint(1000.0*(freq - fqso) - mousedf) - ! JT65-specific rejects only when JT65 is active + ! JT65-specific rejects only when JT65 is active. A detected + ! shorthand is still reported even though sync1 failed. if (mode65 > 0) then if (sync1 <= thresh1) then - if (.not. bq65) then + if (.not. bq65 .and. .not. shorthand_detected) then ! JT65-only mode → real reject newdat = 0 km = 0 @@ -336,7 +377,7 @@ contains if (abs(noffset) > ntol) then - if (.not. bq65) then + if (.not. bq65 .and. .not. shorthand_detected) then ! JT65-only mode → real reject newdat = 0 @@ -463,9 +504,9 @@ contains q65_success = (nsnr0 .gt. -99) ! On success, q65b has already written the decoded text itself - ! (write_stdout, plus units 26/21/12) because nqd=1 and the result - ! is within ntol of mousedf -- the same mechanism the wideband - ! quick-decode pass uses. Nothing further to add to km/msg/sig here. + ! (write_stdout, plus the history and units 21/12) because nqd=1 + ! and the result is within ntol of mousedf. The wideband quick + ! decode uses the same path. Nothing further to add to km/msg/sig. endif !=========================== @@ -522,9 +563,35 @@ contains write (line, '("!",I3,I5,I4,I6.4,F5.1,I5,1X,A1,1X,A22,I2,I5,I5,1X,A1)') & nkHz, ndf, npol, nutc, dt, nsync2, cm, decoded, nkv, nqual, ntxpol, cp call write_stdout(trim(line)//new_line('a')) + + ! Keep manual decodes out of both histories when the wideband + ! pass or an earlier click already recorded the same decode. + if (.not. has_message(f0, nutc, decoded)) then + do j = 1, 5 + a(j) = sig(k, 12 + j) + enddo + ndf0 = nint(a(1)) + ndf1 = nint(a(2)) + ndf2 = nint(a(3)) + cmode = '#A' + if (mode65 .eq. 2) cmode = '#B' + if (mode65 .eq. 4) cmode = '#C' + write (history_line, 1014) f0, ndf, ndf0, ndf1, ndf2, dt, npol, nsync1, & + nsync2, nutc, decoded, '#', cp, cmode + call append_message(history_line, nutc, f0, ndf) + ndecodes = ndecodes + 1 + write (21, 1100) f0, ndf, dt, npol, nsync2, nutc, decoded, '#', cp, & + cmode(1:1), cmode(2:2) + endif endif enddo ! k=1,km + ! Refresh Messages and Band Map after the manual pass. + ! 0.010 is the wideband pass's ftol (kHz); the ftol above is + ! the click tolerance in Hz. + flush (21) + call display(nkeep, 0.010, nutc) + manualDecodeFlag = 0 return endif @@ -1141,9 +1208,10 @@ endif cmode = '#A' if (mode65 .eq. 2) cmode = '#B' if (mode65 .eq. 4) cmode = '#C' - write (26, 1014) f0, ndf, ndf0, ndf1, ndf2, dt, npol, nsync1, & + write (history_line, 1014) f0, ndf, ndf0, ndf1, ndf2, dt, npol, nsync1, & nsync2, nutc, decoded, '#', cp, cmode ! was decoded,cp, 1014 format(f8.3, i5, 3i3, f5.1, i4, i3, i4, i5.4, 4x, a22, 7x, 2a1, 2x, a2) ! was a22,2x,a1,3x,a2 + call append_message(history_line, nutc, f0, ndf) ndecodes = ndecodes + 1 write (21, 1100) f0, ndf, dt, npol, nsync2, nutc, decoded, '#', cp, & cmode(1:1), cmode(2:2)! was decoded,cp, @@ -1151,11 +1219,8 @@ endif endif enddo - write (26, 1015) nutc -1015 format(37x, i6.4, ' ') flush (21) - flush (26) - call display(nkeep, ftol) + call display(nkeep, ftol, nutc) ndecdone = 2 900 close (23) diff --git a/map65/libm65/message_history_mod.f90 b/map65/libm65/message_history_mod.f90 new file mode 100644 index 00000000..155cfaed --- /dev/null +++ b/map65/libm65/message_history_mod.f90 @@ -0,0 +1,109 @@ +module message_history_mod + use iso_fortran_env, only: int64, real64 + implicit none + private + + type, public :: message_record + character(len=83) :: line + integer :: utc + real :: frequency_khz + integer(int64) :: inserted_tick + end type message_record + + type(message_record), allocatable :: history(:) + integer :: history_count = 0 + + public :: append_message, clear_messages, expire_messages, recent_messages, has_message, message_expired + +contains + + subroutine append_message(line, nutc, frequency_mhz, ndf) + character(len=*), intent(in) :: line + integer, intent(in) :: nutc, ndf + real(real64), intent(in) :: frequency_mhz + type(message_record), allocatable :: larger(:) + integer :: capacity + + if (.not. allocated(history)) allocate(history(128)) + if (history_count == size(history)) then + capacity = 2*size(history) + allocate(larger(capacity)) + larger(:history_count) = history + call move_alloc(larger, history) + endif + + history_count = history_count + 1 + history(history_count)%line = line + history(history_count)%utc = 60*(nutc/100) + mod(nutc, 100) + history(history_count)%frequency_khz = & + real(1000.0_real64*(frequency_mhz - 144.0_real64) + 0.001_real64*ndf) + call system_clock(count=history(history_count)%inserted_tick) + end subroutine append_message + + subroutine clear_messages() + history_count = 0 + end subroutine clear_messages + + subroutine expire_messages(nutc, nkeep) + integer, intent(in) :: nutc, nkeep + integer :: i, kept, now + integer(int64) :: now_tick, tick_rate, stored_s + + now = 60*(nutc/100) + mod(nutc, 100) + call system_clock(count=now_tick, count_rate=tick_rate) + kept = 0 + do i = 1, history_count + stored_s = -1 + if (tick_rate > 0 .and. now_tick >= history(i)%inserted_tick) & + stored_s = (now_tick - history(i)%inserted_tick)/tick_rate + if (message_expired(modulo(now - history(i)%utc, 1440), stored_s, nkeep)) cycle + kept = kept + 1 + if (kept /= i) history(kept) = history(i) + enddo + history_count = kept + end subroutine expire_messages + + ! utc_age_min decides expiry. stored_s (seconds since the record was + ! stored, -1 if unknown) only guards against the same UTC minute on a + ! later day, so it allows a margin: a record can be stored up to a pass + ! length before the display that ages it, so at the nkeep edge its + ! wall-clock age exceeds 60*nkeep. + logical function message_expired(utc_age_min, stored_s, nkeep) + integer, intent(in) :: utc_age_min, nkeep + integer(int64), intent(in) :: stored_s + message_expired = utc_age_min > nkeep .or. stored_s > 60_int64*(nkeep + 2) + end function message_expired + + subroutine recent_messages(max_records, records) + integer, intent(in) :: max_records + type(message_record), allocatable, intent(out) :: records(:) + integer :: count + + count = min(history_count, max_records) + allocate(records(count)) + if (count > 0) records = history(history_count-count+1:history_count) + end subroutine recent_messages + + logical function has_message(frequency_mhz, nutc, decoded) + real(real64), intent(in) :: frequency_mhz + integer, intent(in) :: nutc + character(len=*), intent(in) :: decoded + character(len=83) :: probe + integer :: i + + probe = ' ' + write(probe(1:8), '(f8.3)') frequency_mhz + write(probe(39:43), '(i5.4)') nutc + probe(48:69) = decoded + + has_message = .false. + do i = history_count, 1, -1 + if (history(i)%line(1:8) /= probe(1:8)) cycle + if (history(i)%line(39:43) /= probe(39:43)) cycle + if (history(i)%line(48:69) /= probe(48:69)) cycle + has_message = .true. + return + enddo + end function has_message + +end module message_history_mod diff --git a/map65/libm65/npar_ptrs_mod.f90 b/map65/libm65/npar_ptrs_mod.f90 index f33a92fc..10c9b904 100644 --- a/map65/libm65/npar_ptrs_mod.f90 +++ b/map65/libm65/npar_ptrs_mod.f90 @@ -40,7 +40,56 @@ module npar_ptrs_mod character(len=20) :: datetime = '' end type - ! GUI setters prepare draft; only the decoder changes active after claiming a request. + ! --------------------------------------------------------------------------- + ! Decode parameters: draft vs. active + ! + ! Two copies of decode_parameters exist, and which one you touch matters. + ! + ! draft Owned by the GUI thread. Every set_xxx()/get_xxx() bind(C) + ! routine in this module reads or writes draft, under + ! lock_decode_requests(). It holds the *pending* GUI state, not + ! necessarily what the decoder is using right now. + ! + ! active Owned by the decode thread. The module pointers below (nkeep, + ! ntimeout, mycall, ...) alias fields of active, so Fortran decode + ! code that says "ntimeout" is reading active%ntimeout. + ! + ! Flow: + ! MainWindow::decode() -> set_xxx() writes draft + ! publish_decode_request() copies draft into a queued request (forcing + ! newdat = 1 in that copy) and clears + ! draft%manualDecodeFlag + ! claim_decode_request() copies the oldest request into active; an + ! automatic live request whose input generation + ! is out of date is marked expired, and run_m65 + ! skips it (<DecodeSkipped>) + ! initialize_decode_parameters() copies draft into active once, at startup + ! The decoder then sees a consistent snapshot for the whole pass, even if + ! the GUI changes draft meanwhile. + ! + ! Why a lock alone is not enough: a decode pass runs for many seconds and + ! reads its parameters throughout. The lock only protects the instant of a + ! write, so if setters wrote the decoder's own variables directly, a GUI + ! change mid-pass would still alter values partway through the decode. The + ! draft/active copy is what freezes the parameters for a pass and lets + ! requests queue; the lock keeps publish_decode_request from copying a + ! half-written draft. + ! + ! Rules: + ! * C++ callers: a get_xxx() returns draft, i.e. the pending GUI value, + ! not the value the running decode is using. + ! * A parameter only reaches the decoder if the GUI writes it to draft + ! before a request is published. Setting it once and never re-pushing + ! works only if nothing else overwrites it (decode() currently re-pushes + ! most values every cycle). + ! * Fortran code may write active fields only for state it owns (e.g. + ! manualDecodeFlag = 0, nkeep = ...). Such writes do NOT flow back to + ! draft, so the next published request will carry draft's old value. + ! If a value must persist across requests, change it in draft too. + ! * Never add a second variable for the same setting (cf. the old + ! map65RxLog vs. nrxlog bug, where the setter wrote one variable and the + ! decoder read the other). + ! --------------------------------------------------------------------------- type(decode_parameters), target, save :: active type(decode_parameters), save :: draft type :: decode_request @@ -475,14 +524,6 @@ contains subroutine set_map65RxLog(val) bind(C, name="set_map65RxLog") use iso_c_binding integer(c_int), value :: val - ! map65RxLog is read back by get_map65RxLog() for debug logging only. - ! nrxlog is the variable m65c()/decode0.f90 actually check (iand bits - ! 1/2/4/8) to write the date header, erase map65_rx.log, clear the - ! decode-history file on Erase, and force manual dPhi. Before this fix - ! nrxlog was never assigned anywhere, so all four of those flag bits - ! were permanently dead -- e.g. Erase Band Map and Messages never - ! actually cleared unit 26, so the next display() cycle just replayed - ! the full decode history back into the Messages window. call lock_decode_requests() draft%map65RxLog = val draft%nrxlog = val diff --git a/map65/libm65/ptt.c b/map65/libm65/ptt.c index b2c6cb48..3fb06d3c 100644 --- a/map65/libm65/ptt.c +++ b/map65/libm65/ptt.c @@ -1,49 +1,105 @@ #include <windows.h> #include <stdio.h> +#include "ptt.h" + +static HANDLE ptt_handle = INVALID_HANDLE_VALUE; +static enum { PTT_IDLE, PTT_KEYED, PTT_UNCERTAIN } ptt_state = PTT_IDLE; + +static int control_line(DWORD operation) +{ + DWORD error; + if (EscapeCommFunction(ptt_handle, operation)) + return PTT_OK; + error = GetLastError(); + return error == ERROR_DEVICE_NOT_CONNECTED || error == ERROR_NO_SUCH_DEVICE + ? PTT_DEVICE_LOST : PTT_ERROR; +} + +static void close_port(void) +{ + if (ptt_handle != INVALID_HANDLE_VALUE) + CloseHandle(ptt_handle); + ptt_handle = INVALID_HANDLE_VALUE; + ptt_state = PTT_IDLE; +} + +static int release_lines(void) +{ + int rts = control_line(CLRRTS); + int dtr = control_line(CLRDTR); + EscapeCommFunction(ptt_handle, CLRBREAK); + if (rts == PTT_DEVICE_LOST || dtr == PTT_DEVICE_LOST) + return PTT_DEVICE_LOST; + return rts || dtr ? PTT_ERROR : PTT_OK; +} + +static int control_failure(int result, int *iptt) +{ + if (result == PTT_DEVICE_LOST) { + close_port(); + *iptt = 0; + } else { + ptt_state = PTT_UNCERTAIN; + } + return result; +} int ptt_(int *nport, int *ntx, int *iptt) { - static HANDLE hFile; - static int open=0; - char s[64]; - int i3=0,i4=0,i5=0,i6=0,i9=0,i00=0; - - if(*nport==0) { - *iptt=*ntx; - return(0); - } - - if(*ntx && (!open)) { - if (*nport < 10) - snprintf(s, sizeof(s), "COM%d", *nport); - else - snprintf(s, sizeof(s), "\\\\.\\COM%d", *nport); - - hFile = CreateFileA(s, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, - FILE_ATTRIBUTE_NORMAL,NULL); - - if(hFile==INVALID_HANDLE_VALUE) { - // printf("PTT: Cannot open COM port %d.\n",*nport); - return 1; + int result; + char path[64]; + + if (!*ntx) { + if (ptt_handle != INVALID_HANDLE_VALUE) { + result = release_lines(); + if (result != PTT_OK) + return control_failure(result, iptt); + *iptt = 0; + if (!CloseHandle(ptt_handle)) { + ptt_state = PTT_UNCERTAIN; + return PTT_ERROR; + } + ptt_handle = INVALID_HANDLE_VALUE; + } + ptt_state = PTT_IDLE; + *iptt = 0; + return PTT_OK; + } + + if (ptt_state == PTT_UNCERTAIN) + return PTT_ERROR; + if (ptt_state == PTT_KEYED) { + *iptt = 1; + return PTT_OK; + } + if (!*nport) { + *iptt = *ntx; + return PTT_OK; } - open=1; - } - - if(*ntx && open) { - EscapeCommFunction(hFile,3); - EscapeCommFunction(hFile,5); - *iptt=1; - } - - else { - EscapeCommFunction(hFile,4); - EscapeCommFunction(hFile,6); - EscapeCommFunction(hFile,9); - i00=CloseHandle(hFile); - *iptt=0; - open=0; - } - if((i00+i3+i4+i5+i6+i9)==-99) return -1; //Silence compiler warning - return 0; + if (*nport < 10) + snprintf(path, sizeof(path), "COM%d", *nport); + else + snprintf(path, sizeof(path), "\\\\.\\COM%d", *nport); + ptt_handle = CreateFileA(path, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, NULL); + if (ptt_handle == INVALID_HANDLE_VALUE) + return PTT_ERROR; + + result = control_line(SETRTS); + if (result == PTT_OK) + result = control_line(SETDTR); + if (result != PTT_OK) + return control_failure(result, iptt); + ptt_state = PTT_KEYED; + *iptt = 1; + return PTT_OK; +} + +void ptt_close(void) +{ + if (ptt_handle != INVALID_HANDLE_VALUE) { + release_lines(); + close_port(); + } } diff --git a/map65/libm65/ptt.h b/map65/libm65/ptt.h new file mode 100644 index 00000000..150c1545 --- /dev/null +++ b/map65/libm65/ptt.h @@ -0,0 +1,19 @@ +#ifndef MAP65_PTT_H +#define MAP65_PTT_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* Device loss resets logical PTT but does not confirm physical release. */ +enum { PTT_OK = 0, PTT_ERROR = 1, PTT_DEVICE_LOST = 2 }; + +int ptt_(int *nport, int *ntx, int *iptt); +void ptt_close(void); +void ptt_set_override(const char *path); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/map65/libm65/ptt_unix.c b/map65/libm65/ptt_unix.c index 0787308b..d287f1d3 100644 --- a/map65/libm65/ptt_unix.c +++ b/map65/libm65/ptt_unix.c @@ -15,132 +15,117 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -# include <stdio.h> -# include <stdlib.h> -# include <unistd.h> +#include <unistd.h> #include <fcntl.h> -# include <sys/ioctl.h> -#include <errno.h> +#include <sys/ioctl.h> #include <string.h> - -#ifdef HAVE_SYS_STAT_H -# include <sys/stat.h> -#endif -#if (defined(__unix__) || defined(unix)) && !defined(USG) -# include <sys/param.h> -#endif - -#include <string.h> - -int ptt_serial(int fd, int *ntx, int *iptt); - -/* - * ptt_ - * - * generic unix PTT routine called from Fortran - * - * Inputs - * unused Unused, to satisfy old windows calling convention - * ptt_port device name - * ntx pointer to fortran command on or off - * iptt pointer to fortran command status on or off - * Returns - non 0 if error -*/ +#include <poll.h> +#include "ptt.h" static char ptt_override[256] = {0}; -static int ptt_override_valid = 0; +static char opened_path[256] = {0}; +static int fd = -1; +static enum { PTT_IDLE, PTT_KEYED, PTT_UNCERTAIN } ptt_state = PTT_IDLE; void ptt_set_override(const char *path) { - if (path && *path) { - strncpy(ptt_override, path, sizeof(ptt_override)-1); - ptt_override_valid = 1; - } else { - ptt_override_valid = 0; - } + strncpy(ptt_override, path ? path : "", sizeof(ptt_override) - 1); + ptt_override[sizeof(ptt_override) - 1] = '\0'; } -static int fd = -1; - -int ptt_(int *nport, int *ntx, int *iptt) +int ptt_serial(int port_fd, int *ntx, int *iptt) { - (void)nport; -// ptt_log("ptt_unix: entry nport=%d ntx=%d iptt=%d", *nport, *ntx, *iptt); - - // PTT disabled - if (!ptt_override_valid) { - *iptt=*ntx; - return 0; - } - - const char *ptt_port = ptt_override; - - // Open once, keep open - if (fd < 0) { - fd = open(ptt_port, O_RDWR | O_NONBLOCK); - if (fd < 0) { -// ptt_log("ptt_unix: open failed errno=%d (%s)", errno, strerror(errno)); - return 1; - } -// ptt_log("ptt_unix: open OK fd=%d", fd); - - // *** PATCH: Force RTS+DTR LOW immediately after open *** int status = 0; - if (ioctl(fd, TIOCMGET, &status) == 0) { - status &= ~(TIOCM_RTS | TIOCM_DTR); - ioctl(fd, TIOCMSET, &status); -// ptt_log("ptt_unix: forced RTS/DTR LOW after open, status=0x%x", status); - } - } - ptt_serial(fd, ntx, iptt); - return 0; - } + if (ioctl(port_fd, TIOCMGET, &status) < 0) + return PTT_ERROR; + if (*ntx) + status |= TIOCM_RTS | TIOCM_DTR; + else + status &= ~(TIOCM_RTS | TIOCM_DTR); -/* - * ptt_serial - * - * generic serial unix PTT routine called indirectly from Fortran - * - * fd - already opened file descriptor - * ntx - pointer to fortran command on or off - * iptt - pointer to fortran command status on or off - */ + if (ioctl(port_fd, TIOCMSET, &status) < 0) + return PTT_ERROR; + *iptt = *ntx; + return PTT_OK; +} -int -ptt_serial(int fd, int *ntx, int *iptt) +static void close_port(void) { -int status; - -if (ioctl(fd, TIOCMGET, &status) < 0) { -// ptt_log("TIOCMGET failed errno=%d (%s)", errno, strerror(errno)); - return 1; + if (fd >= 0) + close(fd); + fd = -1; + opened_path[0] = '\0'; + ptt_state = PTT_IDLE; } - if(*ntx) { - status |= (TIOCM_RTS | TIOCM_DTR); // PTT ON - } else { - status &= ~(TIOCM_RTS | TIOCM_DTR); // PTT OFF +static int control_error(int *iptt) +{ + struct pollfd port = {fd, 0, 0}; + /* EIO alone can be transient; hangup identifies a dead connection. */ + if (poll(&port, 1, 0) > 0 && (port.revents & POLLHUP)) { + close_port(); + *iptt = 0; + return PTT_DEVICE_LOST; + } + ptt_state = PTT_UNCERTAIN; + return PTT_ERROR; } -if (ioctl(fd, TIOCMSET, &status) < 0) { -// ptt_log("TIOCMSET failed errno=%d (%s)", errno, strerror(errno)); - return 1; -} +int ptt_(int *nport, int *ntx, int *iptt) +{ + (void)nport; + + /* Always release the open port, even when the next selection is disabled. */ + if (!*ntx) { + if (fd >= 0 && ptt_serial(fd, ntx, iptt)) + return control_error(iptt); + ptt_state = PTT_IDLE; + *iptt = 0; + if (fd >= 0 && strcmp(opened_path, ptt_override)) + close_port(); + return PTT_OK; + } -//ptt_log("TIOCMSET OK status=0x%x", status); -*iptt = *ntx; -return 0; + /* A failed operation must be followed by a confirmed release before TX. */ + if (ptt_state == PTT_UNCERTAIN) + return PTT_ERROR; + if (ptt_state == PTT_KEYED) { + *iptt = 1; + return PTT_OK; + } + + if (fd >= 0 && strcmp(opened_path, ptt_override)) + close_port(); + if (!*ptt_override) { + *iptt = *ntx; + return PTT_OK; + } + if (fd < 0) { + fd = open(ptt_override, O_RDWR | O_NONBLOCK | O_NOCTTY); + if (fd < 0) + return PTT_ERROR; + strcpy(opened_path, ptt_override); + int off = 0; + int status = 0; + if (ptt_serial(fd, &off, &status)) + return control_error(iptt); + } + + if (ptt_serial(fd, ntx, iptt)) + return control_error(iptt); + ptt_state = PTT_KEYED; + return PTT_OK; } void ptt_close(void) { if (fd >= 0) { - close(fd); - fd = -1; -// ptt_log("ptt_unix: closed fd"); + int off = 0; + int status = 0; + ptt_serial(fd, &off, &status); + close_port(); } } - diff --git a/map65/libm65/q65b.F90 b/map65/libm65/q65b.F90 index 7f28f0f3..94ee0fb8 100644 --- a/map65/libm65/q65b.F90 +++ b/map65/libm65/q65b.F90 @@ -6,9 +6,61 @@ ! on stdout. module q65b_mod + use iso_fortran_env, only: int16, real64 implicit none + integer, parameter, private :: MAXFFT2 = 336000*2 contains + subroutine build_q65_samples(k0, nfft2, df, xpol, configured_dphi_deg, combine_poldeg, iwave) + use cacb_mod, only: ca, cb + use four2a_mod, only: four2a + integer, intent(in) :: k0, nfft2, configured_dphi_deg + real, intent(in) :: df, combine_poldeg + logical, intent(in) :: xpol + integer(int16), intent(out) :: iwave(:) + real(real64), parameter :: RAD = 57.2957795 + complex :: cx(0:MAXFFT2-1), cy(0:MAXFFT2-1), cz(0:MAXFFT2) + save cx, cy, cz + integer :: i, j, ja, jb + real :: dphi, fac, r + + fac = 1.0/nfft2 + cx(0:nfft2 - 1) = ca(k0:k0 + nfft2 - 1) + cx = fac*cx + if (xpol) then + cy(0:nfft2 - 1) = cb(k0:k0 + nfft2 - 1) + cy = fac*cy + if (configured_dphi_deg .ne. 0) then + dphi = configured_dphi_deg/RAD + cy(0:nfft2 - 1) = cmplx(cos(dphi), sin(dphi))*cy(0:nfft2 - 1) + endif + endif + + if (xpol) then + cz(0:MAXFFT2 - 1) = cos(combine_poldeg/RAD)*cx + sin(combine_poldeg/RAD)*cy + else + cz(0:MAXFFT2 - 1) = cx + endif + + cz(MAXFFT2) = 0. + ja = nint(500.0/df) + jb = nint(2500.0/df) + do i = 0, ja + r = 0.5*(1.0 + cos(i*3.14159/ja)) + cz(ja - i) = r*cz(ja - i) + cz(jb + i) = r*cz(jb + i) + enddo + cz(ja + jb + 1:) = 0. + + call four2a(cz, 2*nfft2, 1, 1, -1) + do i = 0, nfft2 - 1 + j = nfft2 - 1 - i + iwave(2*i + 2) = int(max(-32768, min(32767, nint(real(cz(j))))), kind=2) + iwave(2*i + 1) = int(max(-32768, min(32767, nint(aimag(cz(j))))), kind=2) + enddo + iwave(2*nfft2 + 1:) = 0 + end subroutine build_q65_samples + subroutine write_wav_header(unit, nsamp, fs) integer, intent(in) :: unit, nsamp, fs integer :: datasize, file_size @@ -41,6 +93,7 @@ use timer_module, only: timer use debug_log use stdout_channel_mod, only: write_stdout + use message_history_mod, only: append_message use decodes_mod, only: ldecoded, ndecodes use cacb_mod use four2a_mod @@ -84,13 +137,10 @@ ! MAXFFT1 must be = nfft_big_active; MAXFFT2 must be = the ! downsampled-FFT length at the highest supported rate. integer, parameter :: MAXFFT1 = 56*192000 - integer, parameter :: MAXFFT2 = 336000*2 integer, parameter :: NMAX = 60*12000 - real(real64), parameter :: RAD = 57.2957795 !==== Local variables ===================================================== integer(int16) :: iwave(300*12000) - complex :: cx(0:MAXFFT2-1), cy(0:MAXFFT2-1), cz(0:MAXFFT2) integer :: ipk1(1) ! Small, fixed bin radius for the automatic-candidate ipk search (see ! the note below at "for a wideband candidate"). Deliberately @@ -98,13 +148,13 @@ ! wb_sync's own low-SNR bin-selection noise, not accommodate arbitrary ! user-configured search widths. integer, parameter :: IPK_LOCAL_BINS = 5 - integer :: i, ia, ib, ifreq, ikhz1, ipk, ipol - integer :: j, ja, jb, k0, mhz, ndf, nfft1, nfft2 + integer :: ia, ib, ifreq, ikhz1, ipk, ipol + integer :: k0, mhz, ndf, nfft1, nfft2 integer :: npol, nq65df, nsubmode, ntxpol, nutc00, nh integer :: nfa, nfb integer :: k0_click, mousedf_gate - real :: df, df3, dphi, f_ipk, f_mouse, fac - real :: combine_poldeg, freq1_00, frx, fsked, poldeg, r, snr1 + real :: df, df3, f_ipk, f_mouse + real :: combine_poldeg, freq1_00, frx, fsked, poldeg, snr1 real(real64) :: freq0, freq1 character(len=12) :: mycall, hiscall character(len=4) :: grid4 @@ -112,6 +162,7 @@ character(len=1) :: cp character(len=2) :: cmode character(len=256) :: linenew + character(len=83) :: history_line integer :: t_now, t_rate logical :: search_cursor @@ -272,62 +323,16 @@ ! (below) determine success or failure instead. if (snr1 .lt. 1.5 .and. manualDecodeFlag .eq. 0) go to 900 !### Threshold needs work? ### - fac = 1.0/nfft2 - cx(0:nfft2 - 1) = ca(k0:k0 + nfft2 - 1) - cx = fac*cx - if (xpol) then - cy(0:nfft2 - 1) = cb(k0:k0 + nfft2 - 1) - cy = fac*cy - if (configured_dphi_deg .ne. 0) then - dphi = configured_dphi_deg/RAD - ! The sync weights describe the phase-corrected basis from symspec. - cy(0:nfft2 - 1) = cmplx(cos(dphi), sin(dphi))*cy(0:nfft2 - 1) - endif - endif - -! Here cx and cy (if xpol) are frequency-domain data around the selected -! QSO frequency, taken from the full-length FFT computed in filbig(). -! Values for fsample, nfft1, nfft2, df, and the downsampled data rate -! are as follows: - -! fSample nfft1 df nfft2 fDownSampled -! (Hz) (Hz) (Hz) -!---------------------------------------------------- -! 96000 5376000 0.017857143 336000 6000.000 -! 95238 5120000 0.018601172 322560 5999.994 - poldeg = 0. + combine_poldeg = 0. if (xpol) then ! NB: still uses the (possibly wrong-bin, see k0 above) ipk for ! manual clicks in xpol mode -- not exercised by current testing, ! but worth revisiting if xpol manual decode misbehaves similarly. poldeg = sync(ipk)%pol combine_poldeg = sync(ipk)%combine_pol - cz(0:MAXFFT2 - 1) = cos(combine_poldeg/RAD)*cx + sin(combine_poldeg/RAD)*cy - else - cz(0:MAXFFT2 - 1) = cx endif - - cz(MAXFFT2) = 0. -! Roll off below 500 Hz and above 2500 Hz. - ja = nint(500.0/df) - jb = nint(2500.0/df) - do i = 0, ja - r = 0.5*(1.0 + cos(i*3.14159/ja)) - cz(ja - i) = r*cz(ja - i) - cz(jb + i) = r*cz(jb + i) - enddo - cz(ja + jb + 1:) = 0. - -!Transform to time domain (real), fsample=12000 Hz - call four2a(cz, 2*nfft2, 1, 1, -1) - do i = 0, nfft2 - 1 - j = nfft2 - 1 - i - iwave(2*i + 2) = int(max(-32768, min(32767, nint(real(cz(j))))), kind=2) - iwave(2*i + 1) = int(max(-32768, min(32767, nint(aimag(cz(j))))), kind=2) - enddo - - iwave(2*nfft2 + 1:) = 0 + call build_q65_samples(k0, nfft2, df, xpol, configured_dphi_deg, combine_poldeg, iwave) nsubmode = mode_q65 - 1 nfa = 990 !Tight limits around ipk for the wideband decode @@ -394,13 +399,14 @@ call write_stdout(trim(linenew)//new_line('a')) endif -! Write to lu 26, for Messages and Band Map windows +! Retain wideband decodes for Messages and Band Map. cmode = ': ' cmode(2:2) = char(ichar('A') + mode_q65 - 1) freq1 = freq0 + 0.001d0*(ikhz1 - ikhz) - write (26, 1014) freq1, ndf, 0, 0, 0, xdt0, npol, 0, nsnr0, nutc, msg0(1:28), & + write (history_line, 1014) freq1, ndf, 0, 0, 0, xdt0, npol, 0, nsnr0, nutc, msg0(1:28), & ':', cp, cmode ! was 1:22 1014 format(f8.3, i5, 3i3, f5.1, i4, i3, i4, i5.4, 4x, a28, 1x, 2a1, 2x, a2) ! was a22 + call append_message(history_line, nutc, freq1, ndf) ! Suppress writing duplicates (same time, decoded message, and frequency) ! to map65_rx.log diff --git a/map65/libm65/tmoonsub.c b/map65/libm65/tmoonsub.c index f33dec0e..a7b4eaae 100644 --- a/map65/libm65/tmoonsub.c +++ b/map65/libm65/tmoonsub.c @@ -56,7 +56,7 @@ void getargs(int argc, char *argv[], int *y, int *m, double *tz, /* if not right number of arguments, then print example command line */ if (argc !=5) { - fprintf(stderr, usage); + fputs(usage, stderr); exit(EXIT_FAILURE); } |
