May I ask for the verification of my code snippet?

I have built my first datafield for Edge devices.
On my Edge it runs flawlessly.

The layout meets my needs - but now I want to make it available to the general public.
For myself, I have also set the rear gear index display - but for mechanical derailleurs.
To be able to display electric ANT+ rear derailleurs as well, I have put together the following code with the help of posts in this forum and with the help of the explanations at Toybox. Unfortunately I can't check the code because I don't have an electric derailleur and I can't find a way to check it in the simulator (VS Code).

So may I ask that you take a look at the code and tell me if it will work this way?

SHIFTING.MC

using Toybox.AntPlus;


var frontGearIndex = 66;
var rearGearIndex = 99;


	class myShiftingListener extends AntPlus.ShiftingListener {

		function initialize() {
			AntPlus.ShiftingListener.initialize();
			System.println("I was here 1");
		}
		
		function onShiftingUpdate(data) {
		System.println("I was here 2");
			if (data != null) {
				if (data.frontDerailleur != null) {
					frontGearIndex = data.frontDerailleur.gearIndex;
				}	
				if (data.rearDerailleur != null) {
					rearGearIndex = data.rearDerailleur.gearIndex;
				}
			} else {
				frontGearIndex = 0;
				rearGearIndex = 0;
			}
		}

		function getFrontGearIndex() {
			return frontGearIndex;
		}
	
		function getRearGearIndex() {
			return rearGearIndex;
		}


	}

TESTAVIEW.MC

import Toybox.Activity;
import Toybox.Graphics;
import Toybox.WatchUi;
import Toybox.AntPlus;


class TestAView extends WatchUi.DataField {

    var listener;
    var curGear = 0;
    var shifting;

    function initialize() {
        DataField.initialize();
    
        listener = new myShiftingListener();
	    shifting = new AntPlus.Shifting(listener);
    }

    // The given info object contains all the current workout information:
    function compute(info as Activity.Info) as Void {
        
        //SRAM Index rear:
        curGear = myShiftingListener.getRearGearIndex();
        System.println("curGear = " + curGear);

    }   // END Compute

    // Display the value you computed: 
    function onUpdate(dc as Dc) as Void {
    
        dc.setColor(Graphics.COLOR_BLACK, Graphics.COLOR_WHITE);
        dc.drawText(140, 10, Graphics.FONT_NUMBER_MEDIUM, curGear, Graphics.TEXT_JUSTIFY_CENTER);

    }
}

I am also attaching the complete project of the snippet here.

Thank you for your help!

TestA_SRAMversuch.zip

  • In fact you don't need to use ShiftingListener class to access gear index and size.

    It is available in the info object within compute method. This is what I do to get this information

    		if(info has :frontDerailleurIndex) {
    			if(null != info.frontDerailleurIndex) {
    				_frontDerailleurIndex = info.frontDerailleurIndex;
    			}
    		}
    		
    		if(info has :frontDerailleurMax) {
    			if(null != info.frontDerailleurMax) {
    				_frontDerailleurMax = info.frontDerailleurMax;
    			}
    		}
    		
    		if(info has :frontDerailleurSize) {
    			if(null != info.frontDerailleurSize) {
    				_frontDerailleurSize = info.frontDerailleurSize;
    			}
    		}
    		
    		if(info has :rearDerailleurIndex) {
    			if(null != info.rearDerailleurIndex) {
    				_rearDerailleurIndex = info.rearDerailleurIndex;
    			}
    		}
    		
    		if(info has :rearDerailleurMax) {
    			if(null != info.rearDerailleurMax) {
    				_rearDerailleurMax = info.rearDerailleurMax;
    			}
    		}
    		
    		if(info has :rearDerailleurSize) {
    			if(null != info.rearDerailleurSize) {
    				_rearDerailleurSize = info.rearDerailleurSize;
    			}
    		}

  • Thank you for your quick response!

    As far as I have understood, DerailleirIndex via Activity.info works with Shimano electric shifters only.
    (for Shimano shifters I have already implemented this code.)

    For SRAM and other shifters one need to use Ant+ listener - at least that‘s what I understood…

  • Very nice datafield.   you should share your code when finish so that we can learn how to implement stuff... 

  • Activity.info provides front/rear derrailleur status for any system. Ant+ listener is not available for Shimano. So, in case you want to access battery status (only available via Ant+), you will need to use shiftinglistener, but  it will only work for sram.

  • Ok! That‘s good news!

    Thank you very much!