Editing lines of code in a Library file

Status
Not open for further replies.

Teenfor3

Well-known member
When I use Teensy 3.2 with the Audio board I use the standard config so Teensy just plugs into the board using the standard pins as in the datasheet.
Pins 22 and 23 are I2S pins for the audio adaptor......they are also touchread pins.

I have project that uses all 12 touchread pins, so 22 and 23 not available for I2S, so I moved it to pins 3 and 4 as shown below in lines from output_i2s.ccp

boolean origi2s = true; .......I put this at top of the .ccp file and set it true for origi2s or false for not origi2s config. this allows me to switch for each project.

I also edit these lines below (2 instances of each) in the .ccp file ...............

if (origi2s == true) CORE_PIN22_CONFIG = PORT_PCR_MUX(6); // pin 22, PTC1, I2S0_TXD0
if (origi2s == false) CORE_PIN3_CONFIG = PORT_PCR_MUX(6); // pin 3, PTC12, I2S0_TXD0

if (origi2s == true) CORE_PIN23_CONFIG = PORT_PCR_MUX(6); // pin 23, PTC2, I2S0_TX_FS (LRCLK)
if (origi2s == false) CORE_PIN4_CONFIG = PORT_PCR_MUX(6); // pin 4, PTC13, I2S0_TX_FS (LRCLK)

I have to go to the output_i2s.ccp file and edit the top line depending on which project I am compiling.

If I simply set it in my sketch the compiler tells me not declared in this scope..error.

Is there a way of calling OR setting a flag from my sketch so it will be seen in scope and I dont have to edit the .ccp file each time.

Also the way I do it means I have to set this up each time I install a new version of Teensyduino.

P.S ...perhaps I should say the original lines are........
CORE_PIN22_CONFIG = PORT_PCR_MUX(6); // pin 22, PTC1, I2S0_TXD0
and
CORE_PIN23_CONFIG = PORT_PCR_MUX(6); // pin 23, PTC2, I2S0_TX_FS (LRCLK)
I added the bit "if origi2s == true" so I could select which one .......
Is there a better way of doing this...?????
 
Last edited:
why not set them in the sketch after you initialized the controller? the pin configs can be changed at any time

have each project run a diff pin config in setup after the audio shield is initialized
 
Yes, that works, so dont need to edit library file.....Thanks
I added the lines in the Setup, ....That setting will be used for that sketch and wont be changed as it runs....???
So what happens....is it initially setup during startup from the lines in the library file and then changed by the lines in the sketch as the sketch setup runs..??
Should I take out the actual pin 22 and pin23 lines in the .cpp file or leave them as default for case when not defined in sketch
 
they are not changed at runtime once its configured (the library), so changing it after initializing library is fine, and its per sketch so your other sketch is unaffected, i am not familiar with audio yet but if there is a begin statement that sets up the pins, so you just have to put your pin changes after the begin() statement, i would leave libraries untouched, if they are ever updated and your code stops working and you end up forgetting this you might say the library doesnt work after

putting your pin changes before begin() will lead to the library overriding your changes to put it’s own

no libraries should overwrite pins during runtime unless your using custom code swapping i2c/spi/uart pins to access different peripherals like a mutex. In the case of audio library it sets it once and shouldnt be changed after, thats how it should be done. :)
 
I have Teensy 3.2 plugged into the Audio Shield using I2S pins 22 and 23. When I run the Delay example sketch audio goes to the shield via I2S on pins 22,23,9,and 11 and I hear the audio from the headphone socket on the shield.
when I add the lines to the Delay sketch Setup I still hear the Audio
CORE_PIN3_CONFIG = PORT_PCR_MUX(6); // pin 3, PTC12, I2S0_TXD0
CORE_PIN4_CONFIG = PORT_PCR_MUX(6); // pin 4, PTC13, I2S0_TX_FS (LRCLK)
So audio must still be going out using pins 22 and 23. ............ I dont have any touch pins wired so cannot check touch here.

I have another Teensy 3.2 that generates audio and sends it out to an I2S bluetooth device connected to pin3 and pin4 of teensy as well as pin9 and 11. (no shield)
This device has touch pads connected to pin22 and pin23.
when I add the lines to this sketch Setup I only hear audio hear the Audio when these lines are added to setup.....as expected ok.
CORE_PIN3_CONFIG = PORT_PCR_MUX(6); // pin 3, PTC12, I2S0_TXD0
CORE_PIN4_CONFIG = PORT_PCR_MUX(6); // pin 4, PTC13, I2S0_TX_FS (LRCLK)
and the touch pads 22 and 23 are working as touch, but I cannot check if any I2S signals is on them....?? but doesnt seem to affect them..?
This is the code from the delay example shows where I added the config lines.

