Skip to content

Commit

Permalink
Simplify struct declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
rhargreaves committed Jul 18, 2024
1 parent f178ee6 commit c585c44
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 68 deletions.
6 changes: 2 additions & 4 deletions src/comm.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ static const u16 MAX_COMM_BUSY = 0x28F;

static bool countsInBounds(void);

typedef struct CommVTable CommVTable;

struct CommVTable {
typedef struct CommVTable {
void (*init)(void);
u8 (*read_ready)(void);
u8 (*read)(void);
u8 (*write_ready)(void);
void (*write)(u8 data);
};
} CommVTable;

static const CommVTable Demo_VTable = { comm_demo_init, comm_demo_read_ready, comm_demo_read,
comm_demo_write_ready, comm_demo_write };
Expand Down
3 changes: 1 addition & 2 deletions src/comm.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#pragma once
#include "types.h"

enum CommMode { Discovery, Everdrive, EverdrivePro, Serial, MegaWiFi, Demo };
typedef enum CommMode CommMode;
typedef enum CommMode { Discovery, Everdrive, EverdrivePro, Serial, MegaWiFi, Demo } CommMode;

void comm_init(void);
void comm_write(u8 data);
Expand Down
10 changes: 3 additions & 7 deletions src/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@

#define MSG_MAX_LEN 40

typedef enum LogLevel LogLevel;
typedef enum LogLevel { Info, Warn } LogLevel;

enum LogLevel { Info, Warn };

typedef struct Log Log;

struct Log {
typedef struct Log {
LogLevel level;
u16 msgLen;
char msg[MSG_MAX_LEN];
};
} Log;

void log_init(void);
void log_info(const char* fmt, ...) __attribute__((format(printf, 1, 2)));
Expand Down
11 changes: 4 additions & 7 deletions src/midi.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,19 @@
#define CHANNEL_UNASSIGNED 0xFF
#define LENGTH_OF(x) (sizeof(x) / sizeof(x[0]))

typedef enum DeviceSelect DeviceSelect;
enum DeviceSelect { Auto, FM, PSG_Tone, PSG_Noise };
typedef enum DeviceSelect { Auto, FM, PSG_Tone, PSG_Noise } DeviceSelect;

typedef struct MidiChannel MidiChannel;
struct MidiChannel {
typedef struct MidiChannel {
u8 volume;
u8 program;
u8 pan;
u16 pitchBend;
u8 prevVelocity;
NotePriorityStack notePriority;
DeviceSelect deviceSelect;
};
} MidiChannel;

typedef enum MappingMode MappingMode;
enum MappingMode { MappingMode_Static, MappingMode_Dynamic, MappingMode_Auto };
typedef enum MappingMode { MappingMode_Static, MappingMode_Dynamic, MappingMode_Auto } MappingMode;

static const VTable PSG_VTable = { midi_psg_note_on, midi_psg_note_off, midi_psg_channel_volume,
midi_psg_pitch_bend, midi_psg_program, midi_psg_all_notes_off, midi_psg_pan };
Expand Down
12 changes: 4 additions & 8 deletions src/midi.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,17 @@
#define SYSEX_COMMAND_WRITE_YM2612_REG_PART_0 0x08
#define SYSEX_COMMAND_WRITE_YM2612_REG_PART_1 0x09

typedef struct VTable VTable;

struct VTable {
typedef struct VTable {
void (*noteOn)(u8 chan, u8 pitch, u8 velocity);
void (*noteOff)(u8 chan, u8 pitch);
void (*channelVolume)(u8 chan, u8 volume);
void (*pitchBend)(u8 chan, u16 bend);
void (*program)(u8 chan, u8 program);
void (*allNotesOff)(u8 chan);
void (*pan)(u8 chan, u8 pan);
};

typedef struct DeviceChannel DeviceChannel;
} VTable;

