MBLPinChangeTypeRising and MBLPinChangeTypeFalling

Hi i have a problem,hope you can help
i'm trying to make a project like the metaKeg, but having problems to convert from objc to swift. and i like to make it in swift.
my problem if to get the flow meter to count the puls.
this i what i got in objc c, and it work.

- (IBAction)gpioStartPinChangePressed:(id)sender {   

    MBLGPIOPin *pin = _device.gpio.pins[1];

    if (pin == 0) {pin.changeType = MBLPinChangeTypeRising;}

else if (pin == 1) {pin.changeType = MBLPinChangeTypeFalling;}

else {pin.changeType = MBLPinChangeTypeAny;}

    [self.streamingEvents addObject:pin.changeEvent];

    [pin.changeEvent startNotificationsWithHandlerAsync:^(MBLNumericData * _Nullable obj, NSError * _Nullable error) {

        if (obj) {self.ozPouredLabel.text = [NSString stringWithFormat:@%.1f, (float)(++self.gpioPinChangeCount) * ML_PER_FLOWMETER_TICK * OZ_PER_ML];

}

}

}];

}

Comments

  • Can you attach your swift code as well? Something might of went askew in the translation.
  • her it's  
    func gpioStartPinChangePressed(sender: AnyObject) {
    var pin: MBLGPIOPin = self.device.gpio!.pins[1] as! MBLGPIOPin if pin == 0 {
    pin.changeType = MBLPinChangeTypeRising
    }
    else if pin == 1 {
    pin.changeType = MBLPinChangeTypeFalling
    }
    else {
    pin.changeType = MBLPinChangeTypeAny
    }

    self.streamingEvents.append(pin.changeEvent)
    pin.changeEvent
    {(obj: MBLNumericData, error: NSError) -> Void in
    if obj != nil {
    self.ozPouredLabel.text = String(format: "%.1f", Float(++self.gpioPinChangeCount) * ML_PER_FLOWMETER_TICK * OZ_PER_ML)
    }
    }
    }
    }
    }
  • and it's the MBLPinChangeTypeFalling and the other
  • Check out this function, it might provide some clues:   


        func gpioStartPinChangePressed(sender: AnyObject) {

            let pin = self.device.gpio!.pins[1] as! MBLGPIOPin

            

            // Here pin is an object, it isn't valid to compare it to an int

            // For kegbot, Any is the type you want

            pin.changeType = .Any

            //if pin == 0 {

            //    pin.changeType = MBLPinChangeTypeRising

            //}

            //else if pin == 1 {

            //    pin.changeType = MBLPinChangeTypeFalling

            //}

            //else {

            //    pin.changeType = MBLPinChangeTypeAny

            //}

            

            self.streamingEvents.append(pin.changeEvent)

            pin.changeEvent.startNotificationsWithHandlerAsync { obj, error in

                if obj != nil {

                    self.gpioPinChangeCount += 1

                    self.ozPouredLabel.text = String(format: "%.1f", self.gpioPinChangeCount * ML_PER_FLOWMETER_TICK * OZ_PER_ML)

                }

            }

        }

This discussion has been closed.