xlnpwmon 0.0.3

Rust bindings for Xilinx Power Monitor
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
# xilinx-power-monitor

<p align="center">
  <a href="https://github.com/nerdneilsfield/xilinx-power-monitor"><img alt="GitHub Repo stars" src="https://img.shields.io/github/stars/nerdneilsfield/xilinx-power-monitor?style=social" /></a>
  <a href="https://github.com/nerdneilsfield/xilinx-power-monitor"><img alt="GitHub issues" src="https://img.shields.io/github/issues/nerdneilsfield/xilinx-power-monitor.svg" /></a>
  <a href="https://github.com/nerdneilsfield/xilinx-power-monitor"><img alt="GitHub last commit" src="https://img.shields.io/github/last-commit/nerdneilsfield/xilinx-power-monitor.svg" /></a>
  <a href="https://github.com/nerdneilsfield/xilinx-power-monitor"><img alt="GitHub contributors" src="https://img.shields.io/github/contributors/nerdneilsfield/xilinx-power-monitor.svg" /></a>
</p>

<p align="center">
        <a href=""><img alt="Build With C" src="https://img.shields.io/badge/Made%20with-C-1f425f.svg" /></a>
        <a href=""><img alt="Build With C++" src="https://img.shields.io/badge/Made%20with-C++-1f425f.svg" /></a>
        <a href=""><img alt="Build With Python" src="https://img.shields.io/badge/Made%20with-Python-1f425f.svg" /></a>
        <a href=""><img alt="Build With Rust" src="https://img.shields.io/badge/Made%20with-Rust-1f425f.svg" /></a>
<p align="center">

<p align="center">
  <a href="https://pypistats.org/packages/xlnpwmon"><img alt="PyPI - Downloads" src="https://img.shields.io/pypi/dw/xlnpwmon.svg" /></a>
  <a href="https://badge.fury.io/py/xlnpwmon"><img alt="PyPI version" src="https://badge.fury.io/py/xlnpwmon.svg" /></a>
  <a href="https://www.python.org/"><img alt="PyPI - Python Version" src="https://img.shields.io/pypi/pyversions/xlnpwmon.svg" /></a>
  <a href="https://pypi.org/project/xlnpwmon/"><img alt="PyPI - Format" src="https://img.shields.io/pypi/format/xlnpwmon.svg" /></a>
  <a href="/LICENSE"><img alt="GitHub" src="https://img.shields.io/github/license/nerdneilsfield/xilinx-power-monitor" /></a>
  <a href="https://snyk.io/advisor/python/xlnpwmon"><img alt="xlnpwmon" src="https://snyk.io/advisor/python/xlnpwmon/badge.svg" /></a>
  <a href="https://crates.io/crates/xlnpwmon"><img src="https://img.shields.io/crates/v/xlnpwmon.svg?colorB=319e8c" alt="Version info"></a><br>
  <a href="https://github.com/nerdneilsfield/xilinx-power-monitor/actions?query=workflow%3A%22Build%20ARM%20wheels%22"><img alt="Python" src="https://github.com/nerdneilsfield/xilinx-power-monitor/workflows/Build%20ARM%20wheels/badge.svg" /></a>
  <a href="https://github.com/nerdneilsfield/xilinx-power-monitor/actions?query=workflow%3A%22Build%20Ubuntu%20Packages%22"><img alt="Deb" src="https://github.com/nerdneilsfield/xilinx-power-monitor/workflows/Build%20Ubuntu%20Packages/badge.svg" /></a>
  <a href="https://github.com/nerdneilsfield/xilinx-power-monitor/actions?query=workflow%3A%22Publish%20Rust%20Crate%22"><img alt="Rust" src="https://github.com/nerdneilsfield/xilinx-power-monitor/workflows/Publish%20Rust%20Crate/badge.svg" /></a>
  <a href="https://github.com/nerdneilsfield/xilinx-power-monitor/actions/workflows/github-code-scanning/codeql"><img alt="CodeQL" src="https://github.com/nerdneilsfield/xilinx-power-monitor/actions/workflows/github-code-scanning/codeql/badge.svg?branch=master" /></a>
</p>

English | [中文]https://github.com/nerdneilsfield/xilinx-power-monitor/blob/master/README_CN.md

A comprehensive power monitoring library for NVIDIA Xilinx devices, available in multiple programming languages.

## Features

- Real-time power consumption monitoring
- Support for multiple programming languages (C/C++, Rust, Python)
- Easy installation through package managers
- Low-level access to power metrics
- Cross-platform support for Xilinx devices

## Installation

### Python

```bash
pip install xlnpwmon
```

### Rust

Add to your `Cargo.toml`:

```toml
[dependencies]
xlnpwmon = "0.0.3"
```

### C/C++

**For Debian/Ubuntu systems:**