struct DeviceChannel {
typedef struct DeviceChannel {
u8 number;
const VTable* ops;
bool noteOn;
Expand All @@ -142,7 +138,7 @@ struct DeviceChannel {
u8 volume;
u8 pan;
u16 pitchBend;
};
} DeviceChannel;

void midi_init(const FmChannel** defaultPresets, const PercussionPreset** defaultPercussionPresets,
const u8** defaultEnvelopes);
Expand Down
6 changes: 2 additions & 4 deletions src/midi_fm.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@ static const u16 FREQS[] = {
};
static const u8 FREQ_NORMAL_RANGE_OFFSET = 2;

typedef struct MidiFmChannel MidiFmChannel;

struct MidiFmChannel {
typedef struct MidiFmChannel {
u8 pitch;
u8 volume;
u8 velocity;
u8 pan;
bool percussive;
u16 pitchBend;
};
} MidiFmChannel;

static MidiFmChannel fmChannels[MAX_FM_CHANS];

Expand Down
6 changes: 2 additions & 4 deletions src/midi_fm.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
#define MIN_MIDI_PITCH 11
#define MAX_MIDI_PITCH 106

typedef struct PercussionPreset PercussionPreset;

struct PercussionPreset {
typedef struct PercussionPreset {
FmChannel channel;
u8 key;
};
} PercussionPreset;

void midi_fm_init(
const FmChannel** defaultPresets, const PercussionPreset** defaultPercussionPresets);
Expand Down
8 changes: 3 additions & 5 deletions src/midi_fm_sm.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

#define SM_OP_LEN 3

typedef struct SpecialModeOperator SpecialModeOperator;

struct SpecialModeOperator {
typedef struct SpecialModeOperator {
u8 pitch;
u16 pitchBend;
};
} SpecialModeOperator;

static SpecialModeOperator smOperators[SM_OP_LEN];

Expand Down Expand Up @@ -58,4 +56,4 @@ void midi_fm_sm_program(u8 chan, u8 program)

void midi_fm_sm_all_notes_off(u8 chan)
{
}
}
13 changes: 5 additions & 8 deletions src/midi_psg.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,7 @@ static const u8 ATTENUATIONS[] = { 15, 14, 14, 14, 13, 13, 13, 13, 12, 12, 12, 1
2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

typedef struct MidiPsgChannel MidiPsgChannel;

static u8 userDefinedEnvelope[256];
static u8* userDefinedEnvelopePtr;
static const u8** envelopes;

struct MidiPsgChannel {
typedef struct MidiPsgChannel {
u8 chanNum;
u8 key;
u8 attenuation;
Expand All @@ -53,8 +47,11 @@ struct MidiPsgChannel {
const u8* envelopeLoopStart;
bool noteReleased;
u16 pitchBend;
};
} MidiPsgChannel;

static u8 userDefinedEnvelope[256];
static u8* userDefinedEnvelopePtr;
static const u8** envelopes;
static u8 audible;
static MidiPsgChannel psgChannels[MAX_PSG_CHANS];

Expand Down
6 changes: 2 additions & 4 deletions src/pcm_sample.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#include "types.h"
#include "samples.h"

typedef struct PcmSample PcmSample;

struct PcmSample {
typedef struct PcmSample {
const u8* data;
u32 length;
u8 rate;
};
} PcmSample;

extern const PcmSample* percussionPcmSample[128];
22 changes: 7 additions & 15 deletions src/synth.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
#define STEREO_MODE_RIGHT 1
#define STEREO_MODE_LEFT 2

typedef struct Operator Operator;

struct Operator {
typedef struct Operator {
u8 multiple;
u8 detune;
u8 attackRate;
Expand All @@ -25,11 +23,9 @@ struct Operator {
u8 releaseRate;
u8 totalLevel;
u8 ssgEg;
};

typedef struct FmChannel FmChannel;
} Operator;

struct FmChannel {
typedef struct FmChannel {
u8 algorithm;
u8 feedback;
u8 stereo;
Expand All @@ -38,19 +34,15 @@ struct FmChannel {
u8 octave;
u16 freqNumber;
Operator operators[MAX_FM_OPERATORS];
};
} FmChannel;

typedef struct Global Global;

struct Global {
typedef struct Global {
u8 lfoEnable;
u8 lfoFrequency;
bool specialMode;
};

typedef enum ParameterUpdated ParameterUpdated;
} Global;

enum ParameterUpdated { Channel, Lfo, SpecialMode };
typedef enum ParameterUpdated { Channel, Lfo, SpecialMode } ParameterUpdated;

typedef void ParameterUpdatedCallback(u8 fmChan, ParameterUpdated parameterUpdated);

Expand Down

0 comments on commit c585c44

Please sign in to comment.