[SOLVED & RPi Pico Issue] NANO - CH340 - Win 11 - Flashing issues / RPi Pico communication issue MessagePort

Support for Arduino in combination with Air Manager and Air Player

Moderators: russ, Ralph

Message
Author
SimPassion
Posts: 5346
Joined: Thu Jul 27, 2017 12:22 am

Re: [SOLVED & RPi Pico Issue] NANO - CH340 - Win 11 - Flashing issues / RPi Pico communication issue MessagePort

#111 Post by SimPassion »

Thanks Joe for the advice and yes I've seen some strange behavior using global brigthness. Just going back to home atm, so I will have a look again to add some enhancements, then posting the INO sketch when all is working properly.


User avatar
jph
Posts: 2857
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

Re: [SOLVED & RPi Pico Issue] NANO - CH340 - Win 11 - Flashing issues / RPi Pico communication issue MessagePort

#113 Post by jph »

What would be fun Gilles is to have 3 potentiometers to ADC inputs that control R, G and B so that you can exactly tune a custom colour :) . Maybe a button to change the functions of the adc inputs for other uses ?
That is what I was planning to do with the encoders, and then a button to 'save' the input and hence save the custom colour. Due to the use of the special routine to balance the color scale on the arduino then the colour of the led should match the colour of the 'button' is the same R G B code was used for the button. This assumes a high initial brightness (global) level.
Great fun :mrgreen:
Joe. CISSP, MSc.

SimPassion
Posts: 5346
Joined: Thu Jul 27, 2017 12:22 am

Re: [SOLVED & RPi Pico Issue] NANO - CH340 - Win 11 - Flashing issues / RPi Pico communication issue MessagePort

#114 Post by SimPassion »

I see, this would allow to re-use the stored RGB values from persistance and get the expected tint on visual on each AM instrument run ... !?
Like for example the specific warm amber annunciators leds, together with the blue ones, in the 737 OVH.

User avatar
jph
Posts: 2857
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

Re: [SOLVED & RPi Pico Issue] NANO - CH340 - Win 11 - Flashing issues / RPi Pico communication issue MessagePort

#115 Post by jph »

SimPassion wrote: Mon Feb 05, 2024 6:31 pm I see, this would allow to re-use the stored RGB values from persistance and get the expected tint on visual on each AM instrument run ... !?
Like for example the specific warm amber annunciators leds, together with the blue ones, in the 737 OVH.
Yes, exactly :D
Joe. CISSP, MSc.

SimPassion
Posts: 5346
Joined: Thu Jul 27, 2017 12:22 am

Re: [SOLVED & RPi Pico Issue] NANO - CH340 - Win 11 - Flashing issues / RPi Pico communication issue MessagePort

#116 Post by SimPassion »

jph wrote: Mon Feb 05, 2024 7:31 pm Yes, exactly :D
It's on the way ...
Just to mention, this will also answer to one of my requirement, to light up the simu rig keyboard area when night flights are in progress, with the goal to get a specific tint around soft amber/yellow/white mix with accurate intensity (a bit of what we get with the zibomod panels light), which would be perfect for liners and would avoid too much light in the surrounding. Currently without it, it's just a matter of remembering keyboard layout almost in the dark, as monitors light is not enough, with some attempts and fail ... 😲
So, using persitence, the proper mix of colors and intensity will come straight on each AM instrument run 👍

SimPassion
Posts: 5346
Joined: Thu Jul 27, 2017 12:22 am

Re: [SOLVED & RPi Pico Issue] NANO - CH340 - Win 11 - Flashing issues / RPi Pico communication issue MessagePort

#117 Post by SimPassion »

First INO shot which is working properly, though requires to be enhanced and to add the button press handling to trigger the save to persistence in the AM instrument :

Code: Select all

// controlling addressable leds WS2812 units (neopixel types) from Air Manager with simMessagePort
// Written by Joe Hanley - 2022 - (JPH on Air Manager forums)
// You are free to use and modify this code for NON COMMERCIAL purposes but all credits must remain in place
//
// Thanks to :-
// Adafruit for neopixel library and gamma correction calculations 
// Sim Innovations / Air Manager for SiMessagePort library


//  enjxp_SimPassion	2024	adding 3 x Potentiometer ADC input for hardware RGB handling. From initial author Rui Santos
//  Complete project details at https://RandomNerdTutorials.com/raspberry-pi-pico-analog-inputs-arduino/


