<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>Levitating Pineapple</title>
    <link rel="self" type="application/atom+xml" href="https://levitatingpineapple.github.io/blog/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://levitatingpineapple.github.io/blog/"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2024-01-05T09:41:00+00:00</updated>
    <id>https://levitatingpineapple.github.io/blog/atom.xml</id>
    <entry xml:lang="en">
        <title>Van IOT</title>
        <published>2024-01-05T09:41:00+00:00</published>
        <updated>2024-01-05T09:41:00+00:00</updated>
        
        <author>
          <name>
            
              Arturs
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://levitatingpineapple.github.io/blog/projects/van/"/>
        <id>https://levitatingpineapple.github.io/blog/projects/van/</id>
        
        <content type="html" xml:base="https://levitatingpineapple.github.io/blog/projects/van/">&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;levitatingpineapple.github.io&#x2F;blog&#x2F;projects&#x2F;van&#x2F;iPad.webp&quot; alt=&quot;iPad&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;So I made an app for a camper-van... It has seen daily use for close to a year and honestly I&#x27;m quite impressed. It&#x27;s based on &lt;a href=&quot;http:&#x2F;&#x2F;docs.oasis-open.org&#x2F;mqtt&#x2F;mqtt&#x2F;v3.1.1&#x2F;os&#x2F;mqtt-v3.1.1-os.html&quot;&gt;MQTT&lt;&#x2F;a&gt; protocol, which provides good ability to handle power and network outages and includes state persistence and restoration. The stack is quite lean for the amount of features and has been able to grow well while maintaining reasonable complexity.&lt;br &#x2F;&gt;
