Skip to content

Commit

Permalink
Change min/max macro to MIN/MAX
Browse files Browse the repository at this point in the history
  • Loading branch information
zuckschwerdt committed Mar 1, 2019
1 parent 606e612 commit 162e720
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 25 deletions.
8 changes: 4 additions & 4 deletions include/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
#include <stdint.h>

// Helper macros, collides with MSVC's stdlib.h unless NOMINMAX is used
#ifndef max
#define max(a,b) ((a) > (b) ? (a) : (b))
#ifndef MAX
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#endif
#ifndef min
#define min(a,b) ((a) < (b) ? (a) : (b))
#ifndef MIN
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#endif

/// Reverse (reflect) the bits in an 8 bit byte.
Expand Down
5 changes: 3 additions & 2 deletions src/compat_paths.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ char **compat_get_default_conf_paths()
if (paths[0]) return paths;
// Working directory, i.e. where the binary is located
if (GetModuleFileName(NULL, bufs[0], sizeof(bufs[0]))) {
char *last_slash = max(strrchr(bufs[0], '\\'), strrchr(bufs[0], '/'));
if (last_slash) *last_slash = 0;
char *last_backslash = strrchr(bufs[0], '\\');
if (last_backslash)
*last_backslash = '\0';
strcat_s(bufs[0], sizeof(bufs[0]), "\\rtl_433.conf");
paths[0] = bufs[0];
}
Expand Down
2 changes: 1 addition & 1 deletion src/devices/acurite.c
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ static int acurite_6045_decode(r_device *decoder, bitrow_t bb, int browlen)
* having to enable debug for long running rtl_433 processes.
*/
rawp = (char *)raw_str;
for (int i=0; i < min(browlen, 15); i++) {
for (int i=0; i < MIN(browlen, 15); i++) {
sprintf(rawp,"%02x",bb[i]);
rawp += 2;
};
Expand Down
2 changes: 1 addition & 1 deletion src/devices/bresser_5in1.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ static int bresser_5in1_callback(r_device *decoder, bitbuffer_t *bitbuffer)
return 0; // message too short
}
// truncate any excessive bits
len = min(len, sizeof (msg) * 8);
len = MIN(len, sizeof (msg) * 8);

bitbuffer_extract_bytes(bitbuffer, 0, start_pos, msg, len);