So it looks like since Pin22 and Pin23 are configured from the library and Pin3 and Pin4 are configured from the sketch, then audio goes to both....???
I would probably need to setup a test rig to proove...??

Code:
    // Delay demonstration example, Teensy Audio Library
//   http://www.pjrc.com/teensy/td_libs_Audio.html
// 
// Creates a chirp on the left channel, then
// three delayed copies on the right channel.
//
// Requires the audio shield:
//   http://www.pjrc.com/store/teensy3_audio.html
//
// This example code is in the public domain.

#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioOutputAnalog        dac1;
AudioSynthWaveformSine   sine1;          //xy=158,74
AudioEffectEnvelope      envelope1;      //xy=232,156
AudioEffectDelay         delay1;         //xy=393,238
AudioMixer4              mixer1;         //xy=532,205
AudioOutputI2S           i2s1;           //xy=611,61
AudioConnection          patchCord1(sine1, envelope1);
AudioConnection          patchCord2(envelope1, delay1);
AudioConnection          patchCord3(envelope1, 0, i2s1, 0);
AudioConnection          patchCord4(delay1, 0, mixer1, 0);
AudioConnection          patchCord5(delay1, 1, mixer1, 1);
AudioConnection          patchCord6(delay1, 2, mixer1, 2);
AudioConnection          patchCord7(delay1, 3, mixer1, 3);
AudioConnection          patchCord8(mixer1, 0, i2s1, 1);
//AudioConnection          patchCord8(mixer1, 0, dac1, 0);
AudioControlSGTL5000     sgtl5000_1;     //xy=195,272
// GUItool: end automatically generated code

void setup() {
  // allocate enough memory for the delay
  AudioMemory(170);
  
  // enable the audio shield
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.6);

CORE_PIN3_CONFIG = PORT_PCR_MUX(6); // pin 3, PTC12, I2S0_TXD0 was pin22
CORE_PIN4_CONFIG = PORT_PCR_MUX(6); // pin 4, PTC13, I2S0_TX_FS (LRCLK) was pin23
  
  // configure a sine wave for the chirp
  // the original is turned on/off by an envelope effect
  // and output directly on the left channel
  sine1.frequency(1000);
  sine1.amplitude(0.6);

  // create 3 delay taps, which connect through a
  // mixer to the right channel output
  delay1.delay(0, 110);
  delay1.delay(1, 220);
  delay1.delay(2, 330);
  delay1.delay(3, 440);
  
}

void loop() {
 envelope1.noteOn();
  delay(100);
  envelope1.noteOff();
 delay(4000);
}
 
enable the touch library AFTER you set the pins, this will reconfigure the old pins as touch. If you init touch before the audio then modify pins, the touch is still overridden by the audio before you changed pins. keep it ordered:

init audio
modify pins from sketch
init touch (this takes over the old pins the audio lib used initially if you want them as touch)

libraries cannot assume a user will take over pins, they are basically set and forget, so we have to handle the way they are initialized between libraries and code :)

usually what we do when we take over a pin is clear the current one:
CORE_PINx_CONFIG = 0x0;
then we set the new pin.
This essentially disables the function of the pin and puts it in a low power state
 
when I try the Delay sketch with or without the re-config lines for pin3 and pin4 teensy continues to send audio out over i2s pin22 and 23 to the shield.
audio ony stops if I put a .... int val = touchRead(22); or Serial.println (touchRead(22); at the bottom of the loop. Audio plays 4 secs and stops after the first touch read. and the touchRead continues to work on 22 and 23 even though the shield is still connected. If I use the CORE_PIN22_CONFIG = 0x0; before the lines, this stops audio on 22, 23 altogether and sends it to 3,4 (not connected to i2s but I assume) and touchRead reads 65000 poss. due to state of the shield pins being connected.

When I do same test in my gadget that has I2S connected to pins 3 and 4 and touchpads connected to pins 22 and 23 all works......audio is sent to i2s pin3 and 4 and touch works on pins 22 and 23.

So it seems for the audio shield that i2s does not switch from pins 22 and 23 until they are used or activated as touch with touchRead. and it does not matter whether the CORE_PIN3_CONFIG = PORT_PCR_MUX(6); and CORE_PIN4_CONFIG = PORT_PCR_MUX(6); are in sketch or not.
For my TouchBox the lines need to be in the sketch and switch the audio to pins 3 and 4 whether 22 or 23 are activated as touch by touchRead or not.
That is the normal way I would be using it, I wouldnt be switching back and forth as the sketches are running.
But I can now do it within the sketch which is easier.....Thanks for your help.
 
Status
Not open for further replies.
Back
Top