After some experimentation with existing solutions like &lt;a href=&quot;https:&#x2F;&#x2F;www.openhab.org&quot;&gt;OpenHAB&lt;&#x2F;a&gt;, &lt;a href=&quot;https:&#x2F;&#x2F;www.home-assistant.io&quot;&gt;Home Assistant&lt;&#x2F;a&gt;, &lt;a href=&quot;https:&#x2F;&#x2F;esphome.io&quot;&gt;ESPHome&lt;&#x2F;a&gt; and even Apple Home via &lt;a href=&quot;https:&#x2F;&#x2F;homebridge.io&quot;&gt;Homebridge&lt;&#x2F;a&gt; 
I came to the irresponsible decision to try and do it myself. 
While these open source projects are undeniably easier to get started with, I was finding that the added layer of abstraction and use of &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Domain-specific_language&quot;&gt;DSL&lt;&#x2F;a&gt;s made debugging rather tricky, beyond certain scale.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;requirements&quot;&gt;Requirements&lt;&#x2F;h2&gt;
&lt;p&gt;Features:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Monitoring&lt;&#x2F;strong&gt; - being able to inspect state of the onboard systems, such as solar, temperature, air quality, internet connectivity as well as location and camera feeds.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Control&lt;&#x2F;strong&gt; - ventilation, lights, car alarm with the ability to remotely disable the vehicle.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Automation&lt;&#x2F;strong&gt; - ability to easily add and tweak automations. &lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Technical:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Reasonably scalable&lt;&#x2F;li&gt;
&lt;li&gt;Stable and robust against power or network outages&lt;&#x2F;li&gt;
&lt;li&gt;Low power&#x2F;compute requirements (Solar powered)&lt;&#x2F;li&gt;
&lt;li&gt;Runs locally with remote access trough VPN&lt;&#x2F;li&gt;
&lt;li&gt;Uses common, easy to access components in case of hardware failure&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;architecture&quot;&gt;Architecture&lt;&#x2F;h2&gt;
&lt;p&gt;The core of the system is a &lt;a href=&quot;https:&#x2F;&#x2F;www.raspberrypi.com&#x2F;products&#x2F;raspberry-pi-3-model-b-plus&#x2F;&quot;&gt;single board computer&lt;&#x2F;a&gt; running an open source broker &lt;a href=&quot;https:&#x2F;&#x2F;mosquitto.org&quot;&gt;Mosquitto&lt;&#x2F;a&gt;. The SBC provides a dedicated WiFi access point with static IP allocation, to allow &lt;a href=&quot;https:&#x2F;&#x2F;docs.espressif.com&#x2F;projects&#x2F;esp-idf&#x2F;en&#x2F;latest&#x2F;esp32&#x2F;api-reference&#x2F;system&#x2F;ota.html&quot;&gt;over the air&lt;&#x2F;a&gt; updates. To provide some separation of concerns, the main functions are split between few dedicated clients.
The Internet connectivity if enabled by Mikrotik&#x27;s &lt;a href=&quot;https:&#x2F;&#x2F;mikrotik.com&#x2F;product&#x2F;ltap_mini&quot;&gt;LtAP mini&lt;&#x2F;a&gt;. With the recent addition of &lt;a href=&quot;https:&#x2F;&#x2F;help.mikrotik.com&#x2F;docs&#x2F;display&#x2F;ROS&#x2F;MQTT&quot;&gt;MQTT support&lt;&#x2F;a&gt; in RouterOS, I was also able to integrate GPS, and LTE by writing few simple &lt;a href=&quot;https:&#x2F;&#x2F;wiki.mikrotik.com&#x2F;wiki&#x2F;Manual:Scripting&quot;&gt;scripts&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;levitatingpineapple.github.io&#x2F;blog&#x2F;projects&#x2F;van&#x2F;architecture.svg&quot; alt=&quot;Architecture&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;iOS App&lt;&#x2F;strong&gt; - The only frontend. Some functionality exposed to Siri Shortcuts.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;ESP32 Clients&lt;&#x2F;strong&gt; - Controls and provides readings from external devices using plethora of available interfaces: UART, I2C, SPI, PWM.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Mosquitto Broker&lt;&#x2F;strong&gt; - Source of truth for the system. Retains received state, and persists it on disk. It also periodically pings connected clients to verify connection integrity.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Database Client&lt;&#x2F;strong&gt;
&lt;ul&gt;
&lt;li&gt;Logs sensor readings into a database, to provide ability to view history&lt;&#x2F;li&gt;
&lt;li&gt;Provides chart data for graphing a time range. &lt;&#x2F;li&gt;
&lt;li&gt;The data points get down sampled and averaged to keep the UI responsive.&lt;&#x2F;li&gt;
&lt;li&gt;Cleans up old data&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;SQLite&lt;&#x2F;strong&gt; - After comparing the performance of some dedicated time series databases like &lt;a href=&quot;https:&#x2F;&#x2F;www.influxdata.com&quot;&gt;InfluxDB&lt;&#x2F;a&gt;, I was pleasantly surprised by how much this self-contained engine can handle!&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Automations Client&lt;&#x2F;strong&gt; - Provides cross-client automations, or automations that require external information such as changing fan speed, depending on temperature or dimming lights after sunset.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Notifications Client&lt;&#x2F;strong&gt; - One of iOS limitations is inability to reliably run any kind of background processes. This means having to rely on Apple&#x27;s &lt;a href=&quot;https:&#x2F;&#x2F;developer.apple.com&#x2F;documentation&#x2F;usernotifications&#x2F;setting_up_a_remote_notification_server&quot;&gt;push notification service&lt;&#x2F;a&gt;. While setting one up just for this seems like an overkill, luckily there are services &lt;a href=&quot;https:&#x2F;&#x2F;pushover.net&quot;&gt;Pushover&lt;&#x2F;a&gt; that provide notifications as a service with a simple API. Not elegant, but it works! The notifications client listens to the broker&#x27;s error topic where clients can report implausible sensor readings, connection status using &lt;a href=&quot;http:&#x2F;&#x2F;docs.oasis-open.org&#x2F;mqtt&#x2F;mqtt&#x2F;v3.1.1&#x2F;os&#x2F;mqtt-v3.1.1-os.html#_Toc398718031&quot;&gt;will topic&lt;&#x2F;a&gt; and other errors.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;source-of-truth&quot;&gt;Source of Truth&lt;&#x2F;h3&gt;
&lt;p&gt;Since a lot is happening asynchronously, it&#x27;s important to have a &lt;strong&gt;single source of truth&lt;&#x2F;strong&gt;. What it means in practice is that clients stateless, and does not retain readings for later publishing. They simply receive commands, update external devices, and report the state to the broker.
Clients also fetch the last retained state from broker on startup.&lt;br &#x2F;&gt;
This, combined with last will feature ensures that broker has up-to-date view of the system.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;data-flow&quot;&gt;Data Flow&lt;&#x2F;h3&gt;
&lt;p&gt;Here&#x27;s a simple toy example to illustrate data flow.&lt;br &#x2F;&gt;
Rounded boxes represent clients while squared ones contain topics:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;levitatingpineapple.github.io&#x2F;blog&#x2F;projects&#x2F;van&#x2F;naive.svg&quot; alt=&quot;Naive Approach&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;❌ &lt;strong&gt;Naive Approach&lt;&#x2F;strong&gt; - Here the dimmer has two-way binding to the &lt;code&gt;room&#x2F;light&lt;&#x2F;code&gt; topic and the Light client is simply consuming this state. While this setup mostly works, there are many cases where the state of the light could get out of sync. It could be dropped message or multiple instances of dimmer controlling the same light.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;levitatingpineapple.github.io&#x2F;blog&#x2F;projects&#x2F;van&#x2F;unidirectional.svg&quot; alt=&quot;Unidirectional&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;✅ &lt;strong&gt;Unidirectional Data Flow&lt;&#x2F;strong&gt; - MQTT Topics work on &lt;strong&gt;publish&#x2F;subscribe&lt;&#x2F;strong&gt; principle and this makes them a &lt;em&gt;two way street&lt;&#x2F;em&gt;. To achieve unidirectional data flow my approach was separating the command topics (not retained) from those containing state (retained). Any client might only read or write to a certain topic. Now the state of the light originates at it&#x27;s physical location - the light client. This also prevents potential feedback loops.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;clients&quot;&gt;Clients&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;levitatingpineapple.github.io&#x2F;blog&#x2F;projects&#x2F;van&#x2F;power.webp&quot; alt=&quot;iPad&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Most clients were implemented using ESP development boards. The one above is responsible for managing the battery. It&#x27;s built on a standard 100x100mm prototype PCB using common brake-out boards. The client has a serial connection to the solar charge controller using Victron&#x27;s &lt;a href=&quot;https:&#x2F;&#x2F;www.victronenergy.com&#x2F;upload&#x2F;documents&#x2F;VE.Direct-Protocol-3.33.pdf&quot;&gt;VE.Direct&lt;&#x2F;a&gt; for reporting solar readings. It also measures battery current using a shunt. The state of charge is retained in the broker, for preservation between reboots.&lt;br &#x2F;&gt;
Since I&#x27;m using LiFePO4 batteries that would get damaged, if charged below freezing - the client also operates a heating pad using PWM based on temperature readings.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h2&gt;
&lt;p&gt;While I could go into way more detail, hopefully this article provides a decent high level overview of one of many ways to approach building a system like this.
Overall the process has been a remarkable learning experience and my go-to justification for trying new things. This project involved broad range or technologies and tools such as 3d printing, PBC design&#x2F;assembly, Rust, C++, Time Series Databases, SwiftUI, etc.&lt;br &#x2F;&gt;
While it certainly has been a significant time investment, if feel that system like this is generic enough with sufficiently wide range of applications to justify time spent. (Or at least that&#x27;s what I tell myself)&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;levitatingpineapple.github.io&#x2F;blog&#x2F;projects&#x2F;van&#x2F;comic.webp&quot; alt=&quot;Comic&quot; &#x2F;&gt;
&lt;a href=&quot;https:&#x2F;&#x2F;imgs.xkcd.com&#x2F;comics&#x2F;the_general_problem.png&quot;&gt;XKCD Comic&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Color Naming</title>
        <published>2024-01-05T09:41:00+02:00</published>
        <updated>2024-01-05T09:41:00+02:00</updated>
        
        <author>
          <name>
            
              Arturs
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://levitatingpineapple.github.io/blog/design/color-naming/"/>
        <id>https://levitatingpineapple.github.io/blog/design/color-naming/</id>
        
        <content type="html" xml:base="https://levitatingpineapple.github.io/blog/design/color-naming/">&lt;p&gt;While theming might appear simple at first glance a fair bit of complexity can appear once full context is taken into account. Some common challenges include, but are not limited to:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Theme&#x27;s target changing over time i.e. handling deprecation&lt;&#x2F;li&gt;
&lt;li&gt;Inability to assume theme authors having full context of the software&lt;&#x2F;li&gt;
&lt;li&gt;Accessibility requirements like &lt;a href=&quot;https:&#x2F;&#x2F;www.w3.org&#x2F;TR&#x2F;WCAG21&#x2F;#dfn-contrast-ratio&quot;&gt;contrast&lt;&#x2F;a&gt; or color blindness&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;As the project grows in scale (and age) I&#x27;ve seen theming going through often predictable stages with &lt;strong&gt;Stage 0&lt;&#x2F;strong&gt; being copy-pasting &lt;code&gt;#HEX&lt;&#x2F;code&gt; codes from the design into the codebase.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;stage-one&quot;&gt;Stage One&lt;&#x2F;h2&gt;
&lt;p&gt;Referencing by &lt;strong&gt;WHAT&lt;&#x2F;strong&gt; color is used&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;levitatingpineapple.github.io&#x2F;blog&#x2F;design&#x2F;color-naming&#x2F;pantone.webp&quot; alt=&quot;Pantone&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;A common practice to ensure consistency is limiting colors to a finite set called &lt;strong&gt;palette&lt;&#x2F;strong&gt;.
The palette colors often come with a &lt;em&gt;marketing name&lt;&#x2F;em&gt; like &lt;a href=&quot;https:&#x2F;&#x2F;margaret2.github.io&#x2F;pantone-colors&quot;&gt;Pantone&#x27;s&lt;&#x2F;a&gt; &lt;strong&gt;Seashell Pink&lt;&#x2F;strong&gt; or &lt;strong&gt;Ruby Wine&lt;&#x2F;strong&gt;.
While these names do a great job at conveying the artisanal color picking and evoke abstract associations, they often fall short at being used as references for few reasons:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Having reference that leaks information about the color itself restricts what it can reasonably point to&lt;&#x2F;li&gt;
&lt;li&gt;Its easy to &lt;em&gt;&amp;quot;theme yourself into a corner&amp;quot;&lt;&#x2F;em&gt; where you will end up doing a lot of &lt;code&gt;find &amp;amp; replace&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;These names carry &lt;strong&gt;no information about how they are used&lt;&#x2F;strong&gt; in the project making it trivial to use them in an inconsistent way&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Great example of direct palette usage is present in terminal emulators, which allow &lt;em&gt;theming&lt;&#x2F;em&gt; the first &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;ANSI_escape_code#Colors&quot;&gt;16 ANSI Colors&lt;&#x2F;a&gt;. Some fascinating historical accounts can be found on &lt;a href=&quot;https:&#x2F;&#x2F;invisible-island.net&#x2F;xterm&#x2F;xterm.faq.html&quot;&gt;xterm&#x27;s FAQ&lt;&#x2F;a&gt; where the maintainers answer questions such as: &lt;a href=&quot;https:&#x2F;&#x2F;invisible-island.net&#x2F;xterm&#x2F;xterm.faq.html#dont_like_blue&quot;&gt;I don&#x27;t like that shade of blue&lt;&#x2F;a&gt; with &amp;quot;Nobody Does...&amp;quot; 😀.&lt;br &#x2F;&gt;
Additionally, if we look at the wide variety of interpretations of color &lt;em&gt;Yellow&lt;&#x2F;em&gt; over the years, &lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;levitatingpineapple.github.io&#x2F;blog&#x2F;design&#x2F;color-naming&#x2F;yellow.webp&quot; alt=&quot;Yellow&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;stage-two&quot;&gt;Stage Two&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;em&gt;Referencing by &lt;strong&gt;WHERE&lt;&#x2F;strong&gt; a color is used&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The next logical step to solve Stage 1 issues is to name colors by where they are used instead. Think of references like &lt;code&gt;scrollbar&lt;&#x2F;code&gt;, &lt;code&gt;modalPopoverBackground&lt;&#x2F;code&gt;, &lt;code&gt;loginBorder&lt;&#x2F;code&gt; etc. For smaller projects and&#x2F;or projects that don&#x27;t change too much this approach actually works quite well. For large or dynamic projects I have experienced of two main issues:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;1. Maintainability&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Size of the theme grows &lt;strong&gt;linearly&lt;&#x2F;strong&gt; with the project which might still be manageable.&lt;&#x2F;li&gt;
&lt;li&gt;The time it takes to create a new theme can see a &lt;strong&gt;quadratic increase&lt;&#x2F;strong&gt; though, since for every color themes author has to find, where it&#x27;s used.&lt;&#x2F;li&gt;
&lt;li&gt;When adding new features to the software there are now &lt;code&gt;n!&lt;&#x2F;code&gt; 🙀 possible color transitions. This makes it almost impossible not to break existing themes without increasing reference count even further.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;&lt;strong&gt;2. Barrier of Entry&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;This tight coupling also means that the theme authors are required to have &lt;strong&gt;full&lt;&#x2F;strong&gt; understanding of the software.
Let&#x27;s look at &lt;a href=&quot;https:&#x2F;&#x2F;www.ableton.com&#x2F;en&#x2F;live&#x2F;what-is-live&#x2F;&quot;&gt;Ableton LIVE&lt;&#x2F;a&gt; - a fairly complex audio workstation, which has a thriving theming community. Creating themes for it is not trivial. Good luck finding where colors like &lt;code&gt;ViewCheckControlDisabledOff&lt;&#x2F;code&gt; is used in the software within 160 other references. To mitigate the issue people have been creating tools like &lt;a href=&quot;https:&#x2F;&#x2F;www.livethemes.co&quot;&gt;livethemes&lt;&#x2F;a&gt; with an online GUI theme editor, but that is not ideal either as the tool has to be constantly maintained. Looking at their &lt;a href=&quot;https:&#x2F;&#x2F;www.livethemes.co&#x2F;faq&quot;&gt;FAQ&lt;&#x2F;a&gt; the issues become more apparent:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Will these themes work in Ableton 11?&lt;&#x2F;strong&gt;&lt;br &#x2F;&gt;
Early tests show are they do work in Ableton 11 but are not fully optimized for it. As soon as the final version of Ableton 11 gets released we&#x27;ll get working on a &#x27;Download for Ableton 11&#x27; option.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;I can&#x27;t find {insert element here} to customize.&lt;&#x2F;strong&gt;&lt;br &#x2F;&gt;
There are lots of elements you can customize within a theme, and unfortunately we haven&#x27;t been able to map all of them to the preview yet...&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;As we can see, this approach starts to fall apart when number of colors approach triple digits due to the non-linear nature of growth in complexity. The tricky part is that the issues only reveal themselves after the project is already large and refactoring has become a significant effort.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;stage-three&quot;&gt;Stage Three&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;em&gt;Referencing by &lt;strong&gt;HOW&lt;&#x2F;strong&gt; a color relates to other colors&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;This approach introduces an abstraction layer between the software and palette. The colors are named by how they are used &lt;strong&gt;in relation to other colors&lt;&#x2F;strong&gt;. Like &lt;code&gt;foreground&lt;&#x2F;code&gt; being used to draw text on &lt;code&gt;background&lt;&#x2F;code&gt; or &lt;code&gt;accent&lt;&#x2F;code&gt; being used sparingly to guide user&#x27;s attention. The color names ideally do not leak any implementation details from the software itself or exact colors used. The format of the theme only defines a set of requirements like minimum contrast or matching perceived luminance. This approach also makes themes portable and enables them to be used across various applications, which is why this approach is often used UI Frameworks and operating systems.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;example&quot;&gt;Example&lt;&#x2F;h3&gt;
&lt;p&gt;As an example let&#x27;s imagine that we are building a CLI application and that we would like to use the existing 16 ANSI colors for compatibility. While we can&#x27;t anticipate every color palette it would be very helpful to precisely define what our expectations are using a graph. This can be done in few steps:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Rename the main colors to reflect their usage like 
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;bright-black&lt;&#x2F;code&gt; → &lt;code&gt;primaryBackground&lt;&#x2F;code&gt; &lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;normal-red&lt;&#x2F;code&gt; → &lt;code&gt;error&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;etc...&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;Create an undirected graph that describes the contrast requirements
&lt;ul&gt;
&lt;li&gt;I&#x27;ve grouped the colors to reduce the number of connections required&lt;&#x2F;li&gt;
&lt;li&gt;The connections describe minimum perceived contrast requirement for drawing text&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;levitatingpineapple.github.io&#x2F;blog&#x2F;design&#x2F;color-naming&#x2F;graph.svg&quot; alt=&quot;diagram&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;With this abstraction in place:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Graph can now serve as an interface between creators of the theme and software authors.&lt;&#x2F;li&gt;
&lt;li&gt;It enables colors to be applied consistently when adding new features.&lt;&#x2F;li&gt;
&lt;li&gt;Themes can now also be validated without explicit knowledge of current as well as future software implementations.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h1&gt;
&lt;p&gt;In this article I outlined three ways of referencing colors:&lt;&#x2F;p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Approach&lt;&#x2F;th&gt;&lt;th&gt;Description&lt;&#x2F;th&gt;&lt;th&gt;Example&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;By &lt;strong&gt;WHAT&lt;&#x2F;strong&gt; color is used&lt;&#x2F;td&gt;&lt;td&gt;naive approach&lt;&#x2F;td&gt;&lt;td&gt;&lt;code&gt;burgundyRed&lt;&#x2F;code&gt;, &lt;code&gt;paleBlue&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;By &lt;strong&gt;WHERE&lt;&#x2F;strong&gt; a color is used&lt;&#x2F;td&gt;&lt;td&gt;works, but has scalability issues&lt;&#x2F;td&gt;&lt;td&gt;&lt;code&gt;buttonTitle&lt;&#x2F;code&gt;, &lt;code&gt;scrollbar&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;By &lt;strong&gt;HOW&lt;&#x2F;strong&gt; a colors relate&lt;&#x2F;td&gt;&lt;td&gt;recommended&lt;&#x2F;td&gt;&lt;td&gt;&lt;code&gt;background&lt;&#x2F;code&gt;, &lt;code&gt;accent&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;p&gt;I discussed how theming progresses from basic copy-pasting to more sophisticated approaches with each stage addressing specific challenges, with the ultimate goal of simplifying theme management and ensuring consistency and scalability across projects.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Atreus Keyboard</title>
        <published>2024-01-05T09:41:00+02:00</published>
        <updated>2024-01-05T09:41:00+02:00</updated>
        
        <author>
          <name>
            
              Arturs
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://levitatingpineapple.github.io/blog/projects/atreus/"/>
        <id>https://levitatingpineapple.github.io/blog/projects/atreus/</id>
        
        <content type="html" xml:base="https://levitatingpineapple.github.io/blog/projects/atreus/">&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;levitatingpineapple.github.io&#x2F;blog&#x2F;projects&#x2F;atreus&#x2F;front.webp&quot; alt=&quot;image&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;A keyboard with 42 key &lt;a href=&quot;https:&#x2F;&#x2F;atreus.technomancy.us&quot;&gt;Atreus&lt;&#x2F;a&gt; layout. Uses &lt;a href=&quot;http:&#x2F;&#x2F;www.kailh.com&#x2F;en&#x2F;Products&#x2F;Ks&#x2F;CS&#x2F;&quot;&gt;Kailh Choc&lt;&#x2F;a&gt; low profile switches with hot-swap sockets. Compatible with ProMicro type controllers, but designed to be used with &lt;a href=&quot;https:&#x2F;&#x2F;nicekeyboards.com&#x2F;nice-nano&quot;&gt;Nice!Nano&lt;&#x2F;a&gt;. Capable of n-key rollover. There is enough spacing between keys to fit all of the commonly available keycaps - transparent, flat and concave. PCB and case exports available in the &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;levitatingpineapple&#x2F;atreus&#x2F;tree&#x2F;main&#x2F;export&quot;&gt;github repo&lt;&#x2F;a&gt;!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;materials&quot;&gt;Materials&lt;&#x2F;h2&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Name&lt;&#x2F;th&gt;&lt;th&gt;Part&lt;&#x2F;th&gt;&lt;th&gt;Size&lt;&#x2F;th&gt;&lt;th&gt;Amount&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;Switch&lt;&#x2F;td&gt;&lt;td&gt;&lt;a href=&quot;..switch.pdf&quot;&gt;CPG135001D0X&lt;&#x2F;a&gt;&lt;&#x2F;td&gt;&lt;td&gt;-&lt;&#x2F;td&gt;&lt;td&gt;42&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Socket&lt;&#x2F;td&gt;&lt;td&gt;&lt;a href=&quot;..socket.pdf&quot;&gt;CPG135001S30&lt;&#x2F;a&gt;&lt;&#x2F;td&gt;&lt;td&gt;-&lt;&#x2F;td&gt;&lt;td&gt;42&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Diode&lt;&#x2F;td&gt;&lt;td&gt;&lt;a href=&quot;..diode.pdf&quot;&gt;1N4148&lt;&#x2F;a&gt;&lt;&#x2F;td&gt;&lt;td&gt;SOD123&lt;&#x2F;td&gt;&lt;td&gt;42&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Controller&lt;&#x2F;td&gt;&lt;td&gt;&lt;a href=&quot;..controller.png&quot;&gt;nice!nano&lt;&#x2F;a&gt;&lt;&#x2F;td&gt;&lt;td&gt;-&lt;&#x2F;td&gt;&lt;td&gt;1&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Battery&lt;&#x2F;td&gt;&lt;td&gt;XZK 301230&lt;&#x2F;td&gt;&lt;td&gt;30x12x3.0mm&lt;&#x2F;td&gt;&lt;td&gt;1&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Bolt&lt;&#x2F;td&gt;&lt;td&gt;M2 Hex&lt;&#x2F;td&gt;&lt;td&gt;4mm&lt;&#x2F;td&gt;&lt;td&gt;8&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Machine Headers&lt;&#x2F;td&gt;&lt;td&gt;Mill-Max&lt;&#x2F;td&gt;&lt;td&gt;13p 3mm&lt;&#x2F;td&gt;&lt;td&gt;2&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;h2 id=&quot;pcb&quot;&gt;PCB&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;levitatingpineapple.github.io&#x2F;blog&#x2F;projects&#x2F;atreus&#x2F;pcb.webp&quot; alt=&quot;image&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;⚠️ No power switch for the battery&lt;&#x2F;li&gt;
&lt;li&gt;For every switch has a diode, soldered where an backlight LED would normally be located, providing n-key rollover.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;sockets&quot;&gt;Sockets&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;levitatingpineapple.github.io&#x2F;blog&#x2F;projects&#x2F;atreus&#x2F;battery.webp&quot; alt=&quot;image&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The PCB has 1.5mm wide holes for mounting the controller socket. This allows soldering machine headers flush with the board. The nice!nano controller originally ships with low profile headers, which won&#x27;t fit the battery underneath the controller, if soldered in this manner. Therefore a pair of 3mm tall headers had to be separately ordered.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;case&quot;&gt;Case&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;levitatingpineapple.github.io&#x2F;blog&#x2F;projects&#x2F;atreus&#x2F;exploded.webp&quot; alt=&quot;image&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;All 3d printing was done with 0.4mm nozzle at 0.2mm layer height from PETG.&lt;&#x2F;li&gt;
&lt;li&gt;The bottom case is attached using eight 4mm long M2 bolts.&lt;&#x2F;li&gt;
&lt;li&gt;Added feet using 1mm thick rubber tape&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;led&quot;&gt;Led&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;levitatingpineapple.github.io&#x2F;blog&#x2F;projects&#x2F;atreus&#x2F;leds.webp&quot; alt=&quot;image&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;In the nice!nano&#x27;s cover there are two openings for the LEDs. They can be filled with a dab of hot-glue before the print is removed form the bed to get a more diffused look &lt;a href=&quot;https:&#x2F;&#x2F;youtu.be&#x2F;p7GDyWs1uLk&quot;&gt;&lt;em&gt;...for science, you monster.&lt;&#x2F;em&gt;&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Adding Suport Mail</title>
        <published>2024-01-05T09:41:00+02:00</published>
        <updated>2024-01-05T09:41:00+02:00</updated>
        
        <author>
          <name>
            
              Arturs
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://levitatingpineapple.github.io/blog/swift/adding-support-mail/"/>
        <id>https://levitatingpineapple.github.io/blog/swift/adding-support-mail/</id>
        
        <content type="html" xml:base="https://levitatingpineapple.github.io/blog/swift/adding-support-mail/">&lt;p&gt;While including a flow for contacting support often comes as a last minute after-thought,&lt;br &#x2F;&gt;
having a great &lt;em&gt;complaining experience&lt;&#x2F;em&gt; can be instrumental in further improving the app.&lt;br &#x2F;&gt;
This is by no means a complex task, though there are a few gotchas along the way.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;gathering-context&quot;&gt;Gathering Context&lt;&#x2F;h2&gt;
&lt;p&gt;Feedback is often caused by a regression, which has been introduced in a certain release version, or only occurs in a specific environment.&lt;br &#x2F;&gt;
Therefore, it&#x27;s useful to include this sort of data in the initial support request.
This avoids unnecessary back and forth.
Here&#x27;s an example of gathering some commonly required context.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;swift&quot; class=&quot;language-swift &quot;&gt;&lt;code class=&quot;language-swift&quot; data-lang=&quot;swift&quot;&gt;var app: String {
	Bundle.main.info(&amp;quot;CFBundleDisplayName&amp;quot;) + &amp;quot; &amp;quot; +
	Bundle.main.info(&amp;quot;CFBundleShortVersionString&amp;quot;)
}

