Author Topic: Does not name a type even though I made a typedef  (Read 1321 times)

Offline Zipdox

  • High Voltage Technician
  • ***
  • Posts: 143
  • Karma: +1/-0
    • View Profile
    • Zipdox
Does not name a type even though I made a typedef
« on: March 27, 2020, 11:58:01 PM »
For some reason I can't compile this code for Arduino.
Code: [Select]
#include <MIDI.h>

#define samplingRate 62500
#define maxPlayingNotes 2

MIDI_CREATE_DEFAULT_INSTANCE();

float midiFrequency(float note){
  return 440*(pow(2, (note-69)/12));
}

typedef struct {
    byte pitch;
    int onTime;
    int offTime;
    int hasBeenOn;
} Note;

Note getNoteTimes(byte pitch, float dutyCycle){
  float period = samplingRate/midiFrequency(pitch);
  Note finalNote = {pitch, round(dutyCycle*period), round((1-dutyCycle)*period), 0};
  return finalNote;
}

volatile Note playingNotes[maxPlayingNotes];



void handleNoteOn(byte channel, byte pitch, byte velocity){
    for(int i = 0; i < maxPlayingNotes; i++){
        if(playingNotes[i].onTime == 0){ // 0 onTime means this note isn't in use
            playingNotes[i] = getNoteTimes(pitch, 0.1);
            break;
        }
    }
    digitalWrite(13, HIGH);
}

void handleNoteOff(byte channel, byte pitch, byte velocity){
    for(int i = 0; i < maxPlayingNotes; i++){
        if(playingNotes[i].pitch == pitch){
            playingNotes[i] = {0,0,0,0}; // 0 onTime means it doesn't play
        }
    }
    digitalWrite(13, LOW);
}

//void handlePitchBend(byte channel, int bend){
//  float bendfactor = (1+ (float) bend/8190);
//  float dutyCycle = 0.1;
//  for(int i = 0; i < maxPlayingNotes; i++){
//      if(playingNotes[i].onTime == 0) continue;
//      float period = samplingRate/(bendfactor * midiNotes[playingNotes[i].pitch]);
//      playingNotes[i].onTime = round(dutyCycle*period);
//      playingNotes[i].offTime = round((1-dutyCycle)*period);
//  }
//}






void setup(){
    for(int i = 0; i<(sizeof(playingNotes)/sizeof(playingNotes[0])); i++){
        playingNotes[i] = {0,0,0,0};
    }
//  playingNotes[0] = getNoteTimes(261.6, 0.1);
//  playingNotes[1] = getNoteTimes(329.6, 0.1);
//  playingNotes[2] = getNoteTimes(392.0, 0.1);
    pinMode(9, OUTPUT);
    pinMode(13, OUTPUT);

  cli(); //stop interrupts
//  TCCR2A = 0; // set Timer/Counter Control Registers (TCCR2A/B) to 0
  TCCR2B = 0;
 
  OCR2A = 0; // = 16000000 / (freq*prescaler) - 1 (must be <256)
  // turn on CTC mode
  TCCR2A |= (1 << WGM21);
  // Set CS01 and CS00 bits for 64 prescaler
  TCCR2B |= (1 << CS20);
  // enable timer compare interrupt
  TIMSK2 |= (1 << OCIE2A);
  sei(); //allow interrupts


  MIDI.setHandleNoteOn(handleNoteOn);
  MIDI.setHandleNoteOff(handleNoteOff);
//  MIDI.setHandlePitchBend(handlePitchBend);
  MIDI.begin(MIDI_CHANNEL_OMNI);
}

void loop(){
    MIDI.read();
}

volatile bool shouldPlay;
ISR(TIMER2_COMPA_vect){

  shouldPlay = false;
 
  for(int i = 0; i < maxPlayingNotes; i++){
    if(playingNotes[i].onTime == 0) continue;
    playingNotes[i].hasBeenOn++;
    if(playingNotes[i].hasBeenOn > playingNotes[i].onTime){
        playingNotes[i].hasBeenOn = -playingNotes[i].offTime;
    }
    if(playingNotes[i].hasBeenOn >= 0){
        shouldPlay = true;
    }
  }

  if(shouldPlay){
    PORTB |= 1 << 1;
  }else{
    PORTB &= ~(1 << 1);
  }





 
//  byte ^= 1 << bit_to_flip;
//  byte &= ~(1 << bit_to_clear);
//  byte |= 1 << bit_to_set;
}

