@jasim (Jake) posted a new topic, subject "Blocks touch data". It read:

   Hi. I'd like help reading touch data from the Lightpad Block. Roli referred me here. I'd like to use the SDK, and minimal complete software to read this information. I don't need access to connecting multiple blocks, or audio software at the moment, but the LED's would be nice.
    
    I've compiled the Xcode project into the static library and, I think, linked fine. But BlocksHeader.h sets off a bunch of errors. In the documentation here: http://developer.roli.com/documentation/the_standalone_blocks_sdk.html  
    JuceHeader.h is mentioned. Is this meant to be BlocksHeader.h? Because I don't see the former in the SDK directory. Do I need everything in juce audio_ basics/_audio_devices/_blocks_basics/_core/_events?  
    Thanks!

@matkatmusic (Matkatmusic) replied

    yes, you need the whole juce SDK. use the ProJucer to set up your projects for BLOCKS/Plugins/Audio Apps…

To which @t0m (Tom Poole) replied

    Hi @jasim,
    
    You're correct - `JuceHeader.h` should be `BlocksHeader.h` in the documentation. I'll fix that typo now.
    
    You _do not_ need the whole of JUCE to create BLOCKS apps (though it would certainly make things much easier!), the contents of the standalone SDK repository are sufficient. You'll need everything in audio_basics, audio_devices, blocks_basics, core and events.
    
    What errors are you seeing? Can you build the BlockFinder example application?

@jasim (Jake) replied

    My program is successfully recognizing the connected block (block finder is printing out its info), but when I touch the LightPad Block (and touchChanged is called), I get a bad access code on the last line of:
    
    > /** Calls a member function on each listener in the list, with 2 parameters. */  
    >  template <LL_TEMPLATE(1), LL_TEMPLATE(2)>  
    >  void call (void (ListenerClass8b7c:f320:99b9:690f:4595:cd17:293a:c069*callbackFunction) (P1, P2),  
    >  LL_PARAM(1), LL_PARAM(2)) {  
    >  for (Iterator<DummyBailOutChecker, ThisType> iter (*this); iter.next()  
    >  (iter.getListener()->*callbackFunction) (param1, param2); } // ERROR ON THIS LINE
    
    The related code in my program is a new class, roliBlockHelper, (which is just a copy of what's given here: http://developer.roli.com/documentation/getting_touch_events.html), BlockFinder.h and BlockFinder.cpp, and a couple lines in main. Here's roliBlockHelper:
    
    > class roliBlockHelper : public TouchSur8b7c:f320:99b9:690f:4595:cd17:293a:c069Listener {  
    >  public:  
    >  roliBlockHelper (Block* block){  
    >  if (auto touchSurface = block->getTouchSurface())  
    >  touchSurface->addListener (this); }
    [code] 
    >     virtual void touchChanged (TouchSurface& sourceTouchSurface, const TouchSur8b7c:f320:99b9:690f:4595:cd17:293a:c069Touch& touchEvent) override {
    >      } };
    >     
    [/code]
    
    I've also added a line to the last for loop in BlockFinder.cpp:
    
    > for (auto& block : currentTopology.blocks) {
    [code] 
    >         roliBlockHelper rbh(block);  // LINE I ADDED
    >         
    >         Logger8b7c:f320:99b9:690f:4595:cd17:293a:c069writeToLog ("");
    >         Logger8b7c:f320:99b9:690f:4595:cd17:293a:c069writeToLog (String("    Description:   ") + block->getDeviceDescription());
    >         Logger8b7c:f320:99b9:690f:4595:cd17:293a:c069writeToLog (String("    Battery level: ") + String (block->getBatteryLevel()));
    >         Logger8b7c:f320:99b9:690f:4595:cd17:293a:c069writeToLog (String("    UID:           ") + String (block->uid));
    >         Logger8b7c:f320:99b9:690f:4595:cd17:293a:c069writeToLog (String("    Serial number: ") + block->serialNumber);      }
    >     
    [/code]
    
    And in main, before my main loop, I just have:
    
    > ScopedJuceInitialiser_GUI platform;
    > 
    > BlockFinder bf;
    
    By the way, BlockFinder is printing out the following info about my block:
    
    > New BLOCKS topology.  
    >  Detected 1 blocks:
    
    > 
    [code]
    >     Description:   Pad BLOCK (2x2)
    >     Battery level: 0.548387
    >     UID:           ..
    >     Serial number: ..
    [/code]

Then @t0m (Tom Poole) said

    This is an object lifetime issue.
    
    When you are looping over each `Block` in the `currentTopology.blocks` array, any objects created within the bounds of the loop will only exist whilst you're between the opening “{” and closing “}”. For example, if you try to access your `rbh` variable outside of the loop then you'll get a compiler error and you won't be able to build your project.
    
    So the problem is that you are registering an object as a listener to the Touchsurface, then that object is being destroyed. The TouchSurface has no way of knowing this and attempts to call a function on an object that no longer exists, which causes your program to crash due to a “bad access”.
    
    The solution is to ensure that your listeners have a lifetime that lasts at least as long as the TouchSurface you're interested in.
    
    I would suggest looking at the examples in the JUCE repo, under **examples/BLOCKS/**. They all handle touch events.

So @jasim (Jake) replied

    Got it working - mostly by using the Blocks Synth example. Thanks so much for your help, t0m!


{'source': 'discourse', 'url': 'https://forum.juce.com/t/blocks-touch-data/20060', 'title': 'Blocks touch data', 'views': 770, 'has_accepted_answer': False, 'word_count': 664, 'like_count': 1, 'reply_count': 2, 'participant_count': 3, 'posts': 6, 'tags': '', 'author': '@jasim (Jake)', 'participants': '@jasim (Jake), @matkatmusic (Matkatmusic), @t0m (Tom Poole)', 'image_url': [None], 'created_at': '2016-11-27T20:38:41.930Z', 'last_posted_at': '2016-12-01T00:04:31.833Z'}