@@ -44,34 +44,41 @@ void begin_unit()
4444}
4545
4646/*
47- Reader settings tuned for a tag resting on the antenna .
47+ Three sets of reader settings, and a button to move between them .
4848
4949 A reader as it ships reaches about a metre and a half. At contact the reply overwhelms the
5050 receiver: most inventory rounds find nothing and about half of every read fails, however strong
5151 the reply is, so RSSI is no guide to what is wrong. Bringing the working distance in fixes it,
52- either by lowering the transmit power or by lowering the receiver gain. Lowering the gain is the
53- better of the two, since the transmit power is what energises the tag in the first place.
52+ and lowering the receiver gain is the better way of doing that, since the transmit power is what
53+ energises the tag in the first place.
5454
55- WARNING: the module keeps these settings when it loses power, so running this sketch changes the
56- unit until something changes it back. What one unit was found set to is spelled out below;
57- assign those to the three constants in use to put it back the way it was.
55+ The three differ in the mixer gain and the demodulation threshold and in nothing else, which is
56+ what makes them worth comparing.
5857
59- Those are not the values the chip's own documentation calls the defaults, which are a mixer gain
60- of 9dB and a threshold of 0x01B0. That unit came set for distance rather than for stability, so
61- read what is there before assuming what it was.
58+ WARNING: the module keeps these settings when it loses power. Nothing is written until the button
59+ is pressed, so starting this sketch leaves a unit alone, but pressing it does not. What a unit
60+ was set to before that is only recoverable because one of the three says what this product was
61+ found shipped with.
6262*/
63- namespace as_shipped {
64- constexpr int16_t TX_POWER_DBM100 {2600 };
65- constexpr m5::unit::m100::MixerGain MIXER_GAIN {m5::unit::m100::MixerGain::dB6};
66- constexpr uint16_t THRESHOLD {0x00B0 };
67- } // namespace as_shipped
68-
69- // ! @brief Transmit power in 1/100 dBm. The module accepts 1700 to 2600
70- constexpr int16_t TX_POWER_DBM100 {2600 };
71- // ! @brief Demodulation threshold. 0x01B0 is the lowest the chip documents as worth using
72- constexpr uint16_t DEMODULATOR_THRESHOLD {0x01B0 };
73- // ! @brief Mixer gain, a step below what one unit was found shipped with
74- constexpr m5::unit::m100::MixerGain DEMODULATOR_MIXER_GAIN {m5::unit::m100::MixerGain::dB3};
63+ struct Preset {
64+ const char * name;
65+ int16_t dbm100;
66+ m5::unit::m100::MixerGain mixer_gain;
67+ m5::unit::m100::IFGain if_gain;
68+ uint16_t threshold;
69+ };
70+
71+ const Preset PRESETS [] = {
72+ // What the chip's own documentation gives as the defaults
73+ {" documented" , 2600 , m5::unit::m100::MixerGain::dB9, m5::unit::m100::IFGain::dB36, 0x01B0 },
74+ // What one Unit UHF-RFID (U107) was found set to out of the box: tuned for reach, with a
75+ // threshold below the floor the same documentation gives. Only one unit was ever looked at,
76+ // so compare it against what this sketch prints at startup before trusting it
77+ {" as the unit shipped" , 2600 , m5::unit::m100::MixerGain::dB6, m5::unit::m100::IFGain::dB36, 0x00B0 },
78+ // Measured to read a tag resting on the antenna, where the other two miss most rounds
79+ {" for a tag at contact" , 2600 , m5::unit::m100::MixerGain::dB3, m5::unit::m100::IFGain::dB36, 0x01B0 },
80+ };
81+ size_t preset_index{};
7582
7683const char * region_to_string (const m5::uhf::Region r)
7784{
@@ -153,54 +160,62 @@ void report_settings()
153160 m5::unit::m100::ifGainDb (dp.if_gain ), dp.threshold );
154161 }
155162}
156- } // namespace
157163
158- void setup ()
164+ // ! @brief Write one of the presets and say what the reader holds afterwards
165+ void apply (const Preset& preset)
159166{
160- begin_unit ();
161-
162- M5_LOGI (" --- as found ---" );
163- report_settings ();
164-
165- if (!unit.writeTransmitPower (TX_POWER_DBM100 )) {
167+ M5_LOGI (" --- applying: %s ---" , preset.name );
168+ if (!unit.writeTransmitPower (preset.dbm100 )) {
166169 M5_LOGE (" Failed to set the transmit power" );
167170 }
168171 m5::unit::m100::DemodulatorParameters dp{};
169- if (unit.readDemodulatorParameters (dp)) {
170- dp.mixer_gain = DEMODULATOR_MIXER_GAIN ;
171- dp.threshold = DEMODULATOR_THRESHOLD ;
172- if (!unit.writeDemodulatorParameters (dp)) {
173- M5_LOGE (" Failed to set the demodulator parameters" );
174- }
172+ dp.mixer_gain = preset.mixer_gain ;
173+ dp.if_gain = preset.if_gain ;
174+ dp.threshold = preset.threshold ;
175+ if (!unit.writeDemodulatorParameters (dp)) {
176+ M5_LOGE (" Failed to set the demodulator parameters" );
175177 }
176-
177- M5_LOGI (" --- now ---" );
178+ // Reading back rather than reporting what was asked for: the two are not the same thing
178179 report_settings ();
179- M5_LOGW (" These settings survive a power cycle. As shipped: mixer=%udB threshold=0x%04X, power %d.%02ddBm" ,
180- m5::unit::m100::mixerGainDb (as_shipped::MIXER_GAIN ), as_shipped::THRESHOLD ,
181- as_shipped::TX_POWER_DBM100 / 100 , as_shipped::TX_POWER_DBM100 % 100 );
182180
181+ lcd.fillScreen (TFT_DARKGREEN );
182+ lcd.setCursor (0 , 0 );
183+ lcd.printf (" %s\n " , preset.name );
184+ lcd.printf (" mixer %udB thr %04X\n " , m5::unit::m100::mixerGainDb (preset.mixer_gain ), preset.threshold );
185+ lcd.printf (" %d.%02d dBm\n\n " , preset.dbm100 / 100 , preset.dbm100 % 100 );
186+ lcd.println (" A: next preset" );
187+ }
188+ } // namespace
189+
190+ void setup ()
191+ {
192+ begin_unit ();
193+
194+ // Read only. A unit that has never been written to still holds whatever it shipped with, and
195+ // that is worth seeing before anything replaces it
196+ M5_LOGI (" --- as found ---" );
197+ report_settings ();
183198 survey_band ();
184199
185200 lcd.fillScreen (TFT_DARKGREEN );
186201 lcd.setTextSize (lcd.width () > 320 ? 2 : 1 );
187202 lcd.setCursor (0 , 0 );
188- lcd.println (" A: survey the band" );
189- lcd.println (" (take the tag away first)" );
203+ lcd.println (" as found" );
204+ lcd.println (" (nothing written yet)" );
205+ lcd.println (" " );
206+ lcd.println (" A: next preset" );
190207}
191208
192209void loop ()
193210{
194211 M5 .update ();
195212 Units.update ();
196213
197- // Worth repeating: a tag that will not read while the antenna and the power are both fine is
198- // usually drowned out by something on the air, and these two scans tell that apart from a
199- // problem with the tag
200214 if (M5 .BtnA .wasClicked ()) {
201- lcd.fillScreen (TFT_DARKGREEN );
202- lcd.setCursor (0 , 0 );
203- lcd.println (" surveying..." );
215+ apply (PRESETS [preset_index]);
216+ preset_index = (preset_index + 1 ) % (sizeof (PRESETS ) / sizeof (PRESETS [0 ]));
217+ // The band looks different once the transmit power or the gain moves, and a tag left in
218+ // the field reflects the carrier back and reads as interference
204219 survey_band ();
205220 }
206221}
0 commit comments