Skip to content

Commit

Permalink
Un-unroll some loops and save a few bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
JuantAldea authored and egzumer committed Dec 24, 2023
1 parent 7d982f1 commit bc9bb48
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 145 deletions.
52 changes: 29 additions & 23 deletions app/spectrum.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "app/spectrum.h"
#include "am_fix.h"
#include "audio.h"
#include "misc.h"

#ifdef ENABLE_SCAN_RANGES
#include "chFrScanner.h"
Expand All @@ -37,7 +38,6 @@ struct FrequencyBandInfo {

const uint16_t RSSI_MAX_VALUE = 65535;

static uint16_t R30, R37, R3D, R43, R47, R48, R7E;
static uint32_t initialFreq;
static char String[32];

Expand Down Expand Up @@ -221,24 +221,29 @@ static void ToggleAFBit(bool on) {
BK4819_WriteRegister(BK4819_REG_47, reg);
}

static const BK4819_REGISTER_t registers_to_save[] ={
BK4819_REG_30,
BK4819_REG_37,
BK4819_REG_3D,
BK4819_REG_43,
BK4819_REG_47,
BK4819_REG_48,
BK4819_REG_7E,
};

static uint16_t registers_stack [sizeof(registers_to_save)];

static void BackupRegisters() {
R30 = BK4819_ReadRegister(BK4819_REG_30);
R37 = BK4819_ReadRegister(BK4819_REG_37);
R3D = BK4819_ReadRegister(BK4819_REG_3D);
R43 = BK4819_ReadRegister(BK4819_REG_43);
R47 = BK4819_ReadRegister(BK4819_REG_47);
R48 = BK4819_ReadRegister(BK4819_REG_48);
R7E = BK4819_ReadRegister(BK4819_REG_7E);
for (uint32_t i = 0; i < ARRAY_SIZE(registers_to_save); i++){
registers_stack[i] = BK4819_ReadRegister(registers_to_save[i]);
}
}

static void RestoreRegisters() {
BK4819_WriteRegister(BK4819_REG_30, R30);
BK4819_WriteRegister(BK4819_REG_37, R37);
BK4819_WriteRegister(BK4819_REG_3D, R3D);
BK4819_WriteRegister(BK4819_REG_43, R43);
BK4819_WriteRegister(BK4819_REG_47, R47);
BK4819_WriteRegister(BK4819_REG_48, R48);
BK4819_WriteRegister(BK4819_REG_7E, R7E);

for (uint32_t i = 0; i < ARRAY_SIZE(registers_to_save); i++){
BK4819_WriteRegister(registers_to_save[i], registers_stack[i]);
}
}

static void ToggleAFDAC(bool on) {
Expand Down Expand Up @@ -272,8 +277,8 @@ bool IsCenterMode() { return settings.scanStepIndex < S_STEP_2_5kHz; }
// scan step in 0.01khz
uint16_t GetScanStep() { return scanStepValues[settings.scanStepIndex]; }

uint16_t GetStepsCount()
{
uint16_t GetStepsCount()
{
#ifdef ENABLE_SCAN_RANGES
if(gScanRangeStart) {
return (gScanRangeStop - gScanRangeStart) / GetScanStep();
Expand Down Expand Up @@ -425,10 +430,10 @@ static void UpdatePeakInfo() {

static void SetRssiHistory(uint16_t idx, uint16_t rssi)
{
#ifdef ENABLE_SCAN_RANGES
#ifdef ENABLE_SCAN_RANGES
if(scanInfo.measurementsCount > 128) {
uint8_t i = (uint32_t)ARRAY_SIZE(rssiHistory) * 1000 / scanInfo.measurementsCount * idx / 1000;
if(rssiHistory[i] < rssi || isListening)
if(rssiHistory[i] < rssi || isListening)
rssiHistory[i] = rssi;
rssiHistory[(i+1)%128] = 0;
return;
Expand All @@ -437,8 +442,8 @@ static void SetRssiHistory(uint16_t idx, uint16_t rssi)
rssiHistory[idx] = rssi;
}

static void Measure()
{
static void Measure()
{
uint16_t rssi = scanInfo.rssi = GetRssi();
SetRssiHistory(scanInfo.i, rssi);
}
Expand Down Expand Up @@ -490,6 +495,7 @@ static void UpdateScanStep(bool inc) {
} else {
return;
}

settings.frequencyChangeStep = GetBW() >> 1;
RelaunchScan();
ResetBlacklist();
Expand Down Expand Up @@ -846,7 +852,7 @@ static void OnKeyDown(uint8_t key) {
case KEY_5:
#ifdef ENABLE_SCAN_RANGES
if(!gScanRangeStart)
#endif
#endif
FreqInput();
break;
case KEY_0:
Expand Down Expand Up @@ -1146,7 +1152,7 @@ static void UpdateScan() {
}

if(scanInfo.measurementsCount < 128)
memset(&rssiHistory[scanInfo.measurementsCount], 0,
memset(&rssiHistory[scanInfo.measurementsCount], 0,
sizeof(rssiHistory) - scanInfo.measurementsCount*sizeof(rssiHistory[0]));

redrawScreen = true;
Expand Down
2 changes: 1 addition & 1 deletion driver/bk4819-regs.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#ifndef BK4819_REGS_H
#define BK4819_REGS_H

#include <stdint.h>

typedef struct {
const char *name;
Expand Down Expand Up @@ -387,4 +388,3 @@ enum {
};

#endif

Loading

0 comments on commit bc9bb48

Please sign in to comment.