//includes
#include <Adafruit_NeoPixel.h>
#include <si_message_port.hpp>

//defines

//constants

const uint8_t  LED_DATA_PIN   =  23;        // data pin to NeoPixels.  On chinese 4mb pico gpio 23 is used for the on board WS2812B
const uint8_t  NUM_LEDS       =   1;        // how many LED (neopixels) in total ?
const uint16_t DELAY_PERIOD   = 200;        // time (in milliseconds) to pause // used for debug and testing

// const uint8_t  LED_DATA_PIN   =   2;     // data pin to NeoPixels. (on genuine PICO - ) Use whatever you want.
// const uint8_t  NUM_LEDS       =  10;     // how many LED (neopixels) in total ?
// const uint8_t  NUM_LEDS       =  320;    // how many LED (neopixels) in total ? -- for speed test only

// contant array for our gamma corrected lookup table
extern const uint8_t gamma8[];              // gamma correction table at end of code - stored in flash memory - not sram

// Potentiometer is connected to GPIO 26 (Analog ADC0)
const int potRedPin = 26;
const int potGreenPin = 27;
const int potBluePin = 28;

const int rtmpdelay = 2000;

//variables
//bool a = 0; // for speed test only

// variable for storing the potentiometer value
int potRedValue = 0;
int potGreenValue = 0;
int potBlueValue = 0;
int potRedSave = 0;
int potGreenSave = 0;
int potBlueSave = 0;
int bright = 35;
int rtmpcount = 0;

//objects

Adafruit_NeoPixel pixels( NUM_LEDS, LED_DATA_PIN, NEO_GRB + NEO_KHZ800 );

SiMessagePort* messagePort;      // initialise our messageport object - here we chose the name 'messagePort'

// int32_t red = pixels.Color(255,0,0 ); example of what can be also used as pre-defined.

/* **************************************************************************************************************************
 *                                                                                                                          *
 *                                                 		  Setup                                                             *
 *                                                                                                                          *
 ************************************************************************************************************************** */

void setup() {   // run once at startup
  
	analogReadResolution(8);
	pixels.begin();                     // initialise NeoPixel pixels object (REQUIRED)
	pixels.clear();                     // set all pixel colors to 'off'
	pixels.setBrightness(bright);
	pixels.show();                      // ensure brightness level is sent
	delay(1000);

	pinMode(LED_BUILTIN,OUTPUT);

// Init library on channel A and Arduino type MEGA 2560
//  messagePort = new SiMessagePort(SI_MESSAGE_PORT_DEVICE_ARDUINO_NANO, SI_MESSAGE_PORT_CHANNEL_F, new_message_callback);
//  messagePort = new SiMessagePort(SI_MESSAGE_PORT_DEVICE_RPI_PICO, SI_MESSAGE_PORT_CHANNEL_A, new_message_callback);
//  messagePort = new SiMessagePort(SI_MESSAGE_PORT_DEVICE_ARDUINO_MEGA_2560, SI_MESSAGE_PORT_CHANNEL_A, new_message_callback);  
	messagePort = new SiMessagePort(SI_MESSAGE_PORT_DEVICE_ARDUINO_UNO, SI_MESSAGE_PORT_CHANNEL_A, new_message_callback);  

}               // setup end

/* **************************************************************************************************************************
 *                                                                                                                          *
 *                                                   MAIN LOOP                                                              *
 *                                                                                                                          *
 ************************************************************************************************************************** */

