J1939 Documentation
Version 4.16.00
Loading...
Searching...
No Matches
Joystick demo

This example demonstrates the implementation of a joystick. The following PGNs are used by this ECU example:

PGN Parameter Group Label Acronym Repetition Rate
64982 Basic joystick message 1 BJM1 100 ms

Implementation

The implementation is done in the file j1939_joystick1.c.

//====================================================================================================================//
// File: j1939_demo_joystick1.c //
// Description: Example code for joystick 1 (PGN 64982) //
// //
// Copyright (C) MicroControl GmbH & Co. KG //
// 53844 Troisdorf - Germany //
// www.microcontrol.net //
// //
//--------------------------------------------------------------------------------------------------------------------//
// The copyright to the computer program(s) herein is the property of MicroControl GmbH & Co. KG, Germany. The //
// program(s) may be used and/or copied only with the written permission of MicroControl GmbH & Co. KG or in //
// accordance with the terms and conditions stipulated in the agreement/contract under which the program(s) have //
// been supplied. //
// //
//====================================================================================================================//
/*--------------------------------------------------------------------------------------------------------------------*\
** Include files **
** **
\*--------------------------------------------------------------------------------------------------------------------*/
#include "j1939_joystick.h"
#include "j1939_msg.h"
#include "j1939_pgn.h"
#include "j1939_demo_app.h"
/*--------------------------------------------------------------------------------------------------------------------*\
** Function prototypes **
** **
\*--------------------------------------------------------------------------------------------------------------------*/
int16_t J1939_Pgn64982( uint8_t * pubDataV, uint16_t uwSizeV, uint8_t ubSrcAddrV, uint8_t ubNetV);
/*--------------------------------------------------------------------------------------------------------------------*\
** Variables **
** **
\*--------------------------------------------------------------------------------------------------------------------*/
static uint16_t uwAxis_X_ValueS;
static uint16_t uwAxis_X_Neutral_MinS;
static uint16_t uwAxis_X_Neutral_MaxS;
static uint16_t uwAxis_Y_ValueS;
static uint16_t uwAxis_Y_Neutral_MinS;
static uint16_t uwAxis_Y_Neutral_MaxS;
static uint8_t ubButtonGrp1S = J1939_UNUSED_UINT8; // buttons 1 .. 4
static uint8_t ubButtonGrp2S = J1939_UNUSED_UINT8; // buttons 5 .. 8
static uint8_t ubButtonGrp3S = J1939_UNUSED_UINT8; // buttons 9 .. 12
{ J1939_PRIORITY_CTRL | J1939_MSG_TRM, // PDU Control
(uint32_t) 64982, // PGN value
(uint16_t) 100, // Cycle
(uint16_t) 0, // Counter
(uint8_t) 0x00, // DA
(uint8_t) 0x00, // CAN Flags
(uint16_t) 0x0008, // Data size
J1939_Pgn64982 // PGN handler
},
// Last entry
{ (uint16_t) 0xFFFF, 0, 0, 0, 0, 0, 0, CPP_NULL }
};
/*--------------------------------------------------------------------------------------------------------------------*\
** Functions **
** **
\*--------------------------------------------------------------------------------------------------------------------*/
//--------------------------------------------------------------------------------------------------------------------//
// J1939_JoystickSetButton() //
// set value for Button N //
//--------------------------------------------------------------------------------------------------------------------//
void J1939_JoystickSetButton(uint8_t ubButtonV, uint8_t ubStateV)
{
uint8_t ubButtonMaskT;
uint8_t ubButtonValueT;
switch(ubButtonV)
{
case eJ1939_JOYSTICK_BUTTON_1:
ubButtonMaskT = (uint8_t) 0x3F;
ubStateV <<= 6;
ubButtonValueT = ubStateV;
ubButtonGrp1S = ubButtonGrp1S & ubButtonMaskT;
ubButtonGrp1S = ubButtonGrp1S | ubButtonValueT;
break;
case eJ1939_JOYSTICK_BUTTON_2:
ubButtonMaskT = (uint8_t) 0xCF;
ubStateV <<= 4;
ubButtonValueT = ubStateV;
ubButtonGrp1S = ubButtonGrp1S & ubButtonMaskT;
ubButtonGrp1S = ubButtonGrp1S | ubButtonValueT;
break;
case eJ1939_JOYSTICK_BUTTON_3:
ubButtonMaskT = (uint8_t) 0xF3;
ubStateV <<= 2;
ubButtonValueT = ubStateV;
ubButtonGrp1S = ubButtonGrp1S & ubButtonMaskT;
ubButtonGrp1S = ubButtonGrp1S | ubButtonValueT;
break;
case eJ1939_JOYSTICK_BUTTON_4:
ubButtonMaskT = (uint8_t) 0xFC;
ubButtonValueT = ubStateV;
ubButtonGrp1S = ubButtonGrp1S & ubButtonMaskT;
ubButtonGrp1S = ubButtonGrp1S | ubButtonValueT;
break;
case eJ1939_JOYSTICK_BUTTON_5:
ubButtonMaskT = (uint8_t) 0x3F;
ubStateV <<= 6;
ubButtonValueT = ubStateV;
ubButtonGrp2S = ubButtonGrp2S & ubButtonMaskT;
ubButtonGrp2S = ubButtonGrp2S | ubButtonValueT;
break;
case eJ1939_JOYSTICK_BUTTON_6:
ubButtonMaskT = (uint8_t) 0xCF;
ubStateV <<= 4;
ubButtonValueT = ubStateV;
ubButtonGrp2S = ubButtonGrp2S & ubButtonMaskT;
ubButtonGrp2S = ubButtonGrp2S | ubButtonValueT;
break;
case eJ1939_JOYSTICK_BUTTON_7:
ubButtonMaskT = (uint8_t) 0xF3;
ubStateV <<= 2;
ubButtonValueT = ubStateV;
ubButtonGrp2S = ubButtonGrp2S & ubButtonMaskT;
ubButtonGrp2S = ubButtonGrp2S | ubButtonValueT;
break;
case eJ1939_JOYSTICK_BUTTON_8:
ubButtonMaskT = (uint8_t) 0xFC;
ubButtonValueT = ubStateV;
ubButtonGrp2S = ubButtonGrp2S & ubButtonMaskT;
ubButtonGrp2S = ubButtonGrp2S | ubButtonValueT;
break;
case eJ1939_JOYSTICK_BUTTON_9:
ubButtonMaskT = (uint8_t) 0x3F;
ubStateV <<= 6;
ubButtonValueT = ubStateV;
ubButtonGrp3S = ubButtonGrp3S & ubButtonMaskT;
ubButtonGrp3S = ubButtonGrp3S | ubButtonValueT;
break;
case eJ1939_JOYSTICK_BUTTON_10:
ubButtonMaskT = (uint8_t) 0xCF;
ubStateV <<= 4;
ubButtonValueT = ubStateV;
ubButtonGrp3S = ubButtonGrp3S & ubButtonMaskT;
ubButtonGrp3S = ubButtonGrp3S | ubButtonValueT;
break;
case eJ1939_JOYSTICK_BUTTON_11:
ubButtonMaskT = (uint8_t) 0xF3;
ubStateV <<= 2;
ubButtonValueT = ubStateV;
ubButtonGrp3S = ubButtonGrp3S & ubButtonMaskT;
ubButtonGrp3S = ubButtonGrp3S | ubButtonValueT;
break;
case eJ1939_JOYSTICK_BUTTON_12:
ubButtonMaskT = (uint8_t) 0xFC;
ubButtonValueT = ubStateV;
ubButtonGrp3S = ubButtonGrp3S & ubButtonMaskT;
ubButtonGrp3S = ubButtonGrp3S | ubButtonValueT;
break;
default:
break;
}
}
//--------------------------------------------------------------------------------------------------------------------//
// J1939_JoystickSetNeutralRange() //
// set neutral range for Axis N //
//--------------------------------------------------------------------------------------------------------------------//
void J1939_JoystickSetNeutralRange( uint8_t ubAxisV, uint16_t uwMinValueV, uint16_t uwMaxValueV)
{
switch(ubAxisV)
{
case eJ1939_JOYSTICK_AXIS_X:
uwAxis_X_Neutral_MinS = uwMinValueV;
uwAxis_X_Neutral_MaxS = uwMaxValueV;
break;
case eJ1939_JOYSTICK_AXIS_Y:
uwAxis_Y_Neutral_MinS = uwMinValueV;
uwAxis_Y_Neutral_MaxS = uwMaxValueV;
break;
default:
break;
}
}
//--------------------------------------------------------------------------------------------------------------------//
// J1939_JoystickSetValue() //
// set value for Axis N //
//--------------------------------------------------------------------------------------------------------------------//
void J1939_JoystickSetValue(uint8_t ubAxisV, uint16_t uwValueV)
{
uint16_t uwAxisValueT = 0;
//---------------------------------------------------------------------------------------------------
// limit to 10 bits and move to correct position
//
if (uwValueV > (uint16_t) 1022)
{
uwValueV = (uint16_t) 1022; // this is the error indicator
}
uwAxisValueT = uwValueV & (uint16_t) 0x03FF;
uwAxisValueT <<= 6;
//---------------------------------------------------------------------------------------------------
// assume neutral position
//
uwAxisValueT = uwAxisValueT | (uint16_t) 0x0001;
switch(ubAxisV)
{
case eJ1939_JOYSTICK_AXIS_X:
//-----------------------------------------------------------------------------------
// check left position status
//
if(uwValueV < uwAxis_X_Neutral_MinS)
{
uwAxisValueT &= (uint16_t) 0xFFC0;
uwAxisValueT |= (uint16_t) 0x0004;
}
//-----------------------------------------------------------------------------------
// check right position status
//
if(uwValueV > uwAxis_X_Neutral_MaxS)
{
uwAxisValueT &= (uint16_t) 0xFFC0;
uwAxisValueT |= (uint16_t) 0x0010;
}
uwAxis_X_ValueS = uwAxisValueT;
break;
case eJ1939_JOYSTICK_AXIS_Y:
//-----------------------------------------------------------------------------------
// check left position status
//
if(uwValueV < uwAxis_Y_Neutral_MinS)
{
uwAxisValueT &= (uint16_t) 0xFFC0;
uwAxisValueT |= (uint16_t) 0x0004;
}
//-----------------------------------------------------------------------------------
// check right position status
//
if(uwValueV > uwAxis_Y_Neutral_MaxS)
{
uwAxisValueT &= (uint16_t) 0xFFC0;
uwAxisValueT |= (uint16_t) 0x0010;
}
uwAxis_Y_ValueS = uwAxisValueT;
break;
default:
break;
}
}
//--------------------------------------------------------------------------------------------------------------------//
// J1939_Pgn64982() //
// BJM1 (Basic Joystick Message 1) //
//--------------------------------------------------------------------------------------------------------------------//
int16_t J1939_Pgn64982(uint8_t * pubDataV, uint16_t uwSizeV, uint8_t ubSrcAddrV, uint8_t ubNetV)
{
J1939_Payload_ts *ptsPayloadT = (J1939_Payload_ts *) pubDataV;
//---------------------------------------------------------------------------------------------------
// silence warning for unused parameters
//
(void) uwSizeV;
(void) ubSrcAddrV;
(void) ubNetV;
//---------------------------------------------------------------------------------------------------
// copy data to PGN
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
// byte 0 .. 1 : X-Axis value
//
ptsPayloadT->aubByte[0] = (uint8_t) uwAxis_X_ValueS;
ptsPayloadT->aubByte[1] = (uint8_t) (uwAxis_X_ValueS >> 8);
//---------------------------------------------------------------------------------------------------
// byte 2 .. 3 : Y-Axis value
//
ptsPayloadT->aubByte[2] = (uint8_t) uwAxis_Y_ValueS;
ptsPayloadT->aubByte[3] = (uint8_t) (uwAxis_Y_ValueS >> 8);
ptsPayloadT->aubByte[4] = J1939_UNUSED_UINT8;
ptsPayloadT->aubByte[5] = ubButtonGrp1S; // Button 1 .. 4
ptsPayloadT->aubByte[6] = ubButtonGrp2S; // Button 5 .. 8
ptsPayloadT->aubByte[7] = ubButtonGrp3S; // Button 9 .. 12
return ((int16_t) eJ1939_HANDLER_RESULT_OK);
}
//--------------------------------------------------------------------------------------------------------------------//
// J1939_PgnListUserInit() //
// Setup data for the ECU //
//--------------------------------------------------------------------------------------------------------------------//
void J1939_PgnListUserInit(uint8_t ubNetV)
{
if (ubNetV == (uint8_t) eJ1939_NET_1)
{
uwAxis_X_ValueS = (uint16_t) 500;
uwAxis_X_Neutral_MinS = (uint16_t) 490;
uwAxis_X_Neutral_MaxS = (uint16_t) 510;
uwAxis_Y_ValueS = (uint16_t) 500;
uwAxis_Y_Neutral_MinS = (uint16_t) 490;
uwAxis_Y_Neutral_MaxS = (uint16_t) 510;
}
}
#define J1939_MSG_TRM
Definition j1939_defs.h:257
#define J1939_PRIORITY_CTRL
Definition j1939_defs.h:222
@ eJ1939_NET_1
Definition j1939_defs.h:117
@ eJ1939_HANDLER_RESULT_OK
Definition j1939_defs.h:515
J1939 demo application.
J1939_PGN_Entry_ts atsPgnListJoystick[]
J1939 Identifier calculation functions .
J1939 Parameter Groups .
void J1939_PgnListUserInit(uint8_t ubNetV)
Manage a single PGN.
Definition j1939_defs.h:572