pepevill Posted May 26 Posted May 26 En 19/1/2024 a las 12:15, Tanarg dijo: Producing DCS-BIOS Gauges – Example coding and lessons learned Thought that I would share my work to-date on DCS-BIOS controlled gauges. First with three example individual gauges for the F-18, then a more complex example (first example below and attached), building on these gauges, to have them all controlled from a single ESP32, hence saving on COM ports. I will explain the structure of each bit of code and how it all fits together. Happy to take comments or suggestions for improvements. I have kept the code down to the minimum needed. I am using the GC9A01 1.28 inch 3.3v TFT round displays, but I will try to explain where you will have to amend the code if you are using something else. The code should work with any ESP32 that you can program via the Arduino IDE. Hardware First task is to ensure that your hardware is configured correctly. We will be using the TFT_eSPI library, so this needs to be loaded and configured correctly. In particular you need to ensure that the correct driver is loaded (GC9A01 in this case) and that the pin outs are correct. This can be done in the User_Setup.h file within the TFT_eSPI folder. I suggest running the Color Test from the TFT_eSPI examples to check everything is working correctly. It you are using a different display you will have to select an alternative driver etc. Examples The three gauges that I am going to cover are: Battery Gauge Hydraulic Pressure Brake Pressure All three sketches share a common format, with the main differences around the number, position and rotation of the needles. When we get to the three gauge sketch you will notice that, in addition to adding the code to get the displays to work together, I had to change a few of the names to avoid any conflict or just make things more consistent. Lesson 1 – If you are going to make multiple gauges that you might want to later combine into a single sketch, develop a format for all your function and variable names. Producing Graphics The method of producing the graphics is common to all the gauges, so I will start with that. All the gauges consist of a background image and one or more needles. These are converted into ‘Sprites’ in the code which is the most efficient means of using them. I used the following tools for producing the graphics, but alternatives are fine: Paint.net – Used to produce and manipulate .png images. I found that a tool that supported layers was very useful in developing the images, allowed trial and error. I also found myself using Microsoft Paint on occasion when I needed a very basic capability. Icd-image-converter – This was used to convert the .png files into code that the Arduino IDE can use. Background Image – This needs to be produced at 240x240 pixels (for the GC9A01), I kept with 8-bit colors. My experience is to keep the background of the background image black (and I mean black, not dark grey). Lesson 2 – Not doing this proved troublesome later when the needles passed over the not quite black areas and left a trace behind. The final image needs to be converted, in Icd-image-converter, to R5G6B5 format with a block size of 16 bit and Little-Endian Byte order. Once configured in the tool you can use the ‘Show Preview’ option to display the resultant code and cut and paste it into your sketch. Needle Image - This follows the same process as the background. The size of the needle will be driven by your particular gauge, but will be about 80-120 pixels tall and 15 -25 pixels wide. Try to keep the needle symmetrical and an odd number of pixels wide (see pivot later). Keep your needle background color something you are not using elsewhere on the image as once you have imported it into the IDE you will want to use the Find drop down to replace this color code with the one for TRANSPARENT (0x0120). Pivots – You will need to identify pivot points for your background image and needles. The pivot for the background (GC9A01) will be 120x120. The needle(s) pivot will depend on the needle(s), this is where the odd number of pixels for the needle width comes in handy. Lesson 3 - The needle overall size and pivot point must be accurate in the code otherwise you will get all sorts of strange behaviour. Example 1 - Battery Gauge (F-18_Battery_Gauge.ino) Having produced the graphics for your Battery gauge, we can start on the coding. We will need the ‘colorDepth’ regularly, so set a constant for this (7). ‘include’ the two image files, CDS-BIOS and the TFT_eSPI library (9, 10, 14, 16) ‘define’ DCSBIOS_DEFAULT_SERIAL (12) Set up the Sprites, there will be three in this case, the background and two needles (both the same image) (18 – 20) Declare initial values for the needle angles (22, 23) Call the two sections of DCS-BIOS code, one for each needle (U and E) (25 – 37). There are two common lines of code within this that I will explain. The first ‘angleMapU or E’ uses the Map command with the input ‘newValue’ from the DCS-BIOS function, along with the range of possible outputs from DCS-BIOS (0-65535), together with the range of desired values of the needle to produce a needle angle. In the first case the desired needle value we want is on the left of the gauge between -150 to -30, the needle degrees are measured from 0 (12 O’clock position). In the second case the needle is on the right of the gauge and hence 150 to 30 e.g. both needles will start at the bottom and climb upwards. ‘plotGauge’ is then used to plot both needle positions, even if only one has actually changed. Within the setup function (41 – 47) we include the necessary DCS-BIOS call, initialize TFT and fill the screen with BLACK color. There are then three lines of code that impact the Byte order of the graphics. These interact with the Big and Little-Endian settings. You might need to change these in your own projects, but they work in the example here. There is then a call to a Bit_test that will run on power up and cycle the needles through there min – max movement. You can leave as is, or comment this line out as required. The loop function (52) only needs the DCS-BIOS reference. Lines 55 – 69 are the Bit_test. Lines 71 – 82 are the ‘plotGauge’ function. It starts of creating the three Sprites and pushing the needles, as the desired angles, onto the background. The TFT_TRANSPARENT tell the code to not display anything in this color. Once complete we delete the ‘Sprites’ to free up memory. Lines 84-91 create the background, setting the colorDepth, size and pivot point, and pushing the ‘Battery’ image onto the screen. Lines 93 – 99 and 101 – 107 create the two needles, note the difference between the total size of the needle and its pivot point. If you have done everything correctly you should be good to go…. F-18_Battery_Gauge.ino 4.33 kB · 415 descargas BatteryBackground.h 450.86 kB · 113 descargas Needle.h 11.03 kB · 113 descargas I can't get the code to do anything. When DCS isn't running, I see the battery meter. When I run the "connect serial port" program with the correct port, data starts to appear with DCS and the screen goes black. When I close DCS, the screen returns to the battery meter. What could it be? I use an ESP32-S3.
Recommended Posts