void AddCell(DeviceCard forDevice)
    {
        var copy = Instantiate(prototypeCell, cellsContainer);

        copy.gameObject.SetActive(true);
        copy.Setup(forDevice);
        copy.transform.SetParent(cellsContainer);
        cells.Add(copy);
        print("added cell for device: " + forDevice.name);
        //TODO: do the things with content view so it adjust it's size
    }
Esempio n. 2
0
 private void LearnTouchedDevice()
 {
     namingView.Show((name) => {
         if (touchedDevice == null)
         {
             //that is the case
             Debug.LogError("ups... that should be solved");
             return;
         }
         print("stop device leds is called from unity viewCtrl");
         MetaWearNative.StopDeviceLeds(touchedDevice);
         MetaWearNative.RememberDevice(touchedDevice, name);
         touchedDevice = null;
         Invoke("Scan", 0.5f);
     });
 }
 public static DeviceCard fromIosSerialised(JObject serialised)
 {
     var self = new DeviceCard();
     /*Newtonsoft.Json.Linq.JObject]: {
         "signalStrength": -68,
         "signalTime": "unimplemented",
         "id": "7B3B7ECA-BF7C-C5E7-5FC5-51EC7DD9670F",
         "name": "MetaWear"
     }*/
     self.id = serialised["id"].Value<string>();
     self.name = serialised["name"].Value<string>();
     self.signalStrength = serialised["signalStrength"].Value<int>();
     var dateString = serialised["signalTime"].Value<string>();
     self.signalTime = DateTimeExt.fromiOSDate(dateString);
     return self;
 }
    private static void ProcessIosMessage(string subject, string content)
    {
        var isAboutDevicesFound = subject == MessageSubjects.foundNewDevices || subject == MessageSubjects.foundKnownDevices;

        if (isAboutDevicesFound)
        {
            var devices     = new List <DeviceCard>();
            var jsonContent = JObject.Parse(content);
            var jsonData    = jsonContent["data"] as JArray;

            var anything = jsonData.First;
            //print("ehmmm.... [" + anything.GetType() + "]: " + anything);
            foreach (var thingie in jsonData)
            {
                devices.Add(DeviceCard.fromIosSerialised(thingie as JObject));
            }
            print("deserialised " + devices.Count + " cards at ProcessIosMessage<" + subject + ">");
            if (subject == MessageSubjects.foundNewDevices)
            {
                onNewDevicesScaned?.Invoke(devices);
            }
            else if (subject == MessageSubjects.foundKnownDevices)
            {
                onKnowDevicesScaned?.Invoke(devices);
            }
        }
        else if (subject == MessageSubjects.acceleratorData)
        {
            print("HAVE DATA FROM ACCELEROMETER: " + content);
            var jContent = JObject.Parse(content);
            print("it is: " + jContent);
            var translated = AcceleratorData.fromIosSerialised(jContent);
            print("so finally we have model: " + translated);
        }
        else
        {
            print(">>> got [" + subject + "] message from iOS inside unity: \n   " + content);
        }
    }