var os: String {
#if os(iOS)
	let platform = &amp;quot;iOS &amp;quot;
#elseif os(macOS)
	let platform = &amp;quot;MacOS &amp;quot;
#endif
	return platform + ProcessInfo.processInfo.operatingSystemVersionString
}
&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;em&gt;I&#x27;m using following extension to make fetching data from the app&#x27;s bundle more convenient.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;swift&quot; class=&quot;language-swift &quot;&gt;&lt;code class=&quot;language-swift&quot; data-lang=&quot;swift&quot;&gt;extension Bundle {
	func info(_ key: String) -&amp;gt; String {
		infoDictionary.flatMap { $0[key] as? String }
		?? &amp;quot;Missing &amp;quot; + key
	}
}
&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;constructing-link&quot;&gt;Constructing Link&lt;&#x2F;h2&gt;
&lt;p&gt;The &lt;a href=&quot;https:&#x2F;&#x2F;www.ietf.org&#x2F;rfc&#x2F;rfc2368.txt&quot;&gt;mailto:&lt;&#x2F;a&gt; URI scheme supports specifying &lt;strong&gt;subject&lt;&#x2F;strong&gt; and &lt;strong&gt;body&lt;&#x2F;strong&gt; using query parameters,&lt;br &#x2F;&gt;
which are then parsed by the user&#x27;s default mail client.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;swift&quot; class=&quot;language-swift &quot;&gt;&lt;code class=&quot;language-swift&quot; data-lang=&quot;swift&quot;&gt;var mailTo: URL {
	URL(string: &amp;quot;mailto:\(supportEmail)&amp;quot;)!
		.appending(
			queryItems: [
				URLQueryItem(name: &amp;quot;subject&amp;quot;, value: &amp;quot;Support: \(app)&amp;quot;),
				URLQueryItem(name: &amp;quot;body&amp;quot;, value: &amp;quot;\n\n\nOS: \(os)\nApp: \(app)&amp;quot;),
			]
		)
}
&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;user-interface&quot;&gt;User Interface&lt;&#x2F;h2&gt;
&lt;p&gt;When all is done, SwiftUI offers various ways of opening the constructed link:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Button&lt;&#x2F;strong&gt; - using &lt;a href=&quot;https:&#x2F;&#x2F;developer.apple.com&#x2F;documentation&#x2F;swiftui&#x2F;environmentvalues&#x2F;openurl&quot;&gt;openURL&lt;&#x2F;a&gt; environment variable&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre data-lang=&quot;swift&quot; class=&quot;language-swift &quot;&gt;&lt;code class=&quot;language-swift&quot; data-lang=&quot;swift&quot;&gt;struct ContactSupportView: View {
	@Environment(\.openURL) private var openURL

	var body: some View {
		Button(&amp;quot;Contact Support&amp;quot;) { openURL(mailTo) }
	}
}
&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Inline&lt;&#x2F;strong&gt; - Since iOS 15 SwiftUI supports limited markdown rendering. With one caveat that is.&lt;br &#x2F;&gt;
If we want to use anything else than simple string literals (like interpolation),&lt;br &#x2F;&gt;
&lt;a href=&quot;https:&#x2F;&#x2F;developer.apple.com&#x2F;documentation&#x2F;swiftui&#x2F;localizedstringkey&#x2F;init(stringliteral:)&quot;&gt;LocalizedStringKey(stringLiteral:)&lt;&#x2F;a&gt;
initialiser has to be used explicitly.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre data-lang=&quot;swift&quot; class=&quot;language-swift &quot;&gt;&lt;code class=&quot;language-swift&quot; data-lang=&quot;swift&quot;&gt;Text(
	LocalizedStringKey(
		stringLiteral: &amp;quot;Contact us [\(supportEmail)](\(mailTo))!&amp;quot;
	)
)
&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ul&gt;
&lt;li&gt;Link - The simplest option!&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre data-lang=&quot;swift&quot; class=&quot;language-swift &quot;&gt;&lt;code class=&quot;language-swift&quot; data-lang=&quot;swift&quot;&gt;Link(&amp;quot;Contact Support&amp;quot;, destination: mailTo)
&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;result&quot;&gt;Result&lt;&#x2F;h2&gt;
&lt;p&gt;Now users can create a simple mail template with a single tap!
As a bonus, consistent titles allow for some automation when handling support. This is especially true in cases where multiple apps are served fom a single support mail address.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;levitatingpineapple.github.io&#x2F;blog&#x2F;swift&#x2F;adding-support-mail&#x2F;mail.jpeg&quot; alt=&quot;image&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>SFPro Features</title>
        <published>2024-01-05T09:41:00+02:00</published>
        <updated>2024-01-05T09:41:00+02:00</updated>
        
        <author>
          <name>
            
              Arturs
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://levitatingpineapple.github.io/blog/swift/font-features/"/>
        <id>https://levitatingpineapple.github.io/blog/swift/font-features/</id>
        
        <content type="html" xml:base="https://levitatingpineapple.github.io/blog/swift/font-features/">&lt;ul&gt;