Expand Down
6 changes: 3 additions & 3 deletions src/devices/m_bus.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ static int m_bus_decode_format_a(r_device *decoder, const m_bus_data_t *in, m_bu
for (unsigned n=0; n < num_data_blocks; ++n) {
const uint8_t *in_ptr = in->data+BLOCK1A_SIZE+n*18; // Pointer to where data starts. Each block is 18 bytes
uint8_t *out_ptr = out->data+n*16; // Pointer into block where data starts.
uint8_t block_size = min(block1->L-9-n*16, 16)+2; // Maximum block size is 16 Data + 2 CRC
uint8_t block_size = MIN(block1->L-9-n*16, 16)+2; // Maximum block size is 16 Data + 2 CRC

// Validate CRC
if (!m_bus_crc_valid(decoder, in_ptr, block_size-2)) return 0;
Expand Down Expand Up @@ -195,10 +195,10 @@ static int m_bus_decode_format_b(r_device *decoder, const m_bus_data_t *in, m_bu
}

// Validate CRC
if (!m_bus_crc_valid(decoder, in->data, min(block1->L-1, (BLOCK1B_SIZE+BLOCK2B_SIZE)-2))) return 0;
if (!m_bus_crc_valid(decoder, in->data, MIN(block1->L-1, (BLOCK1B_SIZE+BLOCK2B_SIZE)-2))) return 0;

// Get data from Block 2
memcpy(out->data, in->data+BLOCK1B_SIZE, (min(block1->L-11, BLOCK2B_SIZE-2)));
memcpy(out->data, in->data+BLOCK1B_SIZE, (MIN(block1->L-11, BLOCK2B_SIZE-2)));

// Extract extra block for long telegrams (not tested!)
uint8_t L_OFFSET = BLOCK1B_SIZE+BLOCK2B_SIZE-1; // How much to subtract from L (127)
Expand Down
2 changes: 1 addition & 1 deletion src/pulse_demod.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ int pulse_demod_pcm(const pulse_data_t *pulses, r_device *device)
}
// Add run of zeros
periods -= highs; // Remove 1s from whole period
periods = min(periods, max_zeros); // Don't overflow at end of message
periods = MIN(periods, max_zeros); // Don't overflow at end of message
for (int i = 0; i < periods; ++i) {
bitbuffer_add_bit(&bits, 0);
}
Expand Down
26 changes: 13 additions & 13 deletions src/pulse_detect.c
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ int pulse_detect_package(pulse_detect_t *pulse_detect, int16_t const *envelope_d
{
int const samples_per_ms = samp_rate / 1000;
pulse_detect_t *s = pulse_detect;
s->ook_high_estimate = max(s->ook_high_estimate, OOK_MIN_HIGH_LEVEL); // Be sure to set initial minimum level
s->ook_high_estimate = MAX(s->ook_high_estimate, OOK_MIN_HIGH_LEVEL); // Be sure to set initial minimum level

if (s->data_counter == 0) {
// age the pulse_data if this is a fresh buffer
Expand Down Expand Up @@ -403,8 +403,8 @@ int pulse_detect_package(pulse_detect_t *pulse_detect, int16_t const *envelope_d
s->ook_low_estimate += ((ook_low_delta > 0) ? 1 : -1); // Hack to compensate for lack of fixed-point scaling
// Calculate default OOK high level estimate
s->ook_high_estimate = OOK_HIGH_LOW_RATIO * s->ook_low_estimate; // Default is a ratio of low level
s->ook_high_estimate = max(s->ook_high_estimate, OOK_MIN_HIGH_LEVEL);
s->ook_high_estimate = min(s->ook_high_estimate, OOK_MAX_HIGH_LEVEL);
s->ook_high_estimate = MAX(s->ook_high_estimate, OOK_MIN_HIGH_LEVEL);
s->ook_high_estimate = MIN(s->ook_high_estimate, OOK_MAX_HIGH_LEVEL);
if (s->lead_in_counter <= OOK_EST_LOW_RATIO) s->lead_in_counter++; // Allow initial estimate to settle
}
break;
Expand All @@ -419,7 +419,7 @@ int pulse_detect_package(pulse_detect_t *pulse_detect, int16_t const *envelope_d
else {
// Continue with OOK decoding
pulses->pulse[pulses->num_pulses] = s->pulse_length; // Store pulse width
s->max_pulse = max(s->pulse_length, s->max_pulse); // Find largest pulse
s->max_pulse = MAX(s->pulse_length, s->max_pulse); // Find largest pulse
s->pulse_length = 0;
s->ook_state = PD_OOK_STATE_GAP_START;
}
Expand All @@ -428,8 +428,8 @@ int pulse_detect_package(pulse_detect_t *pulse_detect, int16_t const *envelope_d
else {
// Calculate OOK high level estimate
s->ook_high_estimate += am_n / OOK_EST_HIGH_RATIO - s->ook_high_estimate / OOK_EST_HIGH_RATIO;
s->ook_high_estimate = max(s->ook_high_estimate, OOK_MIN_HIGH_LEVEL);
s->ook_high_estimate = min(s->ook_high_estimate, OOK_MAX_HIGH_LEVEL);
s->ook_high_estimate = MAX(s->ook_high_estimate, OOK_MIN_HIGH_LEVEL);
s->ook_high_estimate = MIN(s->ook_high_estimate, OOK_MAX_HIGH_LEVEL);
// Estimate pulse carrier frequency
pulses->fsk_f1_est += fm_data[s->data_counter] / OOK_EST_HIGH_RATIO - pulses->fsk_f1_est / OOK_EST_HIGH_RATIO;
}
Expand Down Expand Up @@ -542,12 +542,12 @@ void histogram_sum(histogram_t *hist, int const *data, unsigned len, float toler
for (bin = 0; bin < hist->bins_count; ++bin) {
int bn = data[n];
int bm = hist->bins[bin].mean;
if (abs(bn - bm) < (tolerance * max(bn, bm))) {
if (abs(bn - bm) < (tolerance * MAX(bn, bm))) {
hist->bins[bin].count++;
hist->bins[bin].sum += data[n];
hist->bins[bin].mean = hist->bins[bin].sum / hist->bins[bin].count;
hist->bins[bin].min = min(data[n], hist->bins[bin].min);
hist->bins[bin].max = max(data[n], hist->bins[bin].max);
hist->bins[bin].min = MIN(data[n], hist->bins[bin].min);
hist->bins[bin].max = MAX(data[n], hist->bins[bin].max);
break; // Match found! Data added to existing bin
}
}
Expand Down Expand Up @@ -629,13 +629,13 @@ void histogram_fuse_bins(histogram_t *hist, float tolerance)
int bn = hist->bins[n].mean;
int bm = hist->bins[m].mean;
// if within tolerance
if (abs(bn - bm) < (tolerance * max(bn, bm))) {
if (abs(bn - bm) < (tolerance * MAX(bn, bm))) {
// Fuse data for bin[n] and bin[m]
hist->bins[n].count += hist->bins[m].count;
hist->bins[n].sum += hist->bins[m].sum;
hist->bins[n].mean = hist->bins[n].sum / hist->bins[n].count;
hist->bins[n].min = min(hist->bins[n].min, hist->bins[m].min);
hist->bins[n].max = max(hist->bins[n].max, hist->bins[m].max);
hist->bins[n].min = MIN(hist->bins[n].min, hist->bins[m].min);
hist->bins[n].max = MAX(hist->bins[n].max, hist->bins[m].max);
// Delete bin[m]
histogram_delete_bin(hist, m);
m--; // Compare new bin in same place!
Expand Down Expand Up @@ -748,7 +748,7 @@ void pulse_analyzer(pulse_data_t *data)
else if (hist_pulses.bins_count == 2 && hist_gaps.bins_count == 2 && hist_periods.bins_count == 3) {
fprintf(stderr, "Manchester coding\n");
device.modulation = OOK_PULSE_MANCHESTER_ZEROBIT;
device.s_short_width = min(hist_pulses.bins[0].mean, hist_pulses.bins[1].mean); // Assume shortest pulse is half period
device.s_short_width = MIN(hist_pulses.bins[0].mean, hist_pulses.bins[1].mean); // Assume shortest pulse is half period
device.s_long_width = 0; // Not used
device.s_reset_limit = hist_gaps.bins[hist_gaps.bins_count - 1].max + 1; // Set limit above biggest gap
}
Expand Down

0 comments on commit 162e720

Please sign in to comment.