This page describes how to use the Microsoft.CodeAnalysis.Metrics package to perform source code analysis of .NET assemblies from a console application. Visual Studio users can perform source code analysis by clicking the “Analyze” dropdown menu and selecting “Calculate Code Metrics”, but I sought to automate this process so I can generate custom code analysis reports from console applications as part of my CI pipeline.
The code analysis XML contains information about every assembly, namespace, type, and function in the whole code base! There is a lot of possible information to extract, but the code below is enough to get us started extracting basic metric information for every type in the code base.
usingSystem;
usingSystem.IO;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Xml.Linq;
usingSystem.Collections.Generic;
/// <summary>/// Display a particular metric for every type in an assembly./// </summary>void RankTypes(string xmlFilePath, string metricName = "CyclomaticComplexity", bool highToLow = true)
{
string xmlText = File.ReadAllText(xmlFilePath);
XDocument doc = XDocument.Parse(xmlText);
XElement assembly = doc.Descendants("Assembly").First();
var rankedTypes = GetMetricByType(assembly, metricName).OrderBy(x => x.Value).ToArray();
if (highToLow)
Array.Reverse(rankedTypes);
Console.WriteLine($"Types ranked by {metricName}:");
foreach (var type in rankedTypes)
Console.WriteLine($"{type.Value:N0}\t{type.Key}");
}
Dictionary<string, int> GetMetricByType(XElement assembly, string metricName)
{
Dictionary<string, int> metricByType = new();
foreach (XElement namespaceElement in assembly.Element("Namespaces")!.Elements("Namespace"))
{
foreach (XElement namedType in namespaceElement.Elements("Types").Elements("NamedType"))
{
XElement metric = namedType.Element("Metrics")!.Elements("Metric")
.Where(x => x.Attribute("Name")!.Value == metricName)
.Single();
string typeName = namedType.Attribute("Name")!.Value;
string namespaceName = namespaceElement.Attribute("Name")!.Value;
string fullTypeName = $"{namespaceName}.{typeName}";
metricByType[fullTypeName] = int.Parse(metric.Attribute("Value")!.Value!.ToString());
}
}
return metricByType;
}
Specific metrics of interest will vary, but here are some code examples demonstrating how to parse the code metrics file to display useful information. For these examples I run the code analysis command above to generate ScottPlot.Metrics.xml from the ScottPlot code base and use the code above to generate various reports.
Cyclomatic complexity is a measure of the number of different paths that can be taken through a computer program, and it is often used as an indicator for difficult-to-maintain code. Some CI systems even prevent the merging of pull requests if their cyclomatic complexity exceeds a predefined threshold! Although I don’t intend to gate pull requests by complexity at this time, I would like to gain insight into which classes are the most complex as a way to quantitatively target my code maintenance and efforts.
Similarly, ranking all my project’s types by how many lines of code they contain can give me insight into which types may benefit most from refactoring.
The Maintainability Index is a value between 0 (worst) and 100 (best) that represents the relative ease of maintaining the code. It’s calculated from a combination of Halstead complexity (size of the compiled code), Cyclomatic complexity (number of paths that can be taken through the code), and the total number of lines of code.
The maintainability index is calculated by Microsoft.CodeAnalysis.Metrics so we don’t have to. I don’t know how Microsoft arrived at their weights for this formula, but the overall idea is described here.
With a little more effort you can generate HTML reports that use tables and headings to highlight useful code metrics and draw attention to types that could benefit from refactoring to improve maintainability.
Microsoft’s official Microsoft.CodeAnalysis.Metrics NuGet package is a useful tool for analyzing assemblies, navigating through namespaces, types, properties, and methods, and evaluating their metrics. Since these analyses can be performed using console applications, they can be easily integrated into CI pipelines or used to create standalone code analysis applications. Future projects can build on the concepts described here to create graphical visualizations of code metrics in large projects.
NDepend is commercial software for performing code analysis on .NET code bases and has many advanced features that make it worth considering for organizations that wish to track code quality and who can afford the cost. The NDepend Sample Reports demonstrate useful ways to report code analysis metrics.
I often find it useful to gate frequency counters using a 1 pulse per second (1PPS) signal derived from a 10 MHz precision frequency reference. However, a divide-by-10-million circuit isn’t trivial to implement. Counter ICs exist which enable divide-by-100 by combining multiple divide-by-2 and divide-by-5 stages (e.g., MC74HC390A around $0.85 each), but dividing 10 MHz all the way down to 1Hz would require at least 4 of these chips and a lot of wiring.
You can clock a microcontroller at 10 MHz and use its timer and interrupt systems to generate 1PPS. For example, an ATTiny202 in an 8-pin SOIC package is available from Mouser (>50k stocked) for $0.51 each. Note that modern series AVRs require a special UDPI programmer.
ATTiny202 ($0.51)
ATTint826 ($0.95)
This page documents a divide-by-10-million circuit achieved with a single microcontroller to scale a 10MHz frequency reference down to 1PPS. I’m using an ATTiny826 because that is what I have on hand, but these concepts apply to any microcontroller with a 16-bit timer.
Some AVRs come in DIP packages but their pin numbers may be different than the same chip in a SMT package. To facilitate prototyping using designs and code that will work identically across a breadboard prototype and a PCB containing SMT chips, I prefer to build DIP breakout boards using whatever SMT package I intend to include in my final builds. In this case it’s ATTint826 in a SOIC-20 package, and I can easily use this in a breadboard by soldering them onto SOIC-to-DIP breakout boards.
I assembled the breakout board by hand using a regular soldering iron. When working with small packages it helps so much to coat the pins with a little tack flux to facilitate wetting and prevent solder bridges. I’m presently using Chip Quik NC191. Even if flux is advertized as “no-clean”, it’s good practice and makes the boards look much nicer to remove remaining flux with acetone and Q-tips or brushes.
FTDI breakout board for power: To test this design I’m using a FT232 breakout board just to provide easy access to GND and Vcc (5V from the USB rail).
10 MHz can oscillator: It’s not ovenized or GPS disciplined, but I’m using this as a stand-in for whatever high-precision 10 MHz frequency standard will eventually be used in this circuit. The important thing is just to know that it outputs 0-5V square waves at 10 MHz going into the EXTCLK pin of the microcontroller
Traditional AVR microcontrollers used fuse bits to set the clock source, but modern series chips can change the clock source from within code. However, modifying the clock source requires temporarily disabling the configuration change protection (CCP) system.
Disabling the CCP only lasts four clock cycles, so the immediate next statement must be assignment of the new value. I use the following function to facilitate this action.
/* Write a value to a CCP-protected register */voidccp_write(volatileregister8_t* address, uint8_t value){
CCP = CCP_IOREG_gc;
*address = value;
}
// Use internal 20 MHz clock with CKOUT pin enabled
ccp_write(&CLKCTRL.MCLKCTRLA, CLKCTRL.MCLKCTRLA | CLKCTRL_CLKOUT_bm);
Do not use compound statements when writing to the CCP register. The code below fails to change clock as one may expect by looking at the code, presumably because the combined OR operation with the assignment exceeds four clock cycles. Instead of direct assignment, use the ccp_write function described above.
// WARNING: This code does not actually change the clock source
CCP = CCP_IOREG_gc;
CLKCTRL.MCLKCTRLA = CLKCTRL.MCLKCTRLA | CLKCTRL_CLKOUT_bm;
This is how I configured my ATTiny826’s TCA0 16-bit timer to fire an interrupt every 200 ms.
Prescale: By enabling a divide-by-64 prescaler, my 10 MHz input becomes 156,250 Hz.
Top: By setting the top of my 16-bit counter at 31,250, I achieve exactly 5 overflows per second (once every 200 ms).
Interrupt: By enabling an overflow interrupt, I am able to call a function every 200 ms.
voidconfigure_1pps(){
// 10 MHz system clock with div64 prescaler is 156,250 Hz.
// Setting a 16-bit timer's top to 31,250 means 5 overflows per second.
TCA0.SINGLE.INTCTRL = TCA_SINGLE_OVF_bm; // overflow interrupt
TCA0.SINGLE.CTRLB = TCA_SINGLE_WGMODE_NORMAL_gc; // normal mode
TCA0.SINGLE.PER =31249UL; // control timer period by setting timer top
TCA0.SINGLE.CTRLA |= TCA_SINGLE_CLKSEL_DIV64_gc; // set clock source
TCA0.SINGLE.CTRLA |= TCA_SINGLE_ENABLE_bm; // start timer
}
Alternatively, multiple timers could be cascaded to achieve a similar effect. Modern AVR series microcontrollers have sections in their datasheet describing considerations for cascading two 16-bit timers to create a single 32-bit timer. Using this strategy one could set the top of the counter to 5 million and arrange an interrupt to toggle an LED, resulting in a 1Hz signal with 50% duty.
This method is called whenever the timer’s overflow interrupt is triggered. Since it’s called 5 times per second, I just need a global counter to count the number of times it was called, and set an output pin to high on every 5th invocation.
Do not forget to enable global interrupts in your start-up sequence! This is an easy mistake to make, and without calling this function the overflow function will never be invoked.
We have achieved a light that blinks exactly once per second with roughly the same precision as the 10 MHz frequency reference used to clock the microcontroller. This output signal is ready to use for precision measurement purposes, such as toggling the gate of a discrete frequency counter.
Inspecting the header file iotn826.h in my Program Files / Atmel folder was very useful for identifying named bit masks stored as enums. There is a similarly named file for every supported AVR microcontroller.
This page describes how to program Microchip’s newest series of AVR microcontrollers using official programming gear and software. I spent many years programming the traditional series of Atmel chips, but now several years after Microchip acquired Atmel I am interested in exploring the capabilities of the latest series of AVR microcontrollers (especially the new AVR DD family). Currently the global chip shortage makes it difficult to source traditional ATMega and STM32 chips, but the newest series of AVR microcontrollers feature an impressive set of peripherals for the price and are available from all the major vendors.
pressive set of peripherals for the price and are available from all the major vendors.
Older AVR microcontrollers are programmed using in-circuit serial programming (ICSP) through the RESET, SCK, MISO, and MOSI pins using cheap programmers like USBtiny. However, serial programming is not supported on newer AVR microcontrollers.
New AVR microcontrollers are programmed using the unified program and debug interface (UDPI) exclusively through the UDPI pin. UDPI is a Microchip proprietary interface requiring a UDPI-capable programmer.
Official UDPI programmers include Atmel-ICE ($129) and MPLAB Snap ($35). The Atmel-ICE is expensive but it is very well supported. The MPLAB Snap is hacky, requires re-flashing, and has a physical design flaw requiring a hardware modification before it can program AVR series chips.
There are notable attempts to create alternative programmers (e.g., jtag2updi and pymcuprog), but this journey into the land of unofficial programmer designs is fraught with abandoned GitHub repositories and a lot of added complexity and brittleness (e.g., SpenceKonde/AVR-Guidance), so to save yourself frustration in the future I highly recommend just buying an officially supported programmer. It’s also nice when you can program and debug your microcontroller from within your IDE.
UDPI programmers have a Vcc pin that is used to sense supply voltage (but not provide it), so you must power your board yourself while using one of these new programmers.
Blinking a LED is the “Hello, World” of microcontroller programming. Let’s take a look at the code necessary to blink a LED on pin 2 of an ATTiny286. It is compiled and programmed onto the chip using Microchip Studio.
PORTA.DIR sets the direction of pins on port A (0xFF means all outputs)
PORTA.OUT sets output voltage on pins of port A (0xFF means all high)
Using _delay_ms() requires including delay.h
Including delay.h requires defining F_CPU (the CPU frequency)
The ATTiny286 datasheet section 11.3.3 indicates the default clock is 20 MHz with a /6 prescaler, so the default clock is 3333333 Hz (3.3 MHz). This behavior can be customized using the Oscillator Configuration Fuse (FUSE.OSCCFG).
Many of the newer AVR series microcontrollers are not available in breadboard-friendly DIP packages. I find SOIC-to-DIP breakout boards (available on Amazon and eBay) to be useful for experimenting with chips in SOIC packages. Here I added extra power and PA4 (pin 2) LEDs and 10 kΩ current limiting resistors.
I power the device from the 3.3V or 5V pins on a FT232 USB breakout board. Although the topic is out of scope for this article, I find it convenient to use FTDI chips to exchange small amounts of data or debug messages between a microcontroller and a modern PC over USB without requiring special drivers.
I’m surprised how little information there is online about how to reliably program modern AVR series microcontrollers. In late 2022 there is a surprisingly large amount of “advice” on this topic which leads to dead ends and broken or abandoned projects. After looking into it for a while, here is my opinionated assessment. Mouser and Digikey have links to expensive programmers, and Amazon has links to similar items but reviews are littered with comments like “arrived bricked” and “can not program AVR series chips”. DIY options typically involve abandoned (or soon-to-be abandoned?) GitHub repositories, or instructions for Arduino-related programming. I seek to consolidate and distill the most useful information onto this page, and I hope others will find it useful.
After using $5 ICSP programmers for the last decade I almost fell out of my chair when I saw Microchip’s recommended entry-level programmer is over $180! Digikey sells a “basic” version without cables for $130, but that still seems crazy to me. Also, $50 for a ribbon cable?
I found a kit on Amazon that sells the programmer with a cable for $126. It was hard for me to press that buy button, but I figured the time I would save by having access to modern and exotic chips during the present global chip shortage would make it worth it. After a couple days of free Prime shipping, it arrived. It was smaller than I thought it would be from the product photos.
The cable that came with the device seemed a bit hacky at first, but I’m happy to have it. The female 2.54 mm socket is easy to insert breadboard jumpers into.
I’m glad this thing is idiot proof. The very first thing I did after unboxing this programmer was hook it up to my power supply rails using reverse polarity. I misread the pin diagram and confused the socket with the connector (they are mirror images of one another). This is an easy mistake to make though, so here’s a picture of the correct orientation. Note the location of the tab on the side of the connector.
Atmel ICE Pinout
Programming Connection
Black: GND
Red: Vcc - This line is used to sense power and not deliver it, so you are responsible for externally powering your board.
Blue: UPDI pin - Although a pull-up resistor on the UPDI pin is recommended, I did not find it was required to program my chip on the breadboard in this configuration.
You can break-out power and programming pins so you can power your device with this programmer and also not need the stupid ribbon cable for programming it! See my article Hack an Atmel ICE to Deliver Power for more information.
The Atmel ICE was easy to use with Microchip Studio. My programmer was detected immediately, a window popped-up and walked me through updating the firmware, and my LED was blinking in no time.
Did I really need to spend $126 for an AVR programmer? Amazon carries the MPLAB Snap for $34, but lots of reviews say it doesn’t work. After easily getting the Atmel-ICE programmer up and running I thought it would be a similarly easy experience setting-up the MPLAB Snap for AVR UPDI programming, but boy was I wrong. Now that I know the steps to get this thing working it’s not so bad, but the information here was only gathered after hours of frustration.
Here are the steps you can take to program modern AVR microcontrollers with UPDI using a MPLAB Snap:
The MPLAB Snap ships with obsolete firmware and must be re-flashed immediately upon receipt.
Microchip Studio’s firmware upgrade tool does not actually work with the MPLAB Snap. It shows the board with version 0.00 software and it hangs (with several USB disconnect/reconnect sounds) if you try to upgrade it.
You can only re-flash the MPLAB Snap using the MPLAB X IDE. Download the 1.10 GB MPLAB setup executable and install the MPLAB IDE software which occupies a cool 9.83 GB.
In the MPLAB IDE select Tools and select Hardware Tool Emergency Boot Firmware Recovery. At least this tool is helpful. It walks you through how to reset the device and program the latest firmware.
Defective may be a strong word, but let’s just say the hardware was not designed to enable programming AVR chips using UPDI. Microchip Studio will detect the programmer but if you try to program an AVR you’ll get a pop-up error message that provides surprisingly little useful information.
Atmel Studio was unable to start your debug session. Please verify device selection, interface settings, target power and connections to the target device. Look in the details section for more information.
StatusCode: 131107
ModuleName: TCF (TCF command: Processes:launch failed.)
An illegal configuration parameter was used. Debugger command Activate physical failed.
The reason MPLAB Snap cannot program AVR microcontrollers is because the UPDI pin should be pulled high, but the MPLAB Snap comes from the factory with its UPDI pin pulled low with a 4.7 kΩ resistor to ground. You can try to overpower this resistor by adding a low value pull-up resistor to Vcc (1 kΩ worked for me on a breadboard), but the actual fix is to fire-up the soldering iron and remove that tiny surface-mount pull-down resistor labeled R48.
Have your glasses? R48 is here:
These photos were taken after I removed the resistor. I didn’t use hot air. I just touched it a for a few seconds with a soldering iron and wiped it off then threw it away.
You don’t need a microscope, but I’m glad I had one.
You can now program AVR microcontrollers using UPDI with your MPLAB Snap! Blink, LED, blink.
Can you believe this is the officially recommended action? According to the official Microchip Engineering Technical Note ETN #36: MPLAB Snap AVR Interface Modification
Symptom: Programming and debugging fails with AVR microcontroller devices that use the UPDI/PDI/TPI interfaces. MPLAB SNAP, Assembly #02-10381-R1 requires an external pull-up resistor for AVR microcontroller
devices that use these interfaces.
Problem: AVR microcontroller devices that use the UPDI/PDI/TPI interfaces require the idle state of inactivity to be at a logic high level. Internally, the AVR devices have a weak (50-100K) pull-up resistor that attempts to keep the line high. An external and stronger pull-up resistor may be enough to mitigate this issue and bring voltages to acceptable VDD levels. In some cases, this may not be enough and the pull-down resistor that is part of the ICSP protocol can be removed for these AVR microcontroller applications.
Solution: If most of the applications are AVR-centric, consider removing the R48 resistor as shown below. This completely isolates any loading on the programming data line. Additionally, a pull-up resistor to VDD in the range of 1K to 10K should be used for robustness. Pin 4 of J4 is the TPGD data line used for ICSP interfaces and it also doubles as the DAT signal for UPDI/PDI and TPI interfaces. The pull-up resistor can be mounted directly from TVDD (J4-2) to TPGD/DAT (J4-4). Alternatively, the resistor can be mounted on the application side of the circuit
for convenience.
I feel genuinely sorry for the Amazon sellers who are getting poor reviews because they sell this product. It really isn’t their fault. I hope Google directs people here so that they can get their boards working and leave positive reviews that point more people to this issue.
There is no official support for UPDI programming using a serial adapter, but it seems some people have figured out how to do it in some capacity. There was a promising pyupdi project, but it is now deprecated. At the time of writing the leading project aiming to enable UPDI programming without official hardware is pymcuprog, but its repository has a single commit dated two months ago and no activity since. Interestingly, that commit was made by buildmaster@microchip.com (an unverified email address), so it may not be fair to refer to it as an “unofficial” solution. The long term support of the pymcuprog project remains uncertain, but regardless let’s take a closer look at how it works.
To build a programmer you just need a TTL USB serial adapter and a 1kΩ resistor. These are the steps I used to program a LED blink program using this strategy:
Use a generic FT232 breakout board to achieve a USB-controlled serial port on my breadboard.
Connect the programmer as shown with the RX pin directly to the UPDI pin of the microcontroller and the resistor between the RX and TX pins.
I used Microchip Studio to compile my C code and generate the hex file. Now I can use pymcuprog to load those hex files onto the chip. It’s slower to program and inconvenient to drop to a terminal whenever I want to program a chip, but it works.
The new AVR series microcontrollers have lots of cool peripherals for the price and are available during a chip shortage that threatens availability of the more common traditional microcontrollers.
The Atmel-ICE is expensive, but the most convenient and reliable way to program modern AVR microcontrollers using UPDI. You can break out power and programming pins so it can power your device and program the chip without requiring the silly ribbon cable.
The MPLAB Snap can program modern AVRs using UPDI after a software flash and a hardware modification, but its support for AVRs seems like an afterthought rather than its design priority.
You can create a makeshift unofficial UPDI programmer from a USB serial adapter, but the added complexity, lack of debugging capabilities, increased friction during the development loop, and large number of abandoned projects in this space make this an unappealing long term solution in my opinion.
jtag2updi aims to convert an Arduino into a serial port to use as a UPDI programmer. It looks a lot more complicated and expensive than just using a cheap USB serial adapter, and it requires DisablingAutoResetOnSerialConnection.
This page demonstrates how to read differential voltage from a HX710 ADC using Arduino. I recently obtained some pressure sensor boards from Amazon for less than $3 each under names like 6pcs 3.3-5V Digital Barometric Air Pressure Sensor Module Liquid Water Level Controller Board 0-40KPa that use this ADC. Several years ago I worked on a precision pressure meter project based on an I2C temperature and pressure sensor (MS5611), and now that I see new inexpensive SPI pressure sensor modules on the consumer market I’m interested to learn more about their capabilities.
The ADC chip is easily identified as a HX710B24-Bit Analog-to-Digital Converter (ADC) with Built-in Temperature Sensor. According to the datasheet it can be powered by a 3.3V or 5V supply, and the value it reports is the differential voltage between two input pins.
The datasheet indicates this device can be run from a 3.3V or 5V supply, it uses a built-in fixed-gain (128x) differential amplifier, and it can read up to 40 samples per second. The datasheet provides an example circuit demonstrating how this ADC can be used to measure weight from a scale sensor:
To get a better idea of how this sensor works it would be helpful to locate its product number. I had a hunch it was beneath the part so I desoldered it, and indeed I found part identification information.
The pressure sensor is labeled as a PSG010S but unfortunately I struggled to find a quality datasheet for it. I did find some now-deleted images from an AliExpress listing showing the differences between the base model and the R and S variants.
I found this PSG010R datasheet (curiously written in Comic Sans) indicating that maximum voltage is 5V and that the gauge pressure is 0 - 40KPa (0 - 5.8 PSI). This seems to be a fairly standard differential pressure sensor design using a pair of voltage dividers where the pressure is a function of the difference in voltage at the two mid-points (a Wheatstone bridge).
I used a multimeter to determine the resistor network inside this pressure sensor. Interestingly I found that pin 4 was not connected, which is opposite of many other designs I find online (where pin 5 is not connected). It is critically important to get this right if your goal is to design a PCB around this component. I guess connecting pins 1 & 6 and also 3 & 4 could be an option for covering all your bases? I also question if people are getting confused and reporting the mirror image pinout because they mistake the vent hole for the registration marker. Either way, I made the following diagram which I found helpful:
Update (2022-12-23): I received an email from somebody offering additional information about this component:
The PSG010 reports positive and negative pressures and can easily have its range shifted to almost double in one direction with almost none in the other. All that is needed is to lift the VCC or GND pin and insert a surface mount 75R ±15R under it.
Lifting the ground side by 75R makes it double positive, while pushing the applied +V down makes it double negative (vacuum).
– bruceg
This code demonstrates how to measure HX710B values using Arduino and display the readings in the serial terminal sufficient to graph in real time using the serial plotter. The animated plot is what it looks like when I blow puffs of air on the sensor.
voidsetup() {
pinMode(2, INPUT); // Connect HX710 OUT to Arduino pin 2
pinMode(3, OUTPUT); // Connect HX710 SCK to Arduino pin 3
Serial.begin(9600);
}
voidloop() {
// wait for the current reading to finish
while (digitalRead(2)) {}
// read 24 bits
long result =0;
for (int i =0; i <24; i++) {
digitalWrite(3, HIGH);
digitalWrite(3, LOW);
result = result <<1;
if (digitalRead(2)) {
result++;
}
}
// get the 2s compliment
result = result ^0x800000;
// pulse the clock line 3 times to start the next pressure reading
for (char i =0; i <3; i++) {
digitalWrite(3, HIGH);
digitalWrite(3, LOW);
}
// display pressure
Serial.println(result);
}
Although some libraries are available which facilitate interacting with the HX710, here I engage with it discretely to convey each step of the conversion and measurement process. I found that many libraries use the 10 Hz mode by default, whereas I certainly prefer the 40 Hz mode. More frustratingly, code in many libraries refer to this as gain, which is incorrect. The datasheet indicates gain is fixed at 128 and cannot be changed in software.
Update (2022-12-23): I received an email explaining why people often use “gain” and “mode” when referring to the HX710:
The HX711 is similar to the HX710 but it has user selectable gain AND user selectable sample rates BUT only certain combinations are allowed, so setting mode WILL also select its matched gain value.
The HX710 uses most of the same internals, but with just 3 modes - reading the Wheatstone Bridge always using 128 gain at 10 or 40Hz while swapping to Avolt (HX710A) or internal Temperature (HX710B) uses a lower gain and less digits. So for people familiar with the HX711 there is no ambiguity in mixing mode and gain.
– bruceg
Pressure Sensor Guide by T.K. HAREENDRAN - A similar write-up that goes into additional detail. They didn’t de-solder the pressure sensor to identify the component name, but there’s lots of good information on this page.
bogde/HX711 on GitHub - An Arduino library to interface the Avia Semiconductor HX711 24-Bit Analog-to-Digital Converter (ADC) for reading load cells / weight scales. Code on this page does not use this library, but others may find it helpful.
This project how to represent bitmap data in a plain old C object (POCO) to create images from scratch using C# and no dependencies.
Common graphics libraries like SkiaSharp, ImageSharp, System.Drawing, and Maui.Graphics can read and write bitmaps in memory, so a POCO that stores image data and converts it to a bitmap byte allows creation of platform-agnostic APIs that can be interfaced from any graphics library.
This page demonstrates how to use C# (.NET 6.0) to create bitmap images from scratch. Bitmap images can then be saved to disk and viewed with any image editing program, or they can consumed as a byte array in memory by a graphics library. There are various bitmap image formats (grayscale, indexed colors, 16-bit, 32-bit, transparent, etc.) but code here demonstrates the simplest common case (8-bit RGB color).
The following struct represents RGB color as 3 byte values and has helper methods for creating new colors.
publicstructRawColor{
publicreadonlybyte R, G, B;
public RawColor(byte r, byte g, byte b)
{
(R, G, B) = (r, g, b);
}
publicstatic RawColor Random(Random rand)
{
byte r = (byte)rand.Next(256);
byte g = (byte)rand.Next(256);
byte b = (byte)rand.Next(256);
returnnew RawColor(r, g, b);
}
publicstatic RawColor Gray(bytevalue)
{
returnnew RawColor(value, value, value);
}
}
A color class like this could be extended to support additional niceties. Refer to SkiaSharp’s SKColor.cs, System.Drawing’s Color.cs, and Maui.Graphics’ Color.cs for examples and implementation details. I commonly find the following features useful include when writing a color class:
A static class with named colors e.g., RawColors.Blue
RawBitmap bmp = new(400, 300);
Random rand = new();
for (int x = 0; x < bmp.Width; x++)
for (int y = 0; y < bmp.Height; y++)
bmp.SetPixel(x, y, RawColor.Random(rand));
bmp.Save("random-rgb.bmp");
RawBitmap bmp = new(400, 300);
Random rand = new();
for (int x = 0; x < bmp.Width; x++)
{
for (int y = 0; y < bmp.Height; y++)
{
byte r = (byte)(255.0 * x / bmp.Width);
byte g = (byte)(255.0 * y / bmp.Height);
byte b = (byte)(255 - 255.0 * x / bmp.Width);
RawColor color = new(r, g, b);
bmp.SetPixel(x, y, color);
}
}
bmp.Save("rainbow.bmp");
RawBitmap bmp = new(400, 300);
Random rand = new();
for (int i = 0; i < 1000; i++)
{
int rectX = rand.Next(bmp.Width);
int rectY = rand.Next(bmp.Height);
int rectWidth = rand.Next(50);
int rectHeight = rand.Next(50);
RawColor color = RawColor.Random(rand);
for (int x = rectX; x < rectX + rectWidth; x++)
{
for (int y = rectY; y < rectY + rectHeight; y++)
{
if (x < 0 || x >= bmp.Width) continue;
if (y < 0 || y >= bmp.Height) continue;
bmp.SetPixel(x, y, color);
}
}
}
bmp.Save("rectangles.bmp");
publicvoid FillRect(Rectangle rect, Color color)
{
for (int y = rect.YMin; y < rect.YMax; y++)
{
for (int x = rect.XMin; x < rect.XMax; x++)
{
SetPixel(x, y, color);
}
}
}
The following code demonstrates how to load the bitmap byte arrays generated above into common graphics libraries and save the result as a JPEG file. Although the bitmap byte array can be written directly to disk as a .bmp file, these third-party libraries are required to encode images in additional formats like JPEG.