&lt;li&gt;This article is mostly based on &lt;a href=&quot;https:&#x2F;&#x2F;developer.apple.com&#x2F;videos&#x2F;play&#x2F;wwdc2015&#x2F;804&#x2F;&quot;&gt;Introducing the New System Fonts&lt;&#x2F;a&gt; WWDC session from 2015.&lt;&#x2F;li&gt;
&lt;li&gt;If you&#x27;d like to add this to your project as a &lt;code&gt;UIFont&lt;&#x2F;code&gt; extension check out this &lt;a href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;levitatingpineapple&#x2F;396d1524954153aea928bf59e0502744&quot;&gt;gist&lt;&#x2F;a&gt;.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;It&#x27;s been few years already since Apple ditched Helvetica in favor of their proprietary SanFrancisco font. While SFPro font family comes with plethora of features to satisfy even obscure typographic needs, actually finding and using these features can require parsing trough lengthy documentation.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;feature-keys&quot;&gt;Feature Keys&lt;&#x2F;h2&gt;
&lt;p&gt;Font features in &lt;a href=&quot;https:&#x2F;&#x2F;developer.apple.com&#x2F;fonts&#x2F;TrueType-Reference-Manual&#x2F;RM06&#x2F;Chap6AATIntro.html&quot;&gt;AAT&lt;&#x2F;a&gt; (Apple  Advanced Typography) are enabled using a pair of keys. &lt;code&gt;type&lt;&#x2F;code&gt; for selecting a some feature and &lt;code&gt;selector&lt;&#x2F;code&gt; for enabling it. A non-exhaustive list of these features can be found in the &lt;a href=&quot;https:&#x2F;&#x2F;developer.apple.com&#x2F;fonts&#x2F;TrueType-Reference-Manual&#x2F;RM09&#x2F;AppendixF.html&quot;&gt;Font Feature Registry&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;To make working with keys a bit simpler we can create a small abstraction. &lt;code&gt;Feature&lt;&#x2F;code&gt; struct will let us name the key pairs as static variables and enjoy autocomplete.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;swift&quot; class=&quot;language-swift &quot;&gt;&lt;code class=&quot;language-swift&quot; data-lang=&quot;swift&quot;&gt;struct Feature: Hashable {
	private let type: Int
	private let selector: Int
	var setting: [UIFontDescriptor.FeatureKey : Int] { 
		[.featureIdentifier: type, .typeIdentifier: selector]
	}
}
&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;blockquote&gt;
&lt;p&gt;⚠️ The naming above is intentional.&lt;br &#x2F;&gt;
The &lt;code&gt;.featureIdentifier&lt;&#x2F;code&gt; is set with &lt;code&gt;.k(...)Type&lt;&#x2F;code&gt; keys&lt;br &#x2F;&gt;
while &lt;code&gt;.typeIdentifier&lt;&#x2F;code&gt; uses &lt;code&gt;.k(...)Selector&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;The list of possible features is pretty long and some of them can only be used exclusively.
Here are most of the features, presented in the &lt;a href=&quot;https:&#x2F;&#x2F;developer.apple.com&#x2F;videos&#x2F;play&#x2F;wwdc2015&#x2F;804&#x2F;&quot;&gt;WWDC session&lt;&#x2F;a&gt;. As you can see the keys can get pretty verbose.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;swift&quot; class=&quot;language-swift &quot;&gt;&lt;code class=&quot;language-swift&quot; data-lang=&quot;swift&quot;&gt;static let straightSixAndNine = 
	Feature(type: kStylisticAlternativesType, selector: kStylisticAltOneOnSelector)
