Welcome to Cockpitbuilders.com. Please login or sign up.

March 28, 2024, 01:38:05 AM

Login with username, password and session length

PROUDLY ENDORSING


Fly Elise-ng
132 Guests, 0 Users
Members
Stats
  • Total Posts: 59,639
  • Total Topics: 7,853
  • Online today: 130
  • Online ever: 582
  • (January 22, 2020, 08:44:01 AM)
Users Online
Users: 0
Guests: 132
Total: 132

COUNTDOWN TO WF2022


WORLDFLIGHT TEAM USA

Will Depart in...

Recent

Welcome

Teensy LC Left MCP

Started by kurt-olsson, November 04, 2020, 12:25:07 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

kurt-olsson


#define ENCODER_DO_NOT_USE_INTERRUPTS

#include <Bounce.h>
#include <Encoder.h>
#include <DogLcd.h>
#include "LedControl.h"

const int PIN_DIN = 27;
const int PIN_CS = 0;
const int PIN_CLK = 1;

LedControl ledcontrol1 = LedControl(PIN_DIN,PIN_CLK,PIN_CS,2);

Encoder encoder_course_pilot = Encoder(43, 42);       
long couse_pilot_previous = 0;
FlightSimCommand course_pilot_up;
FlightSimCommand course_pilot_down;

Encoder encoder_airpseed = Encoder(45, 44);       
long airspeed_previous = 0;
FlightSimCommand airspeed_up;
FlightSimCommand airspeed_down;

const int PIN_BUTTON_SPEED = 21;
const int PIN_BUTTON_N1 =  22;
const int PIN_BUTTON_CO = 24;

const int PIN_FLIGHT_DIRECTOR_SWITCH = 25;

Bounce button_n1 = Bounce(PIN_BUTTON_N1,5); //Orange is + Blue ground in cable colors
FlightSimCommand n1_press;

Bounce button_speed = Bounce(PIN_BUTTON_SPEED,5); //Orange is + Blue ground in cable colors
FlightSimCommand speed_press;

Bounce button_co = Bounce(PIN_BUTTON_CO,5); //Orange is + Blue ground in cable colors
FlightSimCommand co_press;

const int PIN_N1_LED_ENABLED = 23;
const int PIN_SPEED_LED_ENABLED = 20;
const int PIN_FLIGHT_DIRECTOR_A_ENABLED = 19;
const int PIN_AUTOTHROTTLE_ENABLED = 18;

FlightSimInteger autopilot_n1_status;
FlightSimInteger autopilot_speed_status1;
FlightSimInteger autopilot_master_capt_status;
FlightSimInteger autopilot_autothrottle_status;

FlightSimInteger autopilot_fd_ca;

//Display segment values
FlightSimFloat mcp_ias;
FlightSimFloat mcp_course;
FlightSimFloat mcp_heading;


void setup() {

  Serial.begin(9600);
 
  pinMode(40, INPUT_PULLUP);
  pinMode(41, INPUT_PULLUP);
  pinMode(42, INPUT_PULLUP);
  pinMode(43, INPUT_PULLUP);
  pinMode(44, INPUT_PULLUP);
  pinMode(45, INPUT_PULLUP);
 
  pinMode(PIN_N1_LED_ENABLED, OUTPUT);
  pinMode(PIN_SPEED_LED_ENABLED, OUTPUT);
  pinMode(PIN_FLIGHT_DIRECTOR_A_ENABLED, OUTPUT);
  pinMode(PIN_AUTOTHROTTLE_ENABLED, OUTPUT);

  //buttons
  pinMode(PIN_BUTTON_SPEED, INPUT_PULLUP);
  pinMode(PIN_BUTTON_N1, INPUT_PULLUP);
  pinMode(PIN_BUTTON_CO, INPUT_PULLUP);
 
  pinMode(PIN_FLIGHT_DIRECTOR_SWITCH, INPUT_PULLUP);
 
  course_pilot_up = XPlaneRef("laminar/B738/autopilot/course_pilot_up");
  course_pilot_down = XPlaneRef("laminar/B738/autopilot/course_pilot_dn");

  airspeed_up = XPlaneRef("sim/autopilot/airspeed_up");
  airspeed_down = XPlaneRef("sim/autopilot/airspeed_down");

  n1_press = XPlaneRef("laminar/B738/autopilot/n1_press");
  speed_press = XPlaneRef("laminar/B738/autopilot/speed_press");
  co_press = XPlaneRef("laminar/B738/autopilot/change_over_press");

  autopilot_n1_status = XPlaneRef("laminar/B738/autopilot/n1_status");
  autopilot_speed_status1 = XPlaneRef("laminar/B738/autopilot/speed_status1");
  autopilot_master_capt_status = XPlaneRef("laminar/B738/autopilot/master_capt_status");
  autopilot_autothrottle_status = XPlaneRef("laminar/B738/autopilot/autothrottle_status");

  autopilot_fd_ca = XPlaneRef("laminar/B738/switches/autopilot/fd_ca");

  mcp_ias = XPlaneRef("sim/cockpit2/autopilot/airspeed_dial_kts_mach");
  mcp_course = XPlaneRef("laminar/B738/autopilot/course_pilot");
  mcp_heading = XPlaneRef("sim/cockpit2/autopilot/heading_dial_deg_mag_pilot");

 
  // put your setup code here, to run once:
  for(int index=0;index<ledcontrol1.getDeviceCount();index++) {
      ledcontrol1.shutdown(index,false);
  }
  ledcontrol1.setIntensity(0,4);
  ledcontrol1.setIntensity(1,8);
}


