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

Offline Zipdox

  • High Voltage Enthusiast
  • *
  • Posts: 43
  • Karma: +0/-0
    • View Profile
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 Technician
  • ***
  • Posts: 186
  • Karma: +5/-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 Enthusiast
  • *
  • Posts: 43
  • Karma: +0/-0
    • View Profile
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: 23
  • Karma: +1/-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 Enthusiast
  • *
  • Posts: 43
  • Karma: +0/-0
    • View Profile
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 Technician
  • ***
  • Posts: 186
  • Karma: +5/-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: 23
  • Karma: +1/-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: 133
  • 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: Isn't there a better way to make a custom toroid?
[General Chat]
Twospoons
Today at 03:24:49 AM
post Re: SSTC Micro
[Solid State Tesla Coils (SSTC)]
Magneticitist
Today at 03:24:20 AM
post Re: MMC for a 32kvac load
[Capacitor Banks]
Rowdy
Today at 03:23:33 AM
post Re: MMC for a 32kvac load
[Capacitor Banks]
Weston
Today at 03:06:20 AM
post Re: Isn't there a better way to make a custom toroid?
[General Chat]
johnf
Today at 01:33:47 AM
post MMC for a 32kvac load
[Capacitor Banks]
Rowdy
Today at 01:02:34 AM
post Re: Isn't there a better way to make a custom toroid?
[General Chat]
Rowdy
May 24, 2020, 11:47:26 PM
post Re: GDT cable specs
[Transformer (Ferrite Core)]
costas_p
May 24, 2020, 11:09:44 PM
post Re: Welcome new members, come say hello and tell a little about yourself :)
[General Chat]
Rowdy
May 24, 2020, 10:39:45 PM
post Re: GDT cable specs
[Transformer (Ferrite Core)]
station240
May 24, 2020, 09:55:49 PM
post Re: Working Principle Of Neon Transformer Circuit
[Transformer (Ferrite Core)]
davekni
May 24, 2020, 09:55:48 PM
post Re: Welcome new members, come say hello and tell a little about yourself :)
[General Chat]
Mads Barnkob
May 24, 2020, 08:16:40 PM
post Re: GDT cable specs
[Transformer (Ferrite Core)]
thecyberman
May 24, 2020, 05:38:27 PM
post Re: Welcome new members, come say hello and tell a little about yourself :)
[General Chat]
Rowdy
May 24, 2020, 02:33:55 PM
post Re: Freewheeling diodes are burning (Now other problems)
[Solid State Tesla Coils (SSTC)]
hip424
May 24, 2020, 02:31:39 PM
post Re: Working Principle Of Neon Transformer Circuit
[Transformer (Ferrite Core)]
iraquois
May 24, 2020, 02:08:53 PM
post Re: Easy to build Polyphonic MIDI Interrupter
[Computers, Microcontrollers, Programmable Logic, Interfaces and Displays]
Max
May 24, 2020, 12:12:36 PM
post Re: Working Principle Of Neon Transformer Circuit
[Transformer (Ferrite Core)]
ritaismyconscience
May 24, 2020, 07:56:21 AM
post Re: Freewheeling diodes are burning (Now other problems)
[Solid State Tesla Coils (SSTC)]
ritaismyconscience
May 24, 2020, 03:09:57 AM
post Re: Working Principle Of Neon Transformer Circuit
[Transformer (Ferrite Core)]
davekni
May 23, 2020, 10:24:55 PM
post Re: GDT cable specs
[Transformer (Ferrite Core)]
johnf
May 23, 2020, 09:30:29 PM
post GDT cable specs
[Transformer (Ferrite Core)]
costas_p
May 23, 2020, 09:06:39 PM
post Re: Working Principle Of Neon Transformer Circuit
[Transformer (Ferrite Core)]
Mads Barnkob
May 23, 2020, 08:22:59 PM
post Re: Freewheeling diodes are burning (Now other problems)
[Solid State Tesla Coils (SSTC)]
davekni
May 23, 2020, 07:47:49 PM
post Re: Building a bigger DRSSTC
[Dual Resonant Solid State Tesla coils (DRSSTC)]
davekni
May 23, 2020, 07:41:41 PM
post Working Principle Of Neon Transformer Circuit
[Transformer (Ferrite Core)]
iraquois
May 23, 2020, 02:09:26 PM
post 600 Watt GEL Battery Charger Teardown - Signet Systems HB600-24B
[Electronic Circuits]
Mads Barnkob
May 23, 2020, 12:46:23 PM
post Re: Building a bigger DRSSTC
[Dual Resonant Solid State Tesla coils (DRSSTC)]
ritaismyconscience
May 23, 2020, 04:14:52 AM
post Re: New projet OmegaDR
[Dual Resonant Solid State Tesla coils (DRSSTC)]
ritaismyconscience
May 22, 2020, 01:15:36 AM
post Re: New projet OmegaDR
[Dual Resonant Solid State Tesla coils (DRSSTC)]
Mugi30
May 21, 2020, 11:55:37 PM
post Re: Easy to build Polyphonic MIDI Interrupter
[Computers, Microcontrollers, Programmable Logic, Interfaces and Displays]
Max
May 21, 2020, 07:46:28 PM
post Re: New projet OmegaDR
[Dual Resonant Solid State Tesla coils (DRSSTC)]
Mugi30
May 21, 2020, 09:15:57 AM
post Re: How to get rid of stuff?
[Sell / Buy / Trade]
johnf
May 21, 2020, 06:35:31 AM
post Re: How to get rid of stuff?
[Sell / Buy / Trade]
MRMILSTAR
May 21, 2020, 05:23:40 AM
post Re: Easy to build Polyphonic MIDI Interrupter
[Computers, Microcontrollers, Programmable Logic, Interfaces and Displays]
futurist
May 21, 2020, 12:16:04 AM
post Re: How to get rid of stuff?
[Sell / Buy / Trade]
klugesmith
May 20, 2020, 11:22:34 PM
post Re: Powerful easy to build MIDI Interrupter
[Computers, Microcontrollers, Programmable Logic, Interfaces and Displays]
Max
May 20, 2020, 12:15:43 PM
post Re: Portable Q(uarantine)CW Tesla Coil
[Dual Resonant Solid State Tesla coils (DRSSTC)]
SteveN87
May 19, 2020, 02:08:20 PM
post Re: New projet OmegaDR
[Dual Resonant Solid State Tesla coils (DRSSTC)]
Mugi30
May 19, 2020, 12:05:51 PM
post Re: Powerful easy to build MIDI Interrupter
[Computers, Microcontrollers, Programmable Logic, Interfaces and Displays]
Max
May 19, 2020, 11:49:02 AM
post Re: Powerful easy to build MIDI Interrupter
[Computers, Microcontrollers, Programmable Logic, Interfaces and Displays]
Mads Barnkob
May 19, 2020, 09:26:06 AM
post Re: Portable Q(uarantine)CW Tesla Coil
[Dual Resonant Solid State Tesla coils (DRSSTC)]
Weston
May 19, 2020, 07:55:54 AM
post Re: What's the best way to trigger these things?
[Capacitor Banks]
klugesmith
May 19, 2020, 01:55:11 AM
post Re: New projet OmegaDR
[Dual Resonant Solid State Tesla coils (DRSSTC)]
ritaismyconscience
May 18, 2020, 11:54:27 PM
post Re: New projet OmegaDR
[Dual Resonant Solid State Tesla coils (DRSSTC)]
Mugi30
May 18, 2020, 10:10:09 PM
post Re: New projet OmegaDR
[Dual Resonant Solid State Tesla coils (DRSSTC)]
Mugi30
May 18, 2020, 10:05:14 PM
post Re: What's the best way to trigger these things?
[Capacitor Banks]
johnf
May 18, 2020, 09:03:11 PM
post Re: What's the best way to trigger these things?
[Capacitor Banks]
John123
May 18, 2020, 07:50:08 PM
post Re: Powerful easy to build MIDI Interrupter
[Computers, Microcontrollers, Programmable Logic, Interfaces and Displays]
Max
May 18, 2020, 04:15:16 PM
post Re: Freewheeling diodes are burning (Now other problems)
[Solid State Tesla Coils (SSTC)]
hip424
May 18, 2020, 03:13:14 PM

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