polyphonic_midi_interrupter_pitchbend:19:1: error: 'Note' does not name a type; did you mean 'tone'?
 Note getNoteTimes(byte pitch, float dutyCycle){
 ^~~~
 tone
exit status 1
'Note' does not name a type; did you mean 'tone'?

Offline klugesmith

  • High Voltage Engineer
  • ****
  • Posts: 378
  • Karma: +12/-0
    • View Profile
Re: Does not name a type even though I made a typedef
« Reply #1 on: March 28, 2020, 04:14:17 AM »
A C instructor I am not. 

Am kind of rusty with typedefs, but don't think you need one here.  What if you skip "typedef" ?

The way you said it,  Note seems to be an instance of a nameless kind of struct.
Don't we need to say something like:

struct Note {
 int x;
 int y;
};
struct Note Note1;
struct Note foo();
int bar( struct Note z );

Maybe the typedef lets you later say Note instead of struct Note, but I'm out of my element here.  STFI!

Here's one reference found immediately with DuckDuckGo:
https://stackoverflow.com/questions/9653072/return-a-struct-from-a-function-in-c
« Last Edit: March 28, 2020, 04:33:06 AM by klugesmith »

Offline Zipdox

  • High Voltage Technician
  • ***
  • Posts: 143
  • Karma: +1/-0
    • View Profile
    • Zipdox
Re: Does not name a type even though I made a typedef
« Reply #2 on: March 28, 2020, 01:29:53 PM »
A C instructor I am not. 

Am kind of rusty with typedefs, but don't think you need one here.  What if you skip "typedef" ?

The way you said it,  Note seems to be an instance of a nameless kind of struct.
Don't we need to say something like:

struct Note {
 int x;
 int y;
};
struct Note Note1;
struct Note foo();
int bar( struct Note z );

Maybe the typedef lets you later say Note instead of struct Note, but I'm out of my element here.  STFI!

Here's one reference found immediately with DuckDuckGo:
https://stackoverflow.com/questions/9653072/return-a-struct-from-a-function-in-c

It's really weird because it used to work before, but somehow now that I changed some other code it no longer does.

Offline SteveN87

  • High Voltage Enthusiast
  • *
  • Posts: 47
  • Karma: +4/-0
  • FPGA, Software Defined Radio, HV (mainly flyback)
    • View Profile
Re: Does not name a type even though I made a typedef
« Reply #3 on: March 28, 2020, 03:27:54 PM »
Does it work if you add a struct tag? e.g.:

Code: [Select]
typedef struct Note_t {
    byte pitch;
    int onTime;
    int offTime;
    int hasBeenOn;
} Note;

Offline Zipdox

  • High Voltage Technician
  • ***
  • Posts: 143
  • Karma: +1/-0
    • View Profile
    • Zipdox
Re: Does not name a type even though I made a typedef
« Reply #4 on: March 28, 2020, 04:47:45 PM »
Does it work if you add a struct tag? e.g.:

Code: [Select]
typedef struct Note_t {
    byte pitch;
    int onTime;
    int offTime;
    int hasBeenOn;
} Note;

No it doesn't. I also tried doing struct and typedef separately and it also doesn't work. I believe this might be a bug in the compiler because when I tried moving functions around it gave very weird output, there were words joined without spaces ans it was all very confusing. I may file a bug report later.

Offline klugesmith

  • High Voltage Engineer
  • ****
  • Posts: 378
  • Karma: +12/-0
    • View Profile
Re: Does not name a type even though I made a typedef
« Reply #5 on: March 28, 2020, 06:27:49 PM »
One rite of passage for coders is finding their first compiler bug,
where correct source code does not compile or behaves wrong.

Offline SteveN87

  • High Voltage Enthusiast
  • *
  • Posts: 47
  • Karma: +4/-0
  • FPGA, Software Defined Radio, HV (mainly flyback)
    • View Profile
Re: Does not name a type even though I made a typedef
« Reply #6 on: March 28, 2020, 06:45:21 PM »
Have they changed the default C standard revision to C18? (Maybe knock it down to C11 if so.)

Offline T3sl4co1l

  • High Voltage Technician
  • ***
  • Posts: 172
  • Karma: +9/-0
    • View Profile
Re: Does not name a type even though I made a typedef
« Reply #7 on: March 28, 2020, 10:15:20 PM »
Probably a missing semicolon somewhere then.

Also FYI, declarations are usually placed in headers so they are declared at the top right away; doesn't matter if you're only using it in just one file but it is a more organized style.

Tim

High Voltage Forum

Re: Does not name a type even though I made a typedef
« Reply #7 on: March 28, 2020, 10:15:20 PM »

 


* Recent Topics and Posts

post Re: Tesla Coil Show Controller Project
[Dual Resonant Solid State Tesla coils (DRSSTC)]
Max
Today at 11:13:18 AM
post Re: repair of Panasonic microwave inverter
[Electronic Circuits]
Weston
Today at 10:08:34 AM
post Re: Horrible waveforms advice--just bad test setup?
[Dual Resonant Solid State Tesla coils (DRSSTC)]
davekni
Today at 03:20:42 AM
post Re: repair of Panasonic microwave inverter
[Electronic Circuits]
Twospoons
Today at 03:16:27 AM
post 3kW Cisco Server Supply teardown and help needed
[Electronic Circuits]
Da_Stier
July 23, 2021, 09:23:11 PM
post Re: IGBT question
[Dual Resonant Solid State Tesla coils (DRSSTC)]
thedoc298
July 23, 2021, 08:05:58 PM
post Re: IGBT question
[Dual Resonant Solid State Tesla coils (DRSSTC)]
futurist
July 23, 2021, 07:02:18 PM
post Re: IGBT question
[Dual Resonant Solid State Tesla coils (DRSSTC)]
thedoc298
July 23, 2021, 05:33:57 PM
post Re: Horrible waveforms advice--just bad test setup?
[Dual Resonant Solid State Tesla coils (DRSSTC)]
GKnapp
July 23, 2021, 04:51:37 PM
post Re: Horrible waveforms advice--just bad test setup?
[Dual Resonant Solid State Tesla coils (DRSSTC)]
davekni
July 23, 2021, 03:52:46 AM
post Re: IGBT question
[Dual Resonant Solid State Tesla coils (DRSSTC)]
thedoc298
July 22, 2021, 10:59:32 PM
post Re: Horrible waveforms advice--just bad test setup?
[Dual Resonant Solid State Tesla coils (DRSSTC)]
GKnapp
July 22, 2021, 08:50:34 PM
post Re: IGBT question
[Dual Resonant Solid State Tesla coils (DRSSTC)]
Mads Barnkob
July 22, 2021, 08:47:23 PM
post Help needed - which IGBT module to buy
[Dual Resonant Solid State Tesla coils (DRSSTC)]
thedoc298
July 22, 2021, 07:01:24 PM
post Re: Horrible waveforms advice--just bad test setup?
[Dual Resonant Solid State Tesla coils (DRSSTC)]
davekni
July 22, 2021, 06:50:07 AM
post Re: Proper phase lead adjustment at the input
[Dual Resonant Solid State Tesla coils (DRSSTC)]
davekni
July 22, 2021, 06:39:37 AM
post Re: Horrible waveforms advice--just bad test setup?
[Dual Resonant Solid State Tesla coils (DRSSTC)]
alan sailer
July 21, 2021, 07:55:03 PM
post Horrible waveforms advice--just bad test setup?
[Dual Resonant Solid State Tesla coils (DRSSTC)]
GKnapp
July 21, 2021, 06:34:13 PM
post Re: Global shutter synchronized to arcs?
[DSLR]
Uspring
July 21, 2021, 05:10:18 PM
post Re: Tesla Coil Show Controller Project
[Dual Resonant Solid State Tesla coils (DRSSTC)]
Mads Barnkob
July 21, 2021, 12:42:29 PM
post Re: Proper phase lead adjustment at the input
[Dual Resonant Solid State Tesla coils (DRSSTC)]
Chrisader
July 21, 2021, 08:15:00 AM
post Re: Proper phase lead adjustment at the input
[Dual Resonant Solid State Tesla coils (DRSSTC)]
davekni
July 21, 2021, 04:57:04 AM
post Re: Micro-power continuity checker.
[Electronic Circuits]
davekni
July 21, 2021, 03:50:33 AM
post Re: Skm flyback destroyer 🤗
[Transformer (Ferrite Core)]
oneKone
July 21, 2021, 01:50:19 AM
post Proper phase lead adjustment at the input
[Dual Resonant Solid State Tesla coils (DRSSTC)]
Chrisader
July 20, 2021, 10:06:14 PM
post Re: Micro-power continuity checker.
[Electronic Circuits]
klugesmith
July 20, 2021, 06:24:29 PM
post Micro-power continuity checker.
[Electronic Circuits]
davekni
July 20, 2021, 05:37:36 AM
post Re: Help needed with SSTC. First TC build
[Solid State Tesla Coils (SSTC)]
davekni
July 20, 2021, 05:08:24 AM
post Re: Global shutter synchronized to arcs?
[DSLR]
davekni
July 20, 2021, 04:43:05 AM
post Re: Global shutter synchronized to arcs?
[DSLR]
TMaxElectronics
July 20, 2021, 12:09:36 AM
post Re: Help needed with SSTC. First TC build
[Solid State Tesla Coils (SSTC)]
AstRii
July 19, 2021, 11:13:13 PM
post Another question on CT Feedback
[Solid State Tesla Coils (SSTC)]
Maverikie
July 19, 2021, 08:56:04 PM
post Re: Sync RSG motor problems - help and thoughts needed
[Spark Gap Tesla Coils (SGTC)]
newage
July 19, 2021, 07:09:56 PM
post Re: Global shutter synchronized to arcs?
[DSLR]
klugesmith
July 19, 2021, 06:49:08 PM
post Re: Sync RSG motor problems - help and thoughts needed
[Spark Gap Tesla Coils (SGTC)]
klugesmith
July 19, 2021, 04:39:05 PM
post Global shutter synchronized to arcs?
[DSLR]
davekni
July 19, 2021, 06:32:35 AM
post Re: Sync RSG motor problems - help and thoughts needed
[Spark Gap Tesla Coils (SGTC)]
MRMILSTAR
July 19, 2021, 05:22:51 AM
post Re: Sync RSG motor problems - help and thoughts needed
[Spark Gap Tesla Coils (SGTC)]
newage
July 19, 2021, 04:24:38 AM
post Re: a typical sstc half bridge design, any considerations or tips?
[Solid State Tesla Coils (SSTC)]
eli
July 19, 2021, 12:35:18 AM
post Re: a typical sstc half bridge design, any considerations or tips?
[Solid State Tesla Coils (SSTC)]
Mads Barnkob
July 19, 2021, 12:15:43 AM
post a typical sstc half bridge design, any considerations or tips?
[Solid State Tesla Coils (SSTC)]
eli
July 18, 2021, 11:44:03 PM
post Re: HFSSTC design help and questions
[Solid State Tesla Coils (SSTC)]
eli
July 18, 2021, 09:33:40 PM
post Re: Sync RSG motor problems - help and thoughts needed
[Spark Gap Tesla Coils (SGTC)]
johnf
July 18, 2021, 09:07:15 PM
post Re: Sync RSG motor problems - help and thoughts needed
[Spark Gap Tesla Coils (SGTC)]
MRMILSTAR
July 18, 2021, 03:38:01 PM
post Best and stable version of video
[Dual Resonant Solid State Tesla coils (DRSSTC)]
Transgarp
July 18, 2021, 03:33:31 PM
post Sync RSG motor problems - help and thoughts needed
[Spark Gap Tesla Coils (SGTC)]
newage
July 18, 2021, 01:30:46 PM
post Re: DRSSTC design advise / Primary peak current calculation
[Dual Resonant Solid State Tesla coils (DRSSTC)]
Mads Barnkob
July 17, 2021, 10:05:39 AM
post Re: DRSSTC design advise / Primary peak current calculation
[Dual Resonant Solid State Tesla coils (DRSSTC)]
alan sailer
July 16, 2021, 11:01:43 PM
post Re: DRSSTC design advise / Primary peak current calculation
[Dual Resonant Solid State Tesla coils (DRSSTC)]
Martin
July 16, 2021, 09:55:50 AM
post Re: Skm flyback destroyer 🤗
[Transformer (Ferrite Core)]
Patrick
July 16, 2021, 09:54:50 AM

Sitemap 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 
SimplePortal 2.3.6 © 2008-2014, SimplePortal