long RunCommandByEncoder(long encodervalue, FlightSimCommand x_plane_command_up, FlightSimCommand x_plane_command_down, long previous) {
 
  if (encodervalue != previous) {
    delay(5);
    long pulse = encodervalue - previous;
    previous = encodervalue;
    if (pulse > 0 )
      x_plane_command_up.once(); 
    else
      x_plane_command_down.once(); 
  }
  return previous;
}

void loop() {

// normally the first step in loop() should update from X-Plane
  FlightSim.update();

  couse_pilot_previous = RunCommandByEncoder(encoder_course_pilot.read(), course_pilot_up, course_pilot_down, couse_pilot_previous);
  airspeed_previous    = RunCommandByEncoder(encoder_airpseed.read(), airspeed_up, airspeed_down, airspeed_previous);
 
  if (button_speed.fallingEdge())
    speed_press.once();

  if (button_n1.fallingEdge())
    n1_press.once();   

  if (button_co.fallingEdge())
    co_press.once();

  button_n1.update();
  button_speed.update();
  button_co.update();

 
  //autopilot Leds on buttons
  digitalWrite(PIN_N1_LED_ENABLED,    autopilot_n1_status);
  digitalWrite(PIN_SPEED_LED_ENABLED, autopilot_speed_status1);
  digitalWrite(PIN_FLIGHT_DIRECTOR_A_ENABLED, autopilot_master_capt_status);
  digitalWrite(PIN_AUTOTHROTTLE_ENABLED, autopilot_autothrottle_status);

  if (autopilot_fd_ca != digitalRead(PIN_FLIGHT_DIRECTOR_SWITCH))
    autopilot_fd_ca = digitalRead(PIN_FLIGHT_DIRECTOR_SWITCH);

  displayCourse(String(mcp_course));
  displaySpeed(String(mcp_ias));
  displayHeading(String(mcp_heading)); 
}


void displayCourse(String value) {
  ledcontrol1.setChar(0,6,value[0],false);
  ledcontrol1.setChar(0,5,value[1],false);
  ledcontrol1.setChar(0,4,value[2],false);
}

void displaySpeed(String value) {
  //Display Mach value to the left
  if(value[1] == '.') {
    ledcontrol1.setChar(1,7,' ',false);
    ledcontrol1.setChar(1,6,' ',false);
    ledcontrol1.setChar(1,5,' ',false); 
    ledcontrol1.setChar(0,2,value[0],true);
    ledcontrol1.setChar(0,1,value[2],false);
    ledcontrol1.setChar(0,0,value[3],false);
  }
  else { //display normal IAS at right position
    ledcontrol1.setChar(0,2,' ',false);
    ledcontrol1.setChar(0,1,' ',false);
    ledcontrol1.setChar(0,0,' ',false);
    ledcontrol1.setChar(1,7,value[0],false);
    ledcontrol1.setChar(1,6,value[1],false);
    ledcontrol1.setChar(1,5,value[2],false); 
  }
 
 
}

void displayHeading(String value) {
  ledcontrol1.setChar(1,2,value[0],false);
  ledcontrol1.setChar(1,1,value[1],false);
  ledcontrol1.setChar(1,0,value[2],false);
}


Like the Website ?
Support Cockpitbuilders.com and Click Below to Donate