Download the pre-built `.deb` package from the [Releases](https://github.com/nerdneilsfield/xilinx-power-monitor/releases) page:

```bash
sudo dpkg -i xlnpwmon_0.0.3_amd64.deb
```

**For RPM-based systems (CentOS/RHEL/Fedora/PetaLinux):**

Download the pre-built `.rpm` package from the [Releases](https://github.com/nerdneilsfield/xilinx-power-monitor/releases) page:

```bash
# For DNF-based systems (Fedora, RHEL 8+, Rocky Linux)
sudo dnf install xlnpwmon-0.1-1.aarch64.rpm

# For YUM-based systems (CentOS 7, RHEL 7)
sudo yum install xlnpwmon-0.1-1.aarch64.rpm
```

**Using CMake:**

Or use CMake to find and link the library in your project:

```cmake
find_package(xlnpwmon REQUIRED)
target_link_libraries(your_target PRIVATE xlnpwmon::xlnpwmon)  # Use shared library
# or
target_link_libraries(your_target PRIVATE xlnpwmon::static)    # Use static library

# For C++ bindings
target_link_libraries(your_target PRIVATE xlnpwmon::xlnpwmon_cpp)  # Use shared library
# or
target_link_libraries(your_target PRIVATE xlnpwmon::static_cpp)    # Use static library
```

## Usage

### Python

**Quick Start: Get Instantaneous Readings**

This shows how to get the current total power consumption, voltage, and current readings directly from the device.

```python
import xlnpwmon

# Create a power monitor instance
monitor = xlnpwmon.PowerMonitor()

# Get the latest data snapshot
try:
    data = monitor.get_latest_data()
    
    # Access total readings
    total = data['total']
    print(f"Current total power consumption: {total['power']:.2f} W")
    print(f"Current bus voltage: {total['voltage']:.2f} V")
    print(f"Current total current: {total['current']:.2f} A")
    
    # Access individual sensor readings
    print("\nIndividual Sensor Readings:")
    for sensor in data['sensors']:
        print(f"Sensor {sensor['name']}:")
        print(f"  Power: {sensor['power']:.2f} W")
        print(f"  Voltage: {sensor['voltage']:.2f} V")
        print(f"  Current: {sensor['current']:.2f} A")

except Exception as e:
    print(f"Error reading power metrics: {e}")
    print("Ensure the INA3221 device is connected and accessible (permissions?).")
```

<br/>

<details>
<summary><strong>Advanced: Monitor Power During a Task</strong></summary>

This example demonstrates how to start background power sampling before a task, stop it afterwards, and retrieve detailed statistics (min, max, average power, total energy) for the monitoring period.

```python
import xlnpwmon
import time
import numpy as np # Using numpy for a sample CPU-intensive task

def cpu_intensive_task():
    """Simulate a CPU-intensive task"""
    print("Starting CPU-intensive task...")
    # Reduced size for a quicker example run
    size = 2000
    matrix1 = np.random.rand(size, size)
    matrix2 = np.random.rand(size, size)
    # Perform matrix multiplication
    result = np.dot(matrix1, matrix2)
    print("CPU-intensive task completed.")

def monitor_power_consumption(task_func):
    """Monitor power consumption during task execution"""
    # Create a power monitor instance
    monitor = xlnpwmon.PowerMonitor()

    try:
        # Optional: Set the sampling frequency (e.g., 1000Hz)
        # Higher frequencies provide more granular data but increase overhead.
        # Check library documentation or device limits for valid/optimal values.
        monitor.set_sampling_frequency(1000)

        # Reset statistics before starting a new monitoring period
        monitor.reset_statistics()

        # Start background sampling
        print("Starting power sampling...")
        monitor.start_sampling()

        # --- Execute the task you want to monitor ---
        task_func()
        # --- Task finished ---

        # Optional: Wait briefly to ensure last samples are captured,
        # depends on task duration and sampling frequency.
        time.sleep(0.1)

        # Stop background sampling
        monitor.stop_sampling()
        print("Stopped power sampling.")

        # Get collected statistics
        stats = monitor.get_statistics()

        # --- Print the collected statistics ---
        print("\n--- Power Consumption Statistics ---")

        # Print total power consumption statistics
        if 'total' in stats and 'power' in stats['total']:
            total_stats = stats['total']['power']
            print("Total Power Consumption:")
            # Use .get() for safety in case some stats weren't computed
            print(f"  Minimum Value: {total_stats.get('min', float('nan')):.2f} W")
            print(f"  Maximum Value: {total_stats.get('max', float('nan')):.2f} W")
            print(f"  Average Value: {total_stats.get('avg', float('nan')):.2f} W")
            print(f"  Total Energy: {total_stats.get('total', float('nan')):.2f} J")
            print(f"  Sample Count: {total_stats.get('count', 0)}")
        else:
            print("Total power statistics not available.")

        # Print power consumption information for each sensor/channel
        if 'sensors' in stats:
            print("\nPower Consumption Per Sensor:")
            for sensor in stats['sensors']:
                 if 'power' in sensor:
                     sensor_stats = sensor['power']
                     print(f"\n  Sensor: {sensor.get('name', 'Unknown')}")
                     print(f"    Minimum Value: {sensor_stats.get('min', float('nan')):.2f} W")
                     print(f"    Maximum Value: {sensor_stats.get('max', float('nan')):.2f} W")
                     print(f"    Average Value: {sensor_stats.get('avg', float('nan')):.2f} W")
                     print(f"    Total Energy: {sensor_stats.get('total', float('nan')):.2f} J")
                     print(f"    Sample Count: {sensor_stats.get('count', 0)}")
        else:
             print("\nPer-sensor statistics not available.")

    except Exception as e:
        print(f"\nAn error occurred during monitoring: {e}")
        print("Ensure the INA3221 device is connected and accessible.")

# --- Run the monitoring example ---
print("Xilinx Power Monitor Example Program")
print("===================================")
monitor_power_consumption(cpu_intensive_task)

```

</details>

<br/>

### Rust

First, add `xlnpwmon` as a dependency in your `Cargo.toml`. Adjust the path or version as needed.

```toml
[dependencies]
xlnpwmon = "0.0.3"

# The examples also use these crates:
ndarray = "0.15" # For matrix example
rand = "0.8"     # For matrix example
```

**Quick Start: Get Latest Sensor Readings**

This example shows how to initialize the monitor and get a single snapshot of the current power, voltage, and current, for both the total and individual sensors. Note the use of unsafe to access per-sensor data returned via raw pointers.

```rust
use xlnpwmon::{PowerMonitor, PowerData, SensorData, Error};
use std::slice;

fn main() -> Result<(), Error> {
    println!("Xilinx Power Monitor - Rust Quick Start");
    println!("======================================");

    // Initialize the power monitor. This connects to the hardware.
    // The '?' operator propagates any errors (like device not found).
    let monitor = PowerMonitor::new()?;
    println!("Power monitor initialized successfully.");

    // Get the latest instantaneous data snapshot
    let data: PowerData = monitor.get_latest_data()?;

    // --- Access Total Aggregated Data (Safely) ---
    // The 'total' field is a regular struct within PowerData.
    println!("\n--- Total Readings ---");
    println!("Total Power: {:.2} W", data.total.power);
    println!("Bus Voltage: {:.2} V", data.total.voltage); // Often VIN
    println!("Total Current: {:.2} A", data.total.current);
    println!("Status: {}", String::from_utf8_lossy(&data.total.status).trim_matches('\0'));

    // --- Access Individual Sensor Data (Requires Unsafe) ---
    // 'data.sensors' is a raw pointer (*mut SensorData) from C.
    // We need an unsafe block to dereference it and create a safe slice.
    println!("\n--- Individual Sensor Readings ---");
    if !data.sensors.is_null() && data.sensor_count > 0 {
        // Create a safe slice from the raw pointer and count
        // SAFETY: Assumes the C library guarantees that 'data.sensors' points to valid memory
        // containing 'data.sensor_count' elements, and that this memory remains valid
        // at least for the lifetime of the 'data' variable returned by get_latest_data().
        let sensors_slice: &[SensorData] = unsafe {
            slice::from_raw_parts(data.sensors, data.sensor_count as usize)
        };

        // Now iterate over the safe slice
        for sensor in sensors_slice {
            // Convert the fixed-size u8 array (C string) to a Rust String
            // Using from_utf8_lossy is safer as it handles potential invalid UTF-8 bytes.
            let name = String::from_utf8_lossy(&sensor.name)
                           .trim_matches('\0') // Remove null padding/terminator
                           .to_string();
            let status = String::from_utf8_lossy(&sensor.status).trim_matches('\0').to_string();

            println!(
                "  Sensor: {:<15} | Pwr: {:>6.2} W | V: {:>5.2} V | I: {:>6.2} A | Online: {} | Status: {}",
                name, sensor.power, sensor.voltage, sensor.current, sensor.online, status
            );
        }
    } else {
        println!("  No individual sensor data available or pointer was null.");
    }

    // No explicit cleanup needed. The `PowerMonitor` struct implements the `Drop` trait,
    // which automatically calls the C cleanup function when `monitor` goes out of scope.
    println!("\nMonitor will be cleaned up automatically.");
    Ok(())
}
```

<br/>

<details>
<summary><strong>Advanced: Monitor Power During a Task</strong></summary>

This example demonstrates starting background sampling, running a CPU-intensive task (matrix multiplication across threads), stopping sampling, and retrieving detailed statistics. It highlights error handling with Result and the necessary unsafe block for accessing per-sensor statistics.

Dependencies needed for this example:

```toml
[dependencies]
xlnpwmon = { version = "0.0.3" } # Adjust as needed
ndarray = "0.15"
rand = "0.8"
```

```rust
use xlnpwmon::{PowerMonitor, PowerStats, SensorStats, Error};
use std::{thread, time::Duration, slice, error::Error as StdError};
use ndarray::Array2;
use rand::Rng;

// Example task parameters (adjust as needed)
const MATRIX_SIZE: usize = 1000; // Size of matrices
const NUM_THREADS: usize = 4;    // Number of concurrent tasks
const NUM_ITERATIONS: usize = 5; // Workload per thread

/// Example CPU-intensive task using ndarray for matrix multiplication
fn matrix_multiply_task(thread_id: usize) {
    // println!("Thread {} starting...", thread_id); // Optional logging
    let mut rng = rand::thread_rng();
    // Create large matrices filled with random data
    let mut a: Array2<f64> = Array2::from_shape_fn((MATRIX_SIZE, MATRIX_SIZE), |_| rng.gen());
    let b: Array2<f64> = Array2::from_shape_fn((MATRIX_SIZE, MATRIX_SIZE), |_| rng.gen());

    // Perform repeated multiplications
    for _ in 0..NUM_ITERATIONS {
        a = a.dot(&b); // Matrix multiplication
    }
    // println!("Thread {} finished.", thread_id); // Optional logging
}

// Use Box<dyn StdError> for flexible error handling in main
fn main() -> Result<(), Box<dyn StdError>> {
    println!("Xilinx Power Monitor - Rust Monitoring Example");
    println!("==========================================");

    // Initialize the power monitor
    let monitor = PowerMonitor::new()?; // Propagate errors using '?'
    println!("Power monitor initialized.");

    // Set the desired sampling frequency (e.g., 1000 Hz)
    let frequency = 1000;
    monitor.set_sampling_frequency(frequency)?;
    println!("Set sampling frequency to {} Hz.", frequency);

    // Reset any previously collected statistics
    monitor.reset_statistics()?;
    println!("Reset statistics.");

    // Start background sampling in a separate thread (managed by the C library)
    monitor.start_sampling()?;
    println!("Started power sampling...");

    // Record task start time
    let task_start_time = std::time::Instant::now();

    // --- Run the CPU-intensive task across multiple threads ---
    let mut handles = vec![];
    for i in 0..NUM_THREADS {
        let handle = thread::spawn(move || {
            matrix_multiply_task(i);
        });
        handles.push(handle);
    }
    // Wait for all threads to complete
    for handle in handles {
        handle.join().expect("Task thread panicked!");
    }
    // --- Task finished ---

    let task_duration = task_start_time.elapsed();
    println!("\nTask execution finished in: {:.2?}", task_duration);

    // Allow a brief moment for the last samples to be collected by the background thread
    thread::sleep(Duration::from_millis(100)); // Adjust if needed

    // Stop the background sampling thread
    monitor.stop_sampling()?;
    println!("Stopped power sampling.");

    // Retrieve the collected statistics
    let stats: PowerStats = monitor.get_statistics()?;

    // --- Print the Statistics ---
    println!("\n--- Power Consumption Statistics ---");

    // Print total aggregated statistics
    println!("Total Power Consumption:");
    println!("  Min Power: {:.2} W", stats.total.power.min);
    println!("  Max Power: {:.2} W", stats.total.power.max);
    println!("  Avg Power: {:.2} W", stats.total.power.avg);
    println!("  Total Energy: {:.2} J", stats.total.power.total); // Energy = Avg Power * Duration
    println!("  Sample Count: {}", stats.total.power.count);
    // You can also access stats.total.voltage and stats.total.current if needed

    // Print per-sensor statistics (requires unsafe)
    println!("\nPer-Sensor Power Consumption:");
    if !stats.sensors.is_null() && stats.sensor_count > 0 {
        // Create a safe slice from the raw pointer and count
        // SAFETY: Assumes C library guarantees pointer validity for the lifetime of 'stats'.
        let sensor_stats_slice: &[SensorStats] = unsafe {
            slice::from_raw_parts(stats.sensors, stats.sensor_count as usize)
        };

        // Iterate over the safe slice
        for sensor_stat in sensor_stats_slice {
            // Convert C char array name to Rust String
            let name = String::from_utf8_lossy(&sensor_stat.name)
                           .trim_matches('\0')
                           .to_string();

            println!("\n  Sensor: {}", name);
            println!("    Min Power: {:.2} W", sensor_stat.power.min);
            println!("    Max Power: {:.2} W", sensor_stat.power.max);
            println!("    Avg Power: {:.2} W", sensor_stat.power.avg);
            println!("    Total Energy: {:.2} J", sensor_stat.power.total);
            println!("    Sample Count: {}", sensor_stat.power.count);
            // You can also access sensor_stat.voltage and sensor_stat.current if needed
        }
    } else {
        println!("  No per-sensor statistics available or pointer was null.");
    }

    println!("\nMonitoring complete. Resources will be cleaned up.");
    // `monitor` goes out of scope here, Drop trait calls pm_cleanup()
    Ok(())
}
```

</details>

### C/C++

**Compilation**

1. **Include Header:** Add the following line to your C source files:

    ```c
    #include <xlnpwmon/xlnpwmon.h>
    // Or adjust the path based on your project structure:
    // #include "path/to/include/xlnpwmon/xlnpwmon.h"
    ```

2. **Link Library:** When compiling, you need to link against the `libxlnpwmon` library. Assuming the library and header files are installed in standard system paths or paths specified via `-L` and `-I`:

    ```bash
    # Basic compilation
    gcc your_program.c -o your_program -lxlnpwmon

    # If library/includes are in custom locations:
    # gcc your_program.c -o your_program -I/path/to/xlnpwmon/include -L/path/to/xlnpwmon/lib -lxlnpwmon

    # You can use pkg-config to find the library and include paths:
    # gcc your_program.c -o your_program -lxlnpwmon `pkg-config --libs --cflags xlnpwmon`

    # Add other libraries if needed (like pthread for threading, m for math, omp for OpenMP)
    # Example with OpenMP (like the advanced example below):
    # gcc your_program.c -o your_program -I/path/to/include -L/path/to/lib -lxlnpwmon -fopenmp -lm
    ```

**Quick Start: Get Latest Sensor Readings**

This example demonstrates the basic lifecycle: initialize the library, get a single snapshot of current sensor readings, print them, and perform the mandatory cleanup.

```c
#include <stdio.h>
#include <stdlib.h> // For EXIT_FAILURE
#include <xlnpwmon/xlnpwmon.h> // Adjust path if necessary

int main() {
    pm_handle_t handle = NULL;     // Opaque handle for the library instance
    pm_power_data_t current_data; // Struct to hold the results
    pm_error_t err;                // Variable to store error codes

    // 1. Initialize the library
    // pm_init allocates resources and discovers sensors.
    // It stores the handle needed for subsequent calls in 'handle'.
    err = pm_init(&handle);
    if (err != PM_SUCCESS) {
        // Use pm_error_string to get a readable error message
        fprintf(stderr, "ERROR: Failed to initialize xlnpwmon: %s (code: %d)\n", pm_error_string(err), err);
        return EXIT_FAILURE;
    }
    printf("Library initialized successfully.\n");

    // 2. Get the latest data snapshot
    // Pass the address of the struct; the library fills it.
    // The 'current_data.sensors' pointer will point to an internal library buffer.
    err = pm_get_latest_data(handle, &current_data);
    if (err != PM_SUCCESS) {
        fprintf(stderr, "ERROR: Failed to get latest data: %s (code: %d)\n", pm_error_string(err), err);
        pm_cleanup(handle); // Clean up resources before exiting on error
        return EXIT_FAILURE;
    }

    // 3. Print Total Aggregated Readings
    printf("\n--- Total Readings ---\n");
    printf("Total Power  : %.2f W\n", current_data.total.power);
    printf("Bus Voltage  : %.2f V\n", current_data.total.voltage);
    printf("Total Current: %.2f A\n", current_data.total.current);
    // Assume 'status' is a null-terminated C string
    printf("Status       : %s\n", current_data.total.status);

    // 4. Print Individual Sensor Readings
    printf("\n--- Individual Sensor Readings ---\n");
    // Check if the sensors pointer is valid and count is positive
    // The 'current_data.sensors' pointer is managed by the library and points
    // to 'current_data.sensor_count' elements. It's typically valid until
    // the next call to a library function that modifies this data or pm_cleanup.
    if (current_data.sensors != NULL && current_data.sensor_count > 0) {
        for (int i = 0; i < current_data.sensor_count; ++i) {
            // Access data using array indexing on the pointer
            pm_sensor_data_t* sensor = &current_data.sensors[i];
            printf("  Sensor: %-15s | Pwr: %6.2f W | V: %5.2f V | I: %6.2f A | Online: %s | Status: %s\n",
                   sensor->name,    // Assumes null-terminated string
                   sensor->power,
                   sensor->voltage,
                   sensor->current,
                   sensor->online ? "Yes" : "No",
                   sensor->status); // Assumes null-terminated string
        }
    } else {
        printf("  No individual sensor data available.\n");
    }

    // 5. Clean up library resources (MANDATORY)
    // This releases memory and stops any running background threads.
    err = pm_cleanup(handle);
    if (err != PM_SUCCESS) {
        // Log the error, but the program should still terminate.
        fprintf(stderr, "ERROR: Failed to clean up xlnpwmon cleanly: %s (code: %d)\n", pm_error_string(err), err);
        return EXIT_FAILURE; // Indicate an error occurred during cleanup
    }
    printf("\nLibrary resources cleaned up successfully.\n");

    return 0; // Success
}
```

<br/>

<details>
<summary><strong>Advanced: Monitor Power During a Task</strong></summary>

This example demonstrates the complete workflow for monitoring power consumption during a specific task. It initializes the library, configures and starts sampling, executes a CPU-intensive task (using OpenMP for parallelization), stops sampling, retrieves the collected statistics, prints them, and cleans up.

Note: Compile this example with OpenMP support enabled (e.g., `gcc -fopenmp` ...)

```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> // For usleep()
#include <time.h>   // For timing task using clock_gettime
#include <omp.h>    // For OpenMP parallel task example
#include <xlnpwmon/xlnpwmon.h> // Adjust path if necessary

// Example CPU-intensive task (Matrix Multiplication using OpenMP)
void cpu_intensive_task() {
    printf("Starting CPU-intensive task...\n");
    const int size = 1500; // Moderate size for example
    double *matrix1 = NULL, *matrix2 = NULL, *result = NULL;

    // Allocate memory for matrices
    matrix1 = (double*)malloc(size * size * sizeof(double));
    matrix2 = (double*)malloc(size * size * sizeof(double));
    result = (double*)malloc(size * size * sizeof(double));
    if (!matrix1 || !matrix2 || !result) {
        fprintf(stderr, "Task ERROR: Failed to allocate memory for matrices.\n");
        // Free any potentially allocated memory before returning
        free(matrix1);
        free(matrix2);
        free(result);
        return; // Exit the task function on allocation failure
    }

    // Initialize matrices with random data using OpenMP parallel for
    #pragma omp parallel for
    for (int i = 0; i < size * size; i++) {
        // Note: rand() is not thread-safe, but for a demo it might be acceptable.
        // For production, use thread-safe RNGs or seed per thread.
        matrix1[i] = (double)rand() / RAND_MAX;
        matrix2[i] = (double)rand() / RAND_MAX;
    }

    // Perform matrix multiplication using OpenMP parallel for with collapse
    #pragma omp parallel for collapse(2)
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            double sum = 0.0;
            for (int k = 0; k < size; k++) {
                sum += matrix1[i * size + k] * matrix2[k * size + j];
            }
            result[i * size + j] = sum;
        }
    }

    // Free allocated memory
    free(matrix1);
    free(matrix2);
    free(result);
    printf("CPU-intensive task completed.\n");
}

int main() {
    pm_handle_t handle = NULL;     // Library instance handle
    pm_power_stats_t stats;        // Struct to hold collected statistics
    pm_error_t err;                // Error code variable
    struct timespec start_time, end_time; // For timing the task execution

    printf("Xilinx Power Monitor - C Monitoring Example\n");
    printf("=========================================\n");

    // 1. Initialize the library
    err = pm_init(&handle);
    if (err != PM_SUCCESS) {
        fprintf(stderr, "ERROR: Initialization failed: %s\n", pm_error_string(err));
        return EXIT_FAILURE;
    }
    printf("Library initialized.\n");

    // 2. Set Sampling Frequency (e.g., 1000 Hz)
    int target_frequency = 1000;
    err = pm_set_sampling_frequency(handle, target_frequency);
    if (err != PM_SUCCESS) {
        fprintf(stderr, "ERROR: Failed to set sampling frequency: %s\n", pm_error_string(err));
        pm_cleanup(handle); // Cleanup before exit
        return EXIT_FAILURE;
    }
    printf("Set sampling frequency to %d Hz.\n", target_frequency);

    // 3. Reset Statistics (Recommended before starting a measurement interval)
    err = pm_reset_statistics(handle);
    if (err != PM_SUCCESS) {
        fprintf(stderr, "ERROR: Failed to reset statistics: %s\n", pm_error_string(err));
        pm_cleanup(handle);
        return EXIT_FAILURE;
    }
    printf("Statistics reset.\n");

    // 4. Start Background Sampling
    // This starts a thread managed by the library to collect data.
    err = pm_start_sampling(handle);
    if (err != PM_SUCCESS) {
        fprintf(stderr, "ERROR: Failed to start sampling: %s\n", pm_error_string(err));
        pm_cleanup(handle);
        return EXIT_FAILURE;
    }
    printf("Started power sampling...\n");

    // --- Execute the task to be monitored ---
    clock_gettime(CLOCK_MONOTONIC, &start_time); // Get time before task
    cpu_intensive_task();                        // Run the actual task
    clock_gettime(CLOCK_MONOTONIC, &end_time);   // Get time after task
    // --- Task Finished ---

    // Calculate task duration
    double task_duration_sec = (end_time.tv_sec - start_time.tv_sec) +
                               (end_time.tv_nsec - start_time.tv_nsec) / 1e9;
    printf("\nTask execution finished in: %.3f seconds\n", task_duration_sec);

    // Optional: Wait a short moment to ensure the sampling thread captures final moments.
    // Adjust duration based on sampling frequency and task nature.
    usleep(100 * 1000); // 100 milliseconds

    // 5. Stop Background Sampling
    err = pm_stop_sampling(handle);
    if (err != PM_SUCCESS) {
        // Log error, but proceed to get statistics if possible
        fprintf(stderr, "WARNING: Failed to stop sampling cleanly: %s\n", pm_error_string(err));
    } else {
        printf("Stopped power sampling.\n");
    }

    // 6. Get Collected Statistics
    // Pass address of 'stats' struct; library fills it.
    // 'stats.sensors' will point to an internal buffer managed by the library.
    err = pm_get_statistics(handle, &stats);
    if (err != PM_SUCCESS) {
        fprintf(stderr, "ERROR: Failed to get statistics: %s\n", pm_error_string(err));
        pm_cleanup(handle);
        return EXIT_FAILURE;
    }

    // --- Print Statistics ---
    printf("\n--- Power Consumption Statistics ---\n");
    printf("Total Power Consumption:\n");
    printf("  Min Power   : %.2f W\n", stats.total.power.min);
    printf("  Max Power   : %.2f W\n", stats.total.power.max);
    printf("  Avg Power   : %.2f W\n", stats.total.power.avg);
    printf("  Total Energy: %.2f J (Avg Power * Duration)\n", stats.total.power.total);
    printf("  Sample Count: %lu\n", stats.total.power.count);
    // Access total voltage/current stats via stats.total.voltage.* etc.

    printf("\nPer-Sensor Power Consumption:\n");
    if (stats.sensors != NULL && stats.sensor_count > 0) {
        for (int i = 0; i < stats.sensor_count; ++i) {
            pm_sensor_stats_t* sensor_stat = &stats.sensors[i];
            // Assume 'name' is a null-terminated C string
            printf("\n  Sensor: %s\n", sensor_stat->name);
            printf("    Min Power   : %.2f W\n", sensor_stat->power.min);
            printf("    Max Power   : %.2f W\n", sensor_stat->power.max);
            printf("    Avg Power   : %.2f W\n", sensor_stat->power.avg);
            printf("    Total Energy: %.2f J\n", sensor_stat->power.total);
            printf("    Sample Count: %lu\n", sensor_stat->power.count);
             // Access per-sensor voltage/current stats via sensor_stat->voltage.* etc.
        }
    } else {
        printf("  No per-sensor statistics available.\n");
    }

    // 7. Clean up library resources (MANDATORY)
    err = pm_cleanup(handle);
    if (err != PM_SUCCESS) {
        fprintf(stderr, "ERROR: Cleanup failed: %s\n", pm_error_string(err));
        return EXIT_FAILURE; // Indicate failure on cleanup error
    }
    printf("\nLibrary resources cleaned up successfully.\n");

    return 0; // Success
}
```

</details>

<br/>

### C++

**Compilation**

1. **Include Header:** Use the C++ wrapper header file in your source code:

    ```cpp
    #include <xlnpwmon/xlnpwmon++.hpp> // Use the C++ header
    #include <stdexcept> // For catching exceptions
    #include <iostream>  // For printing
    ```

2. **Link Library:** Compile your C++ code (ensuring C++14 or later standard is enabled) and link against the underlying `libxlnpwmon` C library:

    ```bash
    # Compile using g++ with C++14 support
    g++ your_program.cpp -o your_program -std=c++14 -lxlnpwmon

    # If library/includes are in custom locations:
    # g++ your_program.cpp -o your_program -std=c++14 -I/path/to/xlnpwmon/include -L/path/to/xlnpwmon/lib -lxlnpwmon

    # Add other necessary flags (e.g., -pthread for std::thread, Eigen paths/libs)
    # g++ your_program.cpp -o your_program -std=c++14 -I/path/to/eigen -I/path/to/include -L/path/to/lib -lxlnpwmon -pthread
    ```

**Key Features of the C++ Wrapper:**

- **RAII (Resource Acquisition Is Initialization):** The `xlnpwmon::PowerMonitor` object automatically initializes the library (`pm_init`) on creation and cleans up resources (`pm_cleanup`) upon destruction (when it goes out of scope). No manual cleanup calls are needed.
- **Exception Safety:** C API errors are converted into `std::runtime_error` exceptions, allowing for standard C++ error handling using `try...catch` blocks.

**Quick Start: Get Latest Sensor Readings (C++ Wrapper)**

This example demonstrates initializing the monitor using the C++ wrapper, getting a snapshot of current readings, printing them, and letting RAII handle cleanup.

```cpp
#include <xlnpwmon/xlnpwmon++.hpp> // C++ wrapper header
#include <iostream>
#include <vector>
#include <string>
#include <stdexcept> // For std::runtime_error
#include <cstring>   // For strnlen

// Helper function to safely convert C char array (potentially not null-terminated) to std::string
std::string c_char_to_string(const char* c_str, size_t max_len) {
    // Find the actual length of the string or stop at max_len
    size_t len = strnlen(c_str, max_len);
    return std::string(c_str, len);
}

int main() {
    try {
        // 1. Initialize: Create PowerMonitor object.
        // Constructor handles pm_init() and throws std::runtime_error on failure.
        xlnpwmon::PowerMonitor monitor;
        std::cout << "Power monitor initialized successfully (RAII)." << std::endl;

        // 2. Get Latest Data: Returns a xlnpwmon::PowerData object.
        // This object holds the data snapshot.
        xlnpwmon::PowerData data = monitor.getLatestData(); // Throws on C API error

        // 3. Access and Print Total Readings
        // Use the getTotal() method which returns a const reference to the C struct.
        const pm_sensor_data_t& total_data = data.getTotal();
        std::cout << "\n--- Total Readings ---" << std::endl;
        std::cout << "Total Power  : " << total_data.power << " W" << std::endl;
        std::cout << "Bus Voltage  : " << total_data.voltage << " V" << std::endl;
        std::cout << "Total Current: " << total_data.current << " A" << std::endl;
        // Safely convert C char array status field
        std::cout << "Status       : " << c_char_to_string(total_data.status, sizeof(total_data.status)) << std::endl;

        // 4. Access and Print Individual Sensor Readings
        std::cout << "\n--- Individual Sensor Readings ---" << std::endl;
        const pm_sensor_data_t* sensors_ptr = data.getSensors(); // Get raw C pointer
        int sensor_count = data.getSensorCount();

        // SAFETY NOTE: The pointer from getSensors() points to memory managed
        // by the underlying C library, assumed valid only temporarily. Access promptly.
        if (sensors_ptr != nullptr && sensor_count > 0) {
            for (int i = 0; i < sensor_count; ++i) {
                const pm_sensor_data_t& sensor = sensors_ptr[i]; // Access via pointer
                std::cout << "  Sensor: " << c_char_to_string(sensor.name, sizeof(sensor.name))
                          << " | Pwr: " << sensor.power << " W"
                          << " | V: " << sensor.voltage << " V"
                          << " | I: " << sensor.current << " A"
                          << " | Online: " << (sensor.online ? "Yes" : "No")
                          << " | Status: " << c_char_to_string(sensor.status, sizeof(sensor.status))
                          << std::endl;
            }
        } else {
            std::cout << "  No individual sensor data available." << std::endl;
        }

        // 5. Cleanup is Automatic!
        // When 'monitor' goes out of scope at the end of 'main' (or the try block),
        // its destructor is called, which automatically calls pm_cleanup().
        std::cout << "\nExiting scope. PowerMonitor destructor will handle cleanup." << std::endl;

    } catch (const std::runtime_error& e) {
        // Catch errors thrown by the PowerMonitor wrapper
        std::cerr << "ERROR: " << e.what() << std::endl;
        return 1; // Indicate failure
    } catch (const std::exception& e) {
        // Catch any other standard exceptions
        std::cerr << "An unexpected error occurred: " << e.what() << std::endl;
        return 1;
    }

    return 0; // Success
}
```

<br/>

<details>
<summary><strong>Advanced: Monitor Power During a Task (C++ Wrapper)</strong></summary>

This example uses the `xlnpwmon::PowerMonitor` C++ wrapper, `std::thread`, and exception handling to monitor power consumption during a parallel matrix multiplication task (using Eigen).

*Example Dependencies:* Eigen library, C++11 thread support (`-pthread`).

```cpp
#include <xlnpwmon/xlnpwmon++.hpp> // C++ wrapper
#include <iostream>
#include <vector>
#include <string>
#include <thread>         // Use std::thread
#include <chrono>         // For timing and sleep
#include <stdexcept>      // For exception handling
#include <Eigen/Dense>    // For Eigen matrix task
#include <cstring>        // For strnlen

// Example Task Parameters
const int MATRIX_SIZE = 1000; // Adjust based on system memory/CPU
const int NUM_THREADS = 4;    // Number of threads for the task
const int NUM_ITERATIONS = 5; // Workload per thread

// Helper function to safely convert C char array to std::string
std::string c_char_to_string(const char* c_str, size_t max_len) {
    size_t len = strnlen(c_str, max_len);
    return std::string(c_str, len);
}

// Example CPU-intensive task using Eigen library
void eigen_matrix_task(int thread_id) {
    // std::cout << "Thread " << thread_id << " starting Eigen task..." << std::endl;
    Eigen::MatrixXd a = Eigen::MatrixXd::Random(MATRIX_SIZE, MATRIX_SIZE);
    Eigen::MatrixXd b = Eigen::MatrixXd::Random(MATRIX_SIZE, MATRIX_SIZE);
    for (int i = 0; i < NUM_ITERATIONS; ++i) {
        a = a * b; // Perform matrix multiplication
    }
    // std::cout << "Thread " << thread_id << " finished Eigen task." << std::endl;
}

int main() {
    try {
        // 1. Initialize PowerMonitor (RAII handles pm_init)
        xlnpwmon::PowerMonitor monitor;
        std::cout << "Power monitor initialized." << std::endl;

        // 2. Configure Sampling
        int frequency = 1000;
        monitor.setSamplingFrequency(frequency); // Throws on error
        std::cout << "Set sampling frequency to " << frequency << " Hz." << std::endl;

        // 3. Reset Statistics
        monitor.resetStatistics(); // Throws on error
        std::cout << "Statistics reset." << std::endl;

        // 4. Start Background Sampling
        monitor.startSampling(); // Throws on error
        std::cout << "Started power sampling..." << std::endl;

        // --- Execute the Parallel Task ---
        auto task_start_time = std::chrono::high_resolution_clock::now();

        std::vector<std::thread> task_threads;
        task_threads.reserve(NUM_THREADS);
        for (int i = 0; i < NUM_THREADS; ++i) {
            // Use std::thread for C++ concurrency
            task_threads.emplace_back(eigen_matrix_task, i);
        }
        // Wait for all task threads to complete
        for (auto& t : task_threads) {
            if (t.joinable()) {
                t.join();
            }
        }

        auto task_end_time = std::chrono::high_resolution_clock::now();
        std::chrono::duration<double> task_duration = task_end_time - task_start_time;
        std::cout << "\nTask execution finished in: " << task_duration.count() << " seconds" << std::endl;
        // --- Task Finished ---

        // Optional pause for final sample collection
        std::this_thread::sleep_for(std::chrono::milliseconds(100));

        // 5. Stop Background Sampling
        monitor.stopSampling(); // Throws on error
        std::cout << "Stopped power sampling." << std::endl;

        // 6. Get Collected Statistics
        // Returns a xlnpwmon::PowerStats object.
        xlnpwmon::PowerStats stats = monitor.getStatistics(); // Throws on error

        // --- Print Statistics ---
        std::cout << "\n--- Power Consumption Statistics ---" << std::endl;
        const pm_sensor_stats_t& total_stats = stats.getTotal();
        std::cout << "Total Power Consumption:" << std::endl;
        std::cout << "  Min Power   : " << total_stats.power.min << " W" << std::endl;
        std::cout << "  Max Power   : " << total_stats.power.max << " W" << std::endl;
        std::cout << "  Avg Power   : " << total_stats.power.avg << " W" << std::endl;
        std::cout << "  Total Energy: " << total_stats.power.total << " J" << std::endl;
        std::cout << "  Sample Count: " << total_stats.power.count << std::endl;

        std::cout << "\nPer-Sensor Power Consumption:" << std::endl;
        const pm_sensor_stats_t* sensors_stats_ptr = stats.getSensors();
        int sensor_count = stats.getSensorCount();

        // SAFETY NOTE: Access pointer promptly, assumes temporary validity.
        if (sensors_stats_ptr != nullptr && sensor_count > 0) {
            for (int i = 0; i < sensor_count; ++i) {
                const pm_sensor_stats_t& sensor_stat = sensors_stats_ptr[i];
                std::cout << "\n  Sensor: " << c_char_to_string(sensor_stat.name, sizeof(sensor_stat.name)) << std::endl;
                std::cout << "    Min Power   : " << sensor_stat.power.min << " W" << std::endl;
                std::cout << "    Max Power   : " << sensor_stat.power.max << " W" << std::endl;
                std::cout << "    Avg Power   : " << sensor_stat.power.avg << " W" << std::endl;
                std::cout << "    Total Energy: " << sensor_stat.power.total << " J" << std::endl;
                std::cout << "    Sample Count: " << sensor_stat.power.count << std::endl;
            }
        } else {
            std::cout << "  No per-sensor statistics available." << std::endl;
        }

        // 7. Cleanup is Automatic (RAII)
        std::cout << "\nMonitoring complete. Resources automatically cleaned up." << std::endl;
        // 'monitor' destructor called automatically when main returns.

    } catch (const std::runtime_error& e) {
        std::cerr << "ERROR: " << e.what() << std::endl;
        return 1;
    } catch (const std::exception& e) {
        std::cerr << "An unexpected error occurred: " << e.what() << std::endl;
        return 1;
    }

    return 0;
}

```

</details>

<br/>

## API Documentation

### Python

<details>
<summary><strong>API Reference</strong></summary>

Here are the primary methods available on the `PowerMonitor` class:

```python
class PowerMonitor:
    def __init__(self) -> None:
        """
        Initializes the connection to the power monitor hardware (e.g., INA3221 via I2C).
        May raise an exception if the device cannot be found or accessed.
        """
        pass # Actual implementation omitted

    def get_power_consumption(self) -> float:
        """
        Reads the device for the current total power consumption across relevant channels.
        Returns:
            float: Instantaneous total power in Watts.
        """
        pass

    def get_voltage(self) -> float:
        """
        Reads the device for the current bus voltage (typically from a specific channel like VIN).
        Returns:
            float: Instantaneous voltage in Volts.
        """
        pass

    def get_current(self) -> float:
        """
        Reads the device for the current total shunt current across relevant channels.
        Returns:
            float: Instantaneous total current in Amperes.
        """
        pass

    def set_sampling_frequency(self, frequency_hz: int) -> None:
        """
        Sets the target frequency for background sampling when monitoring.
        Args:
            frequency_hz (int): Desired samples per second (e.g., 100, 1000).
                                The actual achievable rate may be limited by hardware/system load.
        """
        pass

    def start_sampling(self) -> None:
        """
        Starts a background thread or process to continuously sample power data
        at the configured frequency. Statistics are accumulated internally.
        Requires `stop_sampling()` to be called later.
        """
        pass

    def stop_sampling(self) -> None:
        """
        Stops the background sampling process started by `start_sampling()`.
        """
        pass

    def reset_statistics(self) -> None:
        """
        Clears all internally accumulated statistics (min, max, sum for average, energy, count).
        Call this before `start_sampling()` to measure a specific interval.
        """
        pass

    def get_statistics(self) -> dict:
        """
        Retrieves the power statistics collected since the last reset or initialization.
        Best used after `start_sampling()` and `stop_sampling()`.

        Returns:
            dict: A dictionary containing aggregated ('total') and per-sensor ('sensors')
                  statistics. See the structure documented below. Returns empty or
                  partially filled dict if sampling didn't run or failed.
        """
        pass

    def get_power_summary(self) -> dict:
        """
        Retrieves the latest power consumption summary for PS, PL, and Total.

        Returns:
            dict: A dictionary containing:
                - 'ps_total_power' (float): Processing System total power in Watts
                - 'pl_total_power' (float): Programmable Logic total power in Watts
                - 'total_power' (float): Total system power in Watts
        """
        pass

    def get_power_summary_stats(self) -> dict:
        """
        Retrieves power statistics summary for PS, PL, and Total.

        Returns:
            dict: A dictionary containing statistics for each subsystem:
                - 'ps_total_power' (dict): PS power stats with min, max, avg, total, count
                - 'pl_total_power' (dict): PL power stats with min, max, avg, total, count
                - 'total_power' (dict): Total power stats with min, max, avg, total, count
        """
        pass
```

</details>

<br/>

<details>
<summary><strong>Statistics Data Structure (`get_statistics()` return value)</strong></summary>

The `get_statistics()` method returns a dictionary structured as follows:

```python
{
    'total': {  # Statistics aggregated across relevant power-supplying sensors
        'power': {
            'min': float,   # Minimum total power observed during sampling (Watts)
            'max': float,   # Maximum total power observed during sampling (Watts)
            'avg': float,   # Average total power over the sampling period (Watts)
            'total': float, # Total energy consumed during the period (Joules)
                            # Calculated from average power and duration.
            'count': int    # Number of samples contributing to the total statistics.
        }
        # Note: May potentially include 'voltage' and 'current' keys
        # if these are also aggregated and tracked.
    },
    'sensors': [  # A list containing statistics for each individual monitored sensor/channel
        {
            'name': str,    # Name identifying the sensor (e.g., "VDD_GPU_SOC", "VDD_CPU_CV", "VIN_SYS_5V0")
                            # Names depend on Xilinx model and INA3221 configuration.
            'power': {
                'min': float,   # Minimum power for this specific sensor (Watts)
                'max': float,   # Maximum power for this specific sensor (Watts)
                'avg': float,   # Average power for this specific sensor (Watts)
                'total': float, # Total energy for this specific sensor (Joules)
                'count': int    # Number of samples collected for this sensor.
            },
            # Note: May potentially include 'voltage' and 'current' keys
            # if these are monitored per sensor.
        },
        # ... potentially more sensor dictionaries
    ]
}
```

**Important Notes:**

- The exact sensor names available in the `'sensors'` list depend on the specific Xilinx board model and how the INA3221 channels are configured and named within the library.
- The `'total'` energy is typically calculated based on the average power (`avg`) and the duration of the sampling period (derived from `count` and the sampling frequency).
- If `start_sampling()`/`stop_sampling()` were not used, or if data collection failed, the returned dictionary might be empty, partially filled, or contain default values like `0` or `NaN`. Robust code should handle potentially missing keys or non-numeric values (e.g., using `.get()` with defaults as shown in the monitoring example).

</details>

### Rust

<details>
<summary><strong> Rust API Reference</strong></summary>

**Structs & Enums:**

- `PowerMonitor`: The main interface to the library. Manages the C handle and ensures cleanup via the `Drop` trait.
- `SensorType`: Enum identifying the type of sensor (`Unknown`, `I2C`, `System`).
- `SensorData`: Holds *instantaneous* data for one sensor.
  - `name: [u8; 64]`: Sensor name (C string, needs conversion).
  - `type_: SensorType`: Type of the sensor.
  - `voltage: f64`, `current: f64`, `power: f64`: Measured values.
  - `online: bool`: Whether the sensor is currently readable.
  - `status: [u8; 32]`: Status message (C string, needs conversion).
  - `warning_threshold: f64`, `critical_threshold: f64`: Thresholds in Watts.
- `Stats`: Holds statistics (min, max, avg, total, count) for a single metric (like power, voltage, or current).
- `SensorStats`: Holds statistics for one sensor, containing `Stats` for voltage, current, and power.
  - `name: [u8; 64]`: Sensor name (C string, needs conversion).
  - `voltage: Stats`, `current: Stats`, `power: Stats`.
- `PowerData`: Holds instantaneous data snapshot.
  - `total: SensorData`: Aggregated data across relevant sensors.
  - `sensors: *mut SensorData`: **Raw pointer** to an array of `SensorData`. **Requires `unsafe`** to access.
  - `sensor_count: i32`: Number of elements in the `sensors` array.
- `PowerStats`: Holds accumulated statistics.
  - `total: SensorStats`: Aggregated stats across relevant sensors.
  - `sensors: *mut SensorStats`: **Raw pointer** to an array of `SensorStats`. **Requires `unsafe`** to access.
  - `sensor_count: i32`: Number of elements in the `sensors` array.
- `Error`: Enum representing possible error codes from the underlying C library (e.g., `InitFailed`, `NotRunning`, `NoSensors`). Implements `From<i32>` and `Into<i32>`.

**`PowerMonitor` Methods:**

- `PowerMonitor::new() -> Result<Self, Error>`: Creates and initializes the monitor instance. Connects to hardware.
- `set_sampling_frequency(&self, frequency_hz: i32) -> Result<(), Error>`: Sets the target sampling frequency in Hz for background monitoring.
- `get_sampling_frequency(&self) -> Result<i32, Error>`: Gets the currently configured sampling frequency.
- `start_sampling(&self) -> Result<(), Error>`: Starts background sampling thread. Statistics begin accumulating.
- `stop_sampling(&self) -> Result<(), Error>`: Stops the background sampling thread.
- `is_sampling(&self) -> Result<bool, Error>`: Returns `true` if background sampling is currently active.
- `get_latest_data(&self) -> Result<PowerData, Error>`: Fetches the most recent instantaneous readings. **Return value (`PowerData`) contains raw pointers requiring `unsafe` access.** See "Data Structures & Safety Notes".
- `get_statistics(&self) -> Result<PowerStats, Error>`: Fetches the statistics accumulated since the last `reset_statistics()` or initialization. **Return value (`PowerStats`) contains raw pointers requiring `unsafe` access.** See "Data Structures & Safety Notes".
- `reset_statistics(&self) -> Result<(), Error>`: Resets all internal statistics counters (min, max, avg, total, count) to zero.
- `get_sensor_count(&self) -> Result<i32, Error>`: Returns the number of sensors detected by the library.
- `get_sensor_names(&self) -> Result<Vec<String>, Error>`: Returns a `Vec<String>` containing the names of all detected sensors. Handles C string conversion internally.

**Error Handling:**

- All methods that interact with the C library return `Result<T, xlnpwmon::Error>`.
- Use standard Rust error handling (e.g., `match`, `if let Ok/Err`, `?` operator) to check for and handle potential errors like device access failures, invalid states, etc.

**Resource Management:**

- The `PowerMonitor` struct implements the `Drop` trait. When a `PowerMonitor` instance goes out of scope, its `drop` method is automatically called, which in turn calls the C library's cleanup function (`pm_cleanup`). You do **not** need to call a cleanup function manually.

</details>

<br/>

<details>
<summary><strong>Data Structures & Safety Notes</strong></summary>

**Working with Raw Pointers in `PowerData` and `PowerStats`**

The C library returns arrays of sensor data/statistics via raw pointers (`*mut SensorData` or `*mut SensorStats`). The Rust wrapper exposes these directly within the `PowerData` and `PowerStats` structs.

**Accessing this data requires `unsafe` blocks in your code.** The recommended way is to create a temporary, safe Rust slice from the raw pointer and count:

```rust
use std::slice;
use xlnpwmon::{PowerStats, SensorStats, Error}; // Assuming these are defined

fn print_sensor_stats(stats: &PowerStats) -> Result<(), Error> {
    // Check if the pointer is valid and count is positive
    if !stats.sensors.is_null() && stats.sensor_count > 0 {
        // SAFETY: This block assumes the C library guarantees that:
        // 1. `stats.sensors` points to valid memory.
        // 2. The memory contains exactly `stats.sensor_count` initialized `SensorStats` elements.
        // 3. This memory remains valid for the lifetime of the `stats` reference.
        // The caller must uphold these invariants.
        let sensor_stats_slice: &[SensorStats] = unsafe {
            slice::from_raw_parts(stats.sensors, stats.sensor_count as usize)
        };

        // Now 'sensor_stats_slice' is a safe slice you can iterate over
        for sensor_stat in sensor_stats_slice {
            // Process each sensor_stat safely here...
            // Remember to handle the C string 'name' field (see below)
             let name = String::from_utf8_lossy(&sensor_stat.name).trim_matches('\0').to_string();
             println!("Sensor: {}, Avg Power: {:.2} W", name, sensor_stat.power.avg);
        }
    } else {
        println!("No per-sensor statistics available.");
    }
    Ok(())
}

```

*The same pattern applies when accessing `sensors` within a `PowerData` struct.*

**Working with C Strings (`name` and `status` fields)**

Struct fields like `name` (`[u8; 64]`) and `status` (`[u8; 32]`) are fixed-size byte arrays intended to hold C-style null-terminated strings (or potentially just padded with nulls).

To safely convert them to a Rust `String`:

1. Use `String::from_utf8_lossy()`: This handles potential invalid UTF-8 sequences gracefully by replacing them with the  character.
2. Use `.trim_matches('\0')`: This removes any leading/trailing null bytes used for padding or termination in the C buffer.

```rust
use xlnpwmon::SensorData; // Assuming SensorData has a name: [u8; 64]

fn get_name(sensor_data: &SensorData) -> String {
    String::from_utf8_lossy(&sensor_data.name) // Handles invalid UTF-8
        .trim_matches('\0')                  // Removes null padding/terminator
        .to_string()                         // Converts Cow<str> to String
}
```

**Overall Safety**

- This Rust wrapper aims to be safe where possible (using `Result`, `Drop` for cleanup).
- However, **direct interaction with the C library via FFI inherently involves `unsafe` operations**, especially when dealing with raw pointers returned from C (`get_latest_data`, `get_statistics`).
- **It is the user's responsibility** to understand the memory management and lifetime guarantees provided by the underlying C library when working within `unsafe` blocks. Incorrect assumptions can lead to undefined behavior (crashes, memory corruption). Always consult the C library's documentation if available.

</details>

### C/C++

<details>
<summary><strong>API Reference</strong></summary>

**Handle Type:**

- `pm_handle_t`: An opaque pointer (`struct pm_handle_s*`) representing an initialized instance of the library. Returned by `pm_init()` and required by most other functions. Must be passed to `pm_cleanup()` to release resources.

**Enums:**

- `pm_error_t`: Integer error codes. `PM_SUCCESS` (0) indicates success. Negative values indicate errors. See `pm_error_string()` to get descriptions.
  - `PM_SUCCESS = 0`
  - `PM_ERROR_INIT_FAILED = -1`
  - `PM_ERROR_NOT_INITIALIZED = -2`
  - `PM_ERROR_ALREADY_RUNNING = -3`
  - `PM_ERROR_NOT_RUNNING = -4`
  - `PM_ERROR_INVALID_FREQUENCY = -5`
  - `PM_ERROR_NO_SENSORS = -6`
  - `PM_ERROR_FILE_ACCESS = -7`
  - `PM_ERROR_MEMORY = -8`
  - `PM_ERROR_THREAD = -9`
- `pm_sensor_type_t`: Identifies the type of power sensor.
  - `PM_SENSOR_TYPE_UNKNOWN = 0`
  - `PM_SENSOR_TYPE_I2C = 1` (e.g., INA3221)
  - `PM_SENSOR_TYPE_SYSTEM = 2` (e.g., sysfs power supply class)

**Data Structures:**

- `pm_sensor_data_t`: Holds *instantaneous* data for a single sensor.
  - `char name[64]`: Null-terminated sensor name.
  - `pm_sensor_type_t type`: Sensor type.
  - `double voltage`, `current`, `power`: Measured values (V, A, W).
  - `bool online`: Indicates if the sensor is currently readable.
  - `char status[32]`: Null-terminated status string (e.g., "OK").
  - `double warning_threshold`, `critical_threshold`: Power thresholds (W).
- `pm_stats_t`: Holds basic statistics for a metric.
  - `double min`, `max`, `avg`: Min, Max, Average values.
  - `double total`: Sum of values (can be used to calculate energy for power: Energy = Avg Power * Duration).
  - `uint64_t count`: Number of samples collected.
- `pm_sensor_stats_t`: Holds statistics for a single sensor.
  - `char name[64]`: Null-terminated sensor name.
  - `pm_stats_t voltage`, `current`, `power`: Statistics for each metric.
- `pm_power_data_t`: Structure filled by `pm_get_latest_data`.
  - `pm_sensor_data_t total`: Aggregated instantaneous data.
  - `pm_sensor_data_t* sensors`: Pointer to an array of individual sensor data. **Memory is managed by the library.** The pointer is valid until the next relevant library call or `pm_cleanup`. Do not free this pointer.
  - `int sensor_count`: Number of valid elements in the `sensors` array.
- `pm_power_stats_t`: Structure filled by `pm_get_statistics`.
  - `pm_sensor_stats_t total`: Aggregated statistics.
  - `pm_sensor_stats_t* sensors`: Pointer to an array of individual sensor statistics. **Memory is managed by the library.** The pointer is valid until the next relevant library call or `pm_cleanup`. Do not free this pointer.
  - `int sensor_count`: Number of valid elements in the `sensors` array.

**Core Functions:**

- `pm_error_t pm_init(pm_handle_t* handle)`:
  - Initializes the library, discovers sensors, allocates resources.
  - Stores the opaque library instance handle at the address provided by `handle`.
  - **Must be called first.** Returns `PM_SUCCESS` on success.
- `pm_error_t pm_cleanup(pm_handle_t handle)`:
  - Stops sampling (if active) and frees all resources associated with the `handle`.
  - **Must be called** when finished with the library to prevent resource leaks.
- `const char* pm_error_string(pm_error_t error)`:
  - Returns a constant, human-readable string describing the given error code. Do not modify or free the returned string.

**Sampling Control & Status:**

- `pm_error_t pm_set_sampling_frequency(pm_handle_t handle, int frequency_hz)`:
  - Sets the target sampling frequency (in Hz) for the background monitoring thread. Must be > 0.
- `pm_error_t pm_get_sampling_frequency(pm_handle_t handle, int* frequency_hz)`:
  - Retrieves the currently configured sampling frequency, storing it at the address `frequency_hz`.
- `pm_error_t pm_start_sampling(pm_handle_t handle)`:
  - Starts the background sampling thread. Statistics begin accumulating. Returns `PM_ERROR_ALREADY_RUNNING` if already started.
- `pm_error_t pm_stop_sampling(pm_handle_t handle)`:
  - Stops the background sampling thread. Returns `PM_ERROR_NOT_RUNNING` if not running.
- `pm_error_t pm_is_sampling(pm_handle_t handle, bool* is_sampling)`:
  - Checks if the background sampling thread is active, storing the result (`true` or `false`) at the address `is_sampling`.

**Data & Statistics Retrieval:**

- `pm_error_t pm_get_latest_data(pm_handle_t handle, pm_power_data_t* data)`:
  - Fills the user-provided `data` structure with the most recent instantaneous sensor readings.
  - The `data->sensors` pointer will point to an internal library buffer.
- `pm_error_t pm_get_statistics(pm_handle_t handle, pm_power_stats_t* stats)`:
  - Fills the user-provided `stats` structure with statistics accumulated since the last reset.
  - The `stats->sensors` pointer will point to an internal library buffer.
- `pm_error_t pm_reset_statistics(pm_handle_t handle)`:
  - Resets all accumulated statistics (min, max, avg, total, count) to zero.

**Sensor Information:**

- `pm_error_t pm_get_sensor_count(pm_handle_t handle, int* count)`:
  - Gets the total number of sensors detected by the library.
- `pm_error_t pm_get_sensor_names(pm_handle_t handle, char** names, int* count)`:
  - Fills a **caller-allocated** array of C strings (`char* names[]`) with the names of detected sensors.
  - `names`: Pointer to an array of `char*`. The caller must allocate this array. Each `char*` in the array must also point to a caller-allocated buffer (e.g., `char name_buffer[64]`) large enough to hold a sensor name.
  - `count`: `[inout]` parameter. On input, points to the allocated size of the `names` array. On output, points to the actual number of names written.
  - **Note:** This function requires careful memory management by the caller. Accessing names via `pm_get_latest_data` or `pm_get_statistics` (using the `sensors[i].name` field) is often simpler as the library manages those strings.

</details>

### C++ Bindings

<details>
<summary><strong>API Reference (C++ Wrapper)</strong></summary>

**Namespace:** `xlnpwmon`

**Main Class:** `PowerMonitor`

- **Description:** An RAII wrapper class for managing the `xlnpwmon` C library. It handles initialization (`pm_init`) in its constructor and cleanup (`pm_cleanup`) in its destructor automatically. It converts C API error codes into `std::runtime_error` exceptions.
- **Resource Management:** Non-copyable, but movable. Uses `std::unique_ptr` with a custom deleter for the C handle (`pm_handle_t`).
- **Constructor:** `PowerMonitor()`
  - Initializes the library connection.
  - **Throws:** `std::runtime_error` if `pm_init` fails. The exception's `what()` message contains the error description from `pm_error_string`.
- **Destructor:** `~PowerMonitor()`
  - Automatically calls `pm_cleanup` on the managed C handle.
- **Methods:**
  - `void setSamplingFrequency(int frequency_hz)`
    - Sets the background sampling frequency (Hz).
    - **Throws:** `std::runtime_error` on C API failure.
  - `int getSamplingFrequency() const`
    - Gets the current sampling frequency (Hz).
    - **Throws:** `std::runtime_error` on C API failure.
  - `void startSampling()`
    - Starts the background sampling thread.
    - **Throws:** `std::runtime_error` on C API failure (e.g., already running).
  - `void stopSampling()`
    - Stops the background sampling thread.
    - **Throws:** `std::runtime_error` on C API failure (e.g., not running).
  - `bool isSampling() const`
    - Checks if sampling is currently active.
    - **Throws:** `std::runtime_error` on C API failure.
  - `PowerData getLatestData() const`
    - Gets the most recent instantaneous sensor readings.
    - **Returns:** A `PowerData` object containing the snapshot.
    - **Throws:** `std::runtime_error` on C API failure.
    - **Note:** See `PowerData` description and Safety Notes regarding pointer validity.
  - `PowerStats getStatistics() const`
    - Gets the statistics accumulated since the last reset.
    - **Returns:** A `PowerStats` object containing the statistics.
    - **Throws:** `std::runtime_error` on C API failure.
    - **Note:** See `PowerStats` description and Safety Notes regarding pointer validity.
  - `void resetStatistics()`
    - Resets all internal accumulated statistics.
    - **Throws:** `std::runtime_error` on C API failure.
  - `int getSensorCount() const`
    - Gets the number of detected sensors.
    - **Throws:** `std::runtime_error` on C API failure.
  - `std::vector<std::string> getSensorNames() const`
    - Gets the names of all detected sensors. Handles C memory management and string conversion.
    - **Returns:** A `std::vector<std::string>` containing the sensor names.
    - **Throws:** `std::runtime_error` on C API failure.

**Data Wrapper Classes:**

- `PowerData` / `PowerStats`
  - **Description:** Thin wrappers around the C structs `pm_power_data_t` and `pm_power_stats_t`, primarily returned by `getLatestData` and `getStatistics`. They are non-copyable but movable.
  - **Memory:** They hold a *copy* of the `total` C struct member and the *raw C pointer* (`sensors`) along with the `sensor_count`. **They do NOT manage the memory pointed to by the `sensors` pointer.** That memory is owned by the underlying C library.
  - **Getters:**
    - `const pm_sensor_data_t& getTotal() const` (for `PowerData`)
    - `const pm_sensor_stats_t& getTotal() const` (for `PowerStats`)
      - Returns a const reference to the copied `total` data/statistics struct.
    - `const pm_sensor_data_t* getSensors() const` (for `PowerData`)
    - `const pm_sensor_stats_t* getSensors() const` (for `PowerStats`)
      - Returns the raw C pointer to the array of per-sensor data/statistics. **See Safety Notes.**
    - `int getSensorCount() const`: Returns the number of elements pointed to by `getSensors()`.

**Underlying C Structs:**

- The C++ wrapper provides access to data via the C structs (`pm_sensor_data_t`, `pm_stats_t`, `pm_sensor_stats_t`). Refer to the C API documentation for detailed field descriptions within these structs.

</details>

<br/>

<details>
<summary><strong>C++ Wrapper Safety & Pointer Notes</strong></summary>

- **RAII & Exceptions:** The `PowerMonitor` class significantly improves safety by automating resource cleanup (`pm_cleanup`) through its destructor (RAII) and by converting C error codes into C++ exceptions (`std::runtime_error`). Always use `try...catch` blocks when interacting with `PowerMonitor` methods.
- **Pointer Validity (`getSensors()`):** The `PowerData` and `PowerStats` objects returned by `getLatestData()` and `getStatistics()` contain raw C pointers to arrays (`sensors`). **Crucially, the C++ wrapper classes (`PowerData`, `PowerStats`) do NOT manage the lifetime of the memory these pointers point to.** This memory is managed by the C library.
  - **Assumption:** The memory pointed to by `getSensors()` is typically valid only **temporarily**, likely until the next non-const call to the `PowerMonitor` object or until the `PowerMonitor` object is destroyed.
  - **Guideline:** Access the data through the pointer returned by `getSensors()` **immediately** after the call to `getLatestData()` or `getStatistics()`, within the same scope. Do **not** store this raw pointer for later use, as it may become invalid (dangling pointer).
- **C String Handling:** Data structures contain C-style fixed-size character arrays (e.g., `name[64]`, `status[32]`). Use safe methods (like the `c_char_to_string` helper in the examples using `strnlen`) to convert these to `std::string` to avoid buffer over-reads, especially if null termination is not guaranteed within the fixed size.

</details>

## Building from Source

### Prerequisites

- CMake 3.10 or higher
- C++ compiler with C++17 support
- Python 3.8 or higher (for Python bindings)
- Rust toolchain (for Rust bindings)

### Build Steps

#### C Library and C++ Bindings

```bash
git clone https://github.com/nerdneilsfield/xilinx-power-monitor.git
cd xilinx-power-monitor
mkdir build && cd build
cmake ..
make
sudo make install
```

#### Python Bindings

```bash
python3 -m pip install setuptools pybind11
python3 -m pip install -e .

# or you need to build wheel
python3 -m pip install build
python3 -m build --wheel
# the result will be in dist/
```

#### Rust Bindings

```bash
# copy c headers and sources to rust vendor directory
make copy-rust

# build rust crate
cd bindings/rust
cargo build
```

## Contributing

We welcome contributions! Please see our [CONTRIBUTING.md](CONTRIBUTING.md) for detailed information about:

- Project architecture and implementation details
- Development setup and guidelines
- Code style and testing requirements
- Pull request process
- Common development tasks
- Release process

## License

This project is licensed under the `BSD 3-Clause License` License - see the [LICENSE](LICENSE) file for details.

## Acknowledgments

- NVIDIA Xilinx team for their excellent hardware
- All contributors who have helped with this project
- [xilinx_stats]https://github.com/rbonghi/xilinx_stats

## Star History

[![Star History Chart](https://api.star-history.com/svg?repos=nnerdneilsfield/xilinx-power-monitor&type=Date)](https://star-history.com/#nerdneilsfield/xilinx-power-monitor&Date)