void loop() {  // our main program loop

//  // test speed of transfer with 255 leds
//  for (uint16_t i = 0; i < 255; i ++ ) {
//    setPixelsGammaCorrected (i,255,0,0);
//  }
//  //delay(20);
//  pixels.show();
//  
//  for (uint16_t i = 0; i < 255; i ++ ) {
//    setPixelsGammaCorrected (i,0,255,0);
//  }
//  //delay(20);
//  pixels.show();
//  
// for (uint16_t i = 0; i < 255; i ++ ) {
//    setPixelsGammaCorrected (i,0,0,255);
//  }
//  //delay(20);
//  pixels.show();
//
// digitalWrite(LED_BUILTIN,a);
// a = a ^ 1; // toggle led 

        /* setPixelsGammaCorrected (0,80,20,80);
        pixels.show(); */

// Red ADC Input --> pixels Red Component
//	potRedValue = round(analogRead(potRedPin) * 255);
	potRedValue = round(analogRead(potRedPin));
	if (potRedValue == potRedSave)
	{
	}
	else
	{
		potRedSave = potRedValue;
	}

// Green ADC Input --> pixels Green Component
//	potGreenValue = round(analogRead(potGreenPin) * 255);
	potGreenValue = round(analogRead(potGreenPin));
	if (potGreenValue == potGreenSave)
	{
	}
	else
	{
		potGreenSave = potGreenValue;
	}

// Blue ADC Input --> pixels Blue Component
//	potBlueValue = round(analogRead(potBluePin) * 255);
	potBlueValue = round(analogRead(potBluePin));
	if (potBlueValue == potBlueSave)
	{
	}
	else
	{
		potBlueSave = potBlueValue;
	}

	setPixelsGammaCorrected (0,potRedSave,potGreenSave,potBlueSave);
	pixels.show();
	//delay(500);

	rtmpcount++;
	
	if (rtmpcount >= rtmpdelay)
	{
		rtmpcount = 0;
		messagePort->DebugMessage(SI_MESSAGE_PORT_LOG_LEVEL_INFO, (String)" ADC Red = " + potRedSave);
		messagePort->DebugMessage(SI_MESSAGE_PORT_LOG_LEVEL_INFO, (String)" ADC Green = " + potGreenSave);
		messagePort->DebugMessage(SI_MESSAGE_PORT_LOG_LEVEL_INFO, (String)" ADC Blue = " + potBlueSave);
	}
	messagePort->Tick(); // make sure to call frequently 
  
}              // end main program loop

/* **************************************************************************************************************************
 *                                                                                                                          *
 *                                                   Functions                                                              *
 *                                                                                                                          *
 ************************************************************************************************************************* */

// new message received from AM via messagePort:     

	static void new_message_callback(uint16_t message_id, struct SiMessagePortPayload* payload) {
	// Do something with a message from Air Manager or Air Player

// temp var
	uint8_t temp = 0;

	if (payload == NULL) { // debug only
	messagePort->DebugMessage(SI_MESSAGE_PORT_LOG_LEVEL_INFO, (String)message_id + " is without payload");
  }
  
// now check incoming messagePort data from AM. We are looking for message_id numbers from 100 upwards 

	switch (message_id) {      // what is our message_id ?  -- check and action

    
    case 100:                // is this a messageport id of 100 ? (neoxpixel message id - we can choose anything we want, just ensure it is the same in AM and here
                             // message_id 100 is out 'neopixel' message ID from AM. Expect 4 bytes.
                             // so send the data to the ledcontrol to set the neopixel
        setPixelsGammaCorrected (payload->data_byte[0],payload->data_byte[1],payload->data_byte[2],payload->data_byte[3]);
		// pixels.setBrightness(bright);
        pixels.show();       // 'Light 'em up Bob!'
        break;
    
    case 101:                // same routine as above BUT no auto pixel.show() so you can call multiple times and then call pixel.show()
                             // message_id 100 is out 'neopixel' message ID from AM. Expect 4 bytes.
                             // so send the data to the ledcontrol to set the neopixel
        setPixelsGammaCorrected (payload->data_byte[0],payload->data_byte[1],payload->data_byte[2],payload->data_byte[3]);
        break;

    case 102:                // independent show command to allow multiple pixel addressing prior to lighting them up.
        pixels.show();
        break;
    
    case 103:                                          // this is a GLOBAL reset (off) command
        pixels.clear();                                // set ALL leds to 'off' 
        break; 
    
    case 104:                                          // this is a GLOBAL set brightness command
        temp = payload->data_byte[0];                  // temp storage - you cannot declare within the case ! 
        pixels.setBrightness(temp);   // 0 to 255      // cannot pass payload->data_byte[0] directly to the function here
        messagePort->DebugMessage(SI_MESSAGE_PORT_LOG_LEVEL_INFO, (String)"brightness = " + temp);
        break;

        // our message_id list
        // 100 address a numbered led and immediately auto 'show' the result
        // 101 address a numbered led and do NOT show the result until 'show' is called (message_id 102 with nil payload)
        // 102 nil payload here, will immediately send a 'show' command to action any data without auto show
        // 103 nil payload here, will immediately CLEAR - turn off - ALL leds in the chain.
        // 104 GLOBAL set brightness (USE WITH CARE!) . Sets GLOBAL brightness. you need to call message_id 102 after (show) to action this.
        // others may follow as required.
      

    default:
        // set any default action here - or -
        //  ??  feck knows what id that was for so ignore it ;-) -well, we #should# add a messageport send or error message to inform AM that we didnt get a valid id - to do list.
        break;
  } // end switch (message_id)
   

}