Esempio n. 5
0
    //refactor - it's ugly with all those returns of which some are lambdas and others are real returns from this method
    private void OnNewDevicesFound(List <DeviceCard> found)
    {
        print("Unity viewCtrl is processing event: SetupDeviceViewCtrl.OnNewDevicesFound");
        void finishWithoutTouchedDevice()
        {
            if (touchedDevice != null)
            {
                MetaWearNative.StopDeviceLeds(touchedDevice);
                touchedDevice = null;
            }
            EnableSetupUI(false);
            Invoke("Scan", 1.34f);
        }

        print("ns1");
        if (found.Count == 0)
        {
            print("nothing new under the sky");
            finishWithoutTouchedDevice();
            return;
        }
        print("ns2");

        /*found.Sort((d1, d2) => {
         *  return d2.signalStrength.CompareTo(d1.signalStrength);
         * });
         * var nearest = found.First((device) => {
         *  return device.signalStrength <= 0;
         * });*/
        DeviceCard nearest     = null;
        float      highestRRSI = -255;

        foreach (var device in found)
        {
            if (device.signalStrength > highestRRSI)
            {
                highestRRSI = device.signalStrength;
                nearest     = device;
            }
        }

        if (nearest == null)
        {
            finishWithoutTouchedDevice();
            print("this should not happen...but it has alas!!!");
            return;
        }



        print("ns3");
        if (nearest.signalStrength < -55)
        {
            print("there is new device but too far away (signal= (" + nearest.signalStrength + "); " +
                  " last is: " + found.Last().signalStrength +
                  " first is: " + found.First().signalStrength);
            finishWithoutTouchedDevice();
            return;
        }
        if (touchedDevice == nearest)
        {
            return;
        }
        print("ns4");
        if (touchedDevice != null)
        {
            print("stopping device led flashing");
            MetaWearNative.StopDeviceLeds(touchedDevice);
        }
        print("ns5");
        touchedDevice = nearest;
        MetaWearNative.StartFlashingDevice(touchedDevice);
        print("starting device flashing: " + nearest.id);
        EnableSetupUI(true);
        //Invoke("Scan", 1.66f); //to check if dissapeared from range
    }
 public static void DisconnectDevice(DeviceCard device)
 {
     Debug.LogWarning("NOT_IMPLEMENTED - disconnect device");
 }
    public static void StopSensorFusionStream(DeviceCard onDevice)
    {
#if UNITY_IOS && !UNITY_EDITOR
        ios_stopSensorFusioning(onDevice.id);
#endif
    }
    public static void StopAccelerometerStream(DeviceCard onDevice)
    {
#if UNITY_IOS && !UNITY_EDITOR
        ios_stopAccelerometering(onDevice.id);
#endif
    }
    public static void RememberDevice(DeviceCard device, string withName)
    {
#if UNITY_IOS && !UNITY_EDITOR
        ios_rememberDevice(device.id, withName);
#endif
    }
    public static void StopDeviceLeds(DeviceCard device, LedColors color = LedColors.ALL)
    {
#if UNITY_IOS && !UNITY_EDITOR
        ios_stopDeviceLeds(device.id, LedColorCode(color));
#endif
    }
    public static void StartDeviceLed(DeviceCard device, LedColors color)
    {
#if UNITY_IOS && !UNITY_EDITOR
        ios_turnDeviceLedOn(device.id, LedColorCode(color));
#endif
    }
    public static void StartFlashingDevice(DeviceCard device)
    {
#if UNITY_IOS && !UNITY_EDITOR
        ios_startFlashingDevice(device.id);
#endif
    }
    public void Setup(DeviceCard forDevice)
    {
        controlled     = forDevice;
        nameLabel.text = forDevice.name;
        idLabel.text   = forDevice.id;

        redLedSwitch.SetIsOnWithoutNotify(false);
        blueLedSwitch.SetIsOnWithoutNotify(false);
        accelerometerSwitch.SetIsOnWithoutNotify(false);
        sensorFusionSwitch.SetIsOnWithoutNotify(false);

        redLedSwitch.onValueChanged.RemoveAllListeners();
        redLedSwitch.onValueChanged.AddListener((isOn) => {
            if (isOn)
            {
                MetaWearNative.StartDeviceLed(controlled, MetaWearNative.LedColors.RED);
                Debug.Log("Unity - calling to turn on red led");
            }
            else
            {
                Debug.Log("Unity - calling to turn off red led");
                MetaWearNative.StopDeviceLeds(controlled, MetaWearNative.LedColors.RED);
            }
        });
        blueLedSwitch.onValueChanged.RemoveAllListeners();
        blueLedSwitch.onValueChanged.AddListener((isOn) => {
            if (isOn)
            {
                Debug.Log("Unity - calling to turn on blue led");
                MetaWearNative.StartDeviceLed(controlled, MetaWearNative.LedColors.BLUE);
            }
            else
            {
                Debug.Log("Unity - calling to turn off blue led");
                MetaWearNative.StopDeviceLeds(controlled, MetaWearNative.LedColors.BLUE);
            }
        });
        accelerometerSwitch.onValueChanged.RemoveAllListeners();
        accelerometerSwitch.onValueChanged.AddListener((isOn) => {
            if (isOn)
            {
                Debug.Log("Unity -  calling to turn on accelerometer");
                MetaWearNative.StartAccelerometerStream(controlled);
            }
            else
            {
                Debug.Log("Unity - calling to turn off accelerometer");
                MetaWearNative.StopAccelerometerStream(controlled);
            }
        });
        sensorFusionSwitch.onValueChanged.RemoveAllListeners();
        sensorFusionSwitch.onValueChanged.AddListener((isOn) => {
            if (isOn)
            {
                Debug.Log("Unity -  calling to turn on sensor fusion");
                MetaWearNative.StartSensorFusionStream(controlled);

                //Debug.Log("NOT_IMPLEMENTED -> truning on sensor fusion");
            }
            else
            {
                Debug.Log("Unity - calling to turn off sensor fusion");
                MetaWearNative.StopSensorFusionStream(controlled);
                //Debug.Log("NOT_IMPLEMENTED -> turning off sensor fusion");
            }
        });
    }