private void timer1_Tick(object sender, EventArgs e) { string errmsg = ""; if (hardwaredetect == 0) { YAPI.UpdateDeviceList(ref errmsg); } hardwaredetect = (hardwaredetect + 1) % 20; YAPI.HandleEvents(ref errmsg); if (sensor == null) { sensor = YSensor.FirstSensor(); } if (sensor != null) { if (sensor.isOnline()) { label1.Text = sensor.get_friendlyName(); label2.Text = sensor.get_currentValue() + " " + sensor.get_unit(); } else { label1.Text = "OFFLINE"; label2.Text = "Sensor is offline"; sensor = null; } } }
async Task dispatcherTimer_Tick() { try { if (hardwaredetect == 0) { await YAPI.UpdateDeviceList(); } hardwaredetect = (hardwaredetect + 1) % 20; await YAPI.HandleEvents(); if (sensor == null) { sensor = YSensor.FirstSensor(); } if (sensor != null) { HwIdTextBox.Text = await sensor.get_friendlyName(); TempTextBox.Text = await sensor.get_currentValue() + await sensor.get_unit(); } } catch (YAPI_Exception ex) { HwIdTextBox.Text = "Sensor is offline"; TempTextBox.Text = "OFFLINE"; sensor = null; } }
static void Main(string[] args) { Console.WriteLine("Console dotNET Application 0.1.0"); Console.WriteLine("--------------------------------"); Console.WriteLine(""); Console.WriteLine("Using Yoctopuce lib " + YAPI.GetAPIVersion()); string errsmg = ""; if (YAPI.RegisterHub("usb", ref errsmg) != YAPI.SUCCESS) { Console.WriteLine("Unable to register the USB port :" + errsmg); return; } YSensor sensor = YSensor.FirstSensor(); if (sensor == null) { Console.WriteLine("No Yoctopuce sensor find on USB."); return; } YDisplay display = YDisplay.FirstDisplay(); if (display == null) { Console.WriteLine("No Yoctopuce display find on USB."); return; } // display clean up display.resetAll(); YDisplayLayer l1 = display.get_displayLayer(1); l1.hide(); // L1 is hidden, l2 stay visible int w = display.get_displayWidth(); int h = display.get_displayHeight(); while (sensor.isOnline() && display.isOnline()) { string value = sensor.get_currentValue() + " " + sensor.get_unit(); string name = sensor.get_friendlyName(); // display a text in the middle of the screen l1.clear(); l1.selectFont("Large.yfm"); l1.drawText(w / 2, h / 2, YDisplayLayer.ALIGN.CENTER, value); l1.selectFont("Small.yfm"); l1.drawText(w - 1, h - 1, YDisplayLayer.ALIGN.BOTTOM_RIGHT, name); display.swapLayerContent(0, 1); Console.WriteLine(name + " ->" + value); YAPI.Sleep(500, ref errsmg); } YAPI.FreeAPI(); }
protected override void moduleConfigHasChanged() { base.moduleConfigHasChanged(); _currentValue = _func.get_currentValue(); if (_currentValue == YAPI.INVALID_DOUBLE) { _currentValue = Double.NaN; } _unit = _func.get_unit(); _updateFrequency = _func.get_reportFrequency(); if (_updateFrequency == "OFF") { _updateFrequency = "auto"; } _logFrequency = _func.get_logFrequency(); _reportFrequency = _func.get_reportFrequency(); _advMode = _func.get_advMode() + 1; _resolution = _func.get_resolution(); }
async Task deviceArrival(YModule m) { string serial = await m.get_serialNumber(); Output.Text += "Device arrival : " + serial + "\n"; await m.registerLogCallback(deviceLog); await m.registerConfigChangeCallback(configChange); await m.registerBeaconCallback(beaconChange); // First solution: look for a specific type of function (eg. anButton) int fctcount = await m.functionCount(); for (int i = 0; i < fctcount; i++) { string hardwareId = serial + "." + await m.functionId(i); if (hardwareId.IndexOf(".anButton") >= 0) { Output.Text += "- " + hardwareId + "\n"; YAnButton anButton = YAnButton.FindAnButton(hardwareId); await anButton.registerValueCallback(anButtonValueChangeCallBack); } } // Alternate solution: register any kind of sensor on the device YSensor sensor = YSensor.FirstSensor(); while (sensor != null) { YModule module = await sensor.get_module(); if (await module.get_serialNumber() == serial) { string hardwareId = await sensor.get_hardwareId(); Output.Text += "- " + hardwareId + "\n"; string unit = await sensor.get_unit(); await sensor.set_userData(unit); await sensor.registerValueCallback(sensorValueChangeCallBack); await sensor.registerTimedReportCallback(sensorTimedReportCallBack); } sensor = sensor.nextSensor(); } }
static void dumpSensor(YSensor sensor) { string fmt = "dd MMM yyyy hh:mm:ss,fff"; Console.WriteLine("Using DataLogger of " + sensor.get_friendlyName()); YDataSet dataset = sensor.get_recordedData(0, 0); Console.WriteLine("loading summary... "); dataset.loadMore(); YMeasure summary = dataset.get_summary(); String line = String.Format("from {0} to {1} : min={2:0.00}{5} avg={3:0.00}{5} max={4:0.00}{5}", summary.get_startTimeUTC_asDateTime().ToString(fmt), summary.get_endTimeUTC_asDateTime().ToString(fmt), summary.get_minValue(), summary.get_averageValue(), summary.get_maxValue(), sensor.get_unit()); Console.WriteLine(line); Console.Write("loading details : 0%"); int progress = 0; do { progress = dataset.loadMore(); Console.Write(String.Format("\b\b\b\b{0,3:##0}%", progress)); } while(progress < 100); Console.WriteLine(""); List <YMeasure> details = dataset.get_measures(); foreach (YMeasure m in details) { Console.WriteLine( String.Format("from {0} to {1} : min={2:0.00}{5} avg={3:0.00}{5} max={4:0.00}{5}", m.get_startTimeUTC_asDateTime().ToString(fmt), m.get_endTimeUTC_asDateTime().ToString(fmt), m.get_minValue(), m.get_averageValue(), m.get_maxValue(), sensor.get_unit())); } }
static void deviceArrival(YModule m) { string serial = m.get_serialNumber(); Console.WriteLine("Device arrival : " + serial); m.registerLogCallback(deviceLog); m.registerConfigChangeCallback(configChange); m.registerBeaconCallback(beaconChange); // First solution: look for a specific type of function (eg. anButton) int fctcount = m.functionCount(); for (int i = 0; i < fctcount; i++) { string hardwareId = serial + "." + m.functionId(i); if (hardwareId.IndexOf(".anButton") >= 0) { Console.WriteLine("- " + hardwareId); YAnButton anButton = YAnButton.FindAnButton(hardwareId); anButton.registerValueCallback(anButtonValueChangeCallBack); } } // Alternate solution: register any kind of sensor on the device YSensor sensor = YSensor.FirstSensor(); while (sensor != null) { if (sensor.get_module().get_serialNumber() == serial) { string hardwareId = sensor.get_hardwareId(); Console.WriteLine("- " + hardwareId); string unit = sensor.get_unit(); sensor.set_userData(unit); sensor.registerValueCallback(sensorValueChangeCallBack); sensor.registerTimedReportCallback(sensorTimedReportCallBack); } sensor = sensor.nextSensor(); } }
private void DisplayValue(YSensor fct) { double value = fct.get_currentValue(); double rawvalue = fct.get_currentRawValue(); double resolution = fct.get_resolution(); string valunit = fct.get_unit(); // displays the sensor value on the ui ValueDisplayUnits.Text = valunit; if (resolution != YSensor.RESOLUTION_INVALID) { // if resolution is available on the device the use it to round the value string format = "F" + ((int)-Math.Round(Math.Log10(resolution))).ToString(); RawValueDisplay.Text = "(raw value: " + (resolution * Math.Round(rawvalue / resolution)).ToString(format) + ")"; ValueDisplay.Text = (resolution * Math.Round(value / resolution)).ToString(format); } else { ValueDisplay.Text = value.ToString(); RawValueDisplay.Text = ""; } }
// the core function : load data from datalogger to send it to the graph private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { // lets hide the graph wgile updating chart1.Visible = false; comboBox1.Enabled = false; // remove any previous timed report call back for (int i = 0; i < comboBox1.Items.Count; i++) { ((YSensor)comboBox1.Items[i]).registerTimedReportCallback(null); } // allow zooming chart1.ChartAreas[0].CursorX.Interval = 0.001; chart1.ChartAreas[0].CursorX.IsUserEnabled = true; chart1.ChartAreas[0].CursorX.IsUserSelectionEnabled = true; chart1.ChartAreas[0].CursorX.AutoScroll = true; chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true; chart1.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = true; int index = comboBox1.SelectedIndex; if (index >= 0) { clearGraph(); } YSensor s = getSelectedSensor(); if (s != null) { FirstPointDate = -1; LastPointDate = -1; // some ui control loading.Visible = true; refreshDatloggerButton(null); progressBar.Visible = true; Status.Text = "Loading data from datalogger..."; for (int i = 0; i < 100; i++) { Application.DoEvents(); // makes sure the UI changes are repainted } // load data from datalogger YDataSet data = s.get_recordedData(0, 0); int progress = data.loadMore(); while (progress < 100) { try { progressBar.Value = progress; } catch { return; } Application.DoEvents(); progress = data.loadMore(); } // sets the unit (because ° is not a ASCII-128 character, Yoctopuce temperature // sensors report unit as 'C , so we fix it). chart1.ChartAreas[0].AxisY.Title = s.get_unit().Replace("'C", "°C"); chart1.ChartAreas[0].AxisY.TitleFont = new Font("Arial", 12, FontStyle.Regular); // send the data to the graph List <YMeasure> alldata = data.get_measures(); for (int i = 0; i < alldata.Count; i++) { chart1.Series[0].Points.AddXY(UnixTimeStampToDateTime(alldata[i].get_endTimeUTC()), alldata[i].get_averageValue()); } // used to compute graph length if (alldata.Count > 0) { FirstPointDate = alldata[0].get_endTimeUTC(); LastPointDate = alldata[alldata.Count - 1].get_endTimeUTC(); } setGraphScale(); // restore UI comboBox1.Enabled = true; progressBar.Visible = false; setSensorCount(); s.set_reportFrequency("3/s"); s.registerTimedReportCallback(newSensorValue); loading.Visible = false; chart1.Visible = true; refreshDatloggerButton(s); } }