/* ************************************************************************************************************************** */

// gamma corrected neopixel call wrapper routine by Joe (JPH)
// test of gamma corrected function for setting colours
// this is the main function to transmit the data to the appropriate led in the chain (setPixelsGammaCorrected)

void setPixelsGammaCorrected (uint8_t ledNumber, uint8_t r, uint8_t g, uint8_t b) { // call with led number and r,g,b and it should set the led
//call this routine to send a gamma corrected rgb value to the numbered led
  pixels.setPixelColor(ledNumber, pixels.Color(
                         pgm_read_byte(&gamma8[r]),
                         pgm_read_byte(&gamma8[g]),
                         pgm_read_byte(&gamma8[b])));
  //pixels.show(); // needs to be on separate function ! - we may need to set multiple pixels prior to call.

}

/* ************************************************************************************************************************** */

//  Gamma correction table - see https://learn.adafruit.com/led-tricks-gamma-correction/the-quick-fix
//  this table converts the led gamma to approximate the actual human eye's perception of colour
//  hence you can use 'colour numbers' R,G,B from any source as a colour guide 
/*
    Accessing the table - as is
    result = pgm_read_byte(&gamma8[input]);
    or as a rountine in -
  pixel.setPixelColor(pixelNumber,
  pgm_read_byte(&gamma8[red]),
  pgm_read_byte(&gamma8[green]),
  pgm_read_byte(&gamma8[blue]));


*/
const uint8_t PROGMEM gamma8[] = {
  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  1,
  1,  1,  1,  1,  1,  1,  1,  1,  1,  2,  2,  2,  2,  2,  2,  2,
  2,  3,  3,  3,  3,  3,  3,  3,  4,  4,  4,  4,  4,  5,  5,  5,
  5,  6,  6,  6,  6,  7,  7,  7,  7,  8,  8,  8,  9,  9,  9, 10,
  10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
  17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
  25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
  37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
  51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
  69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
  90, 92, 93, 95, 96, 98, 99, 101, 102, 104, 105, 107, 109, 110, 112, 114,
  115, 117, 119, 120, 122, 124, 126, 127, 129, 131, 133, 135, 137, 138, 140, 142,
  144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 167, 169, 171, 173, 175,
  177, 180, 182, 184, 186, 189, 191, 193, 196, 198, 200, 203, 205, 208, 210, 213,
  215, 218, 220, 223, 225, 228, 231, 233, 236, 239, 241, 244, 247, 249, 252, 255
  };

User avatar
jph
Posts: 2857
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

Re: [SOLVED & RPi Pico Issue] NANO - CH340 - Win 11 - Flashing issues / RPi Pico communication issue MessagePort

#118 Post by jph »

That is a really neat idea to use it for the simu rig keyboard lights!. If you need any diffusion you can try baking paper / grease proof paper in English, it is the white paper that you can place in an oven during cooking to stop things sticking. It works well as a diffuser and you can use multiple layers but YMMV.
Also I have mentioned before that car headlight / tail light 'tint' material is also excellent and can be used to show a dark surface when the light unit is off.
One problem I have come across is that sometimes the neo pixels are TOO bright and it is better to use layers of diffuser paper or headlight tint to reduce the brightness as reducing the rgb too much may distort the colour, although the range of brightness is still extremely good - that is at the rgb level with appropriate global value set at the beginning on startup. Plus you can bake a cake with the left over material :lol:
Joe. CISSP, MSc.


User avatar
jph
Posts: 2857
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

Re: [SOLVED & RPi Pico Issue] NANO - CH340 - Win 11 - Flashing issues / RPi Pico communication issue MessagePort

#120 Post by jph »

Hello Gilles, :)
Very neat!. Like all PSU adapters I think it needs a good selection of fuses on the output (as does the one I linked to above) - I have a plan to cut the tracks and solder fuse holders onto the back of the PCB on mine.

Joe
Joe. CISSP, MSc.

Post Reply