static let openFour = 
	Feature(type: kStylisticAlternativesType, selector: kStylisticAltTwoOnSelector)
static let alignedColon = 
	Feature(type: kStylisticAlternativesType, selector: kStylisticAltThreeOnSelector)
static let highLegibility = 
	Feature(type: kStylisticAlternativesType, selector: kStylisticAltSixOnSelector)
static let oneStoreyA = 
	Feature(type: kStylisticAlternativesType, selector: kStylisticAltSixOnSelector)
static let monospacedNumbers = 
	Feature(type: kNumberSpacingType, selector: kMonospacedNumbersSelector)
static let diagonalFractions = 
	Feature(type: kFractionsType, selector: kDiagonalFractionsSelector)
static let loverCaseSmallCaps = 
	Feature(type: kLowerCaseType, selector: kLowerCaseSmallCapsSelector)
static let upperCaseSmallCaps = 
	Feature(type: kUpperCaseType, selector: kUpperCaseSmallCapsSelector)
&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;caching&quot;&gt;Caching&lt;&#x2F;h2&gt;
&lt;p&gt;When required frequently enough, constructing fonts with typographic features can be considered an expensive operation. Therefore, we can introduce a simple caching mechanism using a dictionary. This dictionary is keyed by a hash-able struct, which represents the required function parameters.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;swift&quot; class=&quot;language-swift &quot;&gt;&lt;code class=&quot;language-swift&quot; data-lang=&quot;swift&quot;&gt;private static var cache = [Key: UIFont]()
private struct Key: Hashable {
	let size: CGFloat
	let weight: Weight
	let features: [Feature]
	let italic: Bool
}
&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;usage&quot;&gt;Usage&lt;&#x2F;h2&gt;
&lt;p&gt;With setup work out of the way, the font can now be constructed! &lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;First we create a &lt;code&gt;Key&lt;&#x2F;code&gt; from the function parameters and return a cached font, if one exists. &lt;&#x2F;li&gt;
&lt;li&gt;Then a &lt;code&gt;FontDescriptor&lt;&#x2F;code&gt; is constructed as a base&lt;&#x2F;li&gt;
&lt;li&gt;After that a font is composed by adding attributes and symbolic traits.&lt;&#x2F;li&gt;
&lt;li&gt;Finally, the font is stored in the cache and returned.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre data-lang=&quot;swift&quot; class=&quot;language-swift &quot;&gt;&lt;code class=&quot;language-swift&quot; data-lang=&quot;swift&quot;&gt;static func systemFont(
	ofSize size: CGFloat,
	weight: Weight = .regular,
	features: [Feature] = [],
	italic: Bool = false
) -&amp;gt; UIFont {
	let key = Key(size: size, weight: weight, features: features, italic: italic)
	if let font = cache[key] { return font }
	let descriptor = UIFont.systemFont(ofSize: size, weight: weight).fontDescriptor
	let font = UIFont(
		descriptor: descriptor
			.addingAttributes([.featureSettings: features.map { $0.setting }])
			.withSymbolicTraits(descriptor
				.symbolicTraits
				.union(italic ? .traitExpanded : .init(rawValue: 0))
			) ?? descriptor,
		size: size
	)
	cache[key] = font
	return font
}
&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;for-designers&quot;&gt;For Designers&lt;&#x2F;h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Figma&lt;&#x2F;strong&gt; has supported open type features since 2019.&lt;br &#x2F;&gt;
More information available in the &lt;a href=&quot;https:&#x2F;&#x2F;www.figma.com&#x2F;blog&#x2F;opentype-font-features&#x2F;&quot;&gt;Ode to OpenType&lt;&#x2F;a&gt; article.&lt;&#x2F;li&gt;
&lt;li&gt;If the design is done using &lt;strong&gt;Sketch&lt;&#x2F;strong&gt; OpenType features are available from the menu bar: &lt;code&gt;Text&#x2F;OpenTypeFeatures&#x2F;...&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;levitatingpineapple.github.io&#x2F;blog&#x2F;swift&#x2F;font-features&#x2F;sketch.jpg&quot; alt=&quot;Sketch&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
</content>
        
    </entry>
</feed>
