Alternate NXC API
Posted: 06 Mar 2011, 22:14
Hello all,
When I started coding some small programs for the NXT in summer 2010 I always had a look in the great online manual of John's NXC. Some times I had to look a lot because I always forgot the names of the interface functions and / or constants.
These days I started again with a small program and I thought - the API seems to me - well - growed. But I am missing a system in the naming - sorry John. I do not know all of the history...
Today I started an "alternate API". This is nothing magic - there are mostly the same constants and functions just with another naming. Things work exactly as the original API functions because this is just a header file. So of course the original API still works with it.
Here I use "Out" or "OUT_" as prefix for the module API, "Set" or "SET_" as prefix for functions which are setting / writing data, "Rst" or "RST_" for functions which are resetting data and "Get" or "GET_" as prefix for functions which are getting / reading data. There are some other naming conventions which should be clear by reading the file. For making it really complete I have to add some (doxygen style) comments - perhaps Johns originals changed to the new API names - if he allows me to use them.
If anybody is interested in this I can work further on this and all of you are invited to work with me If not then I know I only do it for me.
So here we are, the first module is the output module:
Bye - marvin
When I started coding some small programs for the NXT in summer 2010 I always had a look in the great online manual of John's NXC. Some times I had to look a lot because I always forgot the names of the interface functions and / or constants.
These days I started again with a small program and I thought - the API seems to me - well - growed. But I am missing a system in the naming - sorry John. I do not know all of the history...
Today I started an "alternate API". This is nothing magic - there are mostly the same constants and functions just with another naming. Things work exactly as the original API functions because this is just a header file. So of course the original API still works with it.
Here I use "Out" or "OUT_" as prefix for the module API, "Set" or "SET_" as prefix for functions which are setting / writing data, "Rst" or "RST_" for functions which are resetting data and "Get" or "GET_" as prefix for functions which are getting / reading data. There are some other naming conventions which should be clear by reading the file. For making it really complete I have to add some (doxygen style) comments - perhaps Johns originals changed to the new API names - if he allows me to use them.
If anybody is interested in this I can work further on this and all of you are invited to work with me If not then I know I only do it for me.
So here we are, the first module is the output module:
Code: Select all
// **************************************************************************
// * Filename: output.h *
// * *
// * Harmonized output module API for NXC *
// * *
// * Prefix used: Out / OUT *
// * *
// * (C) 2011 Matthias Bunte *
// **************************************************************************
#ifndef _OUTPUT_H_
#define _OUTPUT_H_
// **********************
// * Internal constants *
// **********************
#define OUT_INTERN_WAIT_FOR_UD (1)
// *************
// * Constants *
// *************
// Outputs
#define OUT_PORT_A (OUT_A)
#define OUT_PORT_B (OUT_B)
#define OUT_PORT_C (OUT_C)
#define OUT_PORT_AB (OUT_AB)
#define OUT_PORT_AC (OUT_AC)
#define OUT_PORT_BC (OUT_BC)
#define OUT_PORT_ABC (OUT_ABC)
// Update flags
#define OUT_UF_MODE (UF_UPDATE_MODE)
#define OUT_UF_PWR (UF_UPDATE_SPEED)
#define OUT_UF_PID_VALUES (UF_UPDATE_PID_VALUES)
#define OUT_UF_RST_CNT_TACHO (UF_UPDATE_RESET_COUNT)
#define OUT_UF_RST_CNT_BLOCK_TACHO (UF_UPDATE_RESET_BLOCK_COUNT)
#define OUT_UF_RST_CNT_ROTATION (UF_UPDATE_RESET_ROTATION_COUNT)
#define OUT_UF_TACHO_LIMIT (UF_UPDATE_TACHO_LIMIT)
#define OUT_UF_PENDING_UPDATES (UF_PENDING_UPDATES)
// Modes (already defined in the same philosophy)
/*
#define OUT_MODE_COAST
#define OUT_MODE_MOTORON
#define OUT_MODE_BRAKE
#define OUT_MODE_REGULATED
#define OUT_MODE_REGMETHOD
*/
// Runstates (already defined in the same philosophy)
/*
#define OUT_RUNSTATE_IDLE
#define OUT_RUNSTATE_RAMPUP
#define OUT_RUNSTATE_RUNNING
#define OUT_RUNSTATE_RAMPDOWN
#define OUT_RUNSTATE_HOLD
*/
// Regulation modes (already defined in the same philosophy)
/*
#define OUT_REGMODE_IDLE
#define OUT_REGMODE_SPEED
#define OUT_REGMODE_SYNC
*/
// PID
#define OUT_PID_0 (PID_0)
#define OUT_PID_1 (PID_1)
#define OUT_PID_2 (PID_2)
#define OUT_PID_3 (PID_3)
#define OUT_PID_4 (PID_4)
#define OUT_PID_5 (PID_5)
#define OUT_PID_6 (PID_6)
#define OUT_PID_7 (PID_7)
// Counter flags
#define OUT_CF_RST_NONE (RESET_NONE)
#define OUT_CF_RST_CNT_TACHO (RESET_COUNT)
#define OUT_CF_RST_CNT_BLOCK (RESET_BLOCK_COUNT)
#define OUT_CF_RST_CNT_ROTATION (RESET_ROTATION_COUNT)
#define OUT_CF_RST_CNT_BLOCKANDTACHO (RESET_BLOCKANDTACHO)
#define OUT_CF_RST_CNT_ALL (RESET_ALL)
// Options (already defined in the same philosophy)
/*
#define OUT_OPTION_HOLDATLIMIT
#define OUT_OPTION_RAMPDOWNTOLIMIT
*/
// Output field constants
#define OUT_IOFIELD_UPDATE_FLAGS (UpdateFlagsField)
#define OUT_IOFIELD_MODE (OutputModeField)
#define OUT_IOFIELD_RUN_STATE (RunStateField)
#define OUT_IOFIELD_PWR (PowerField)
#define OUT_IOFIELD_ACTUAL_SPEED (ActualSpeedField)
#define OUT_IOFIELD_REG_MODE (RegModeField)
#define OUT_IOFIELD_TURN_RATIO (TurnRatioField)
#define OUT_IOFIELD_OVERLOAD (OverloadField)
#define OUT_IOFIELD_REG_P_VALUE (RegPValueField)
#define OUT_IOFIELD_REG_I_VALUE (RegIValueField)
#define OUT_IOFIELD_REG_D_VALUE (RegDValueField)
#define OUT_IOFIELD_CNT_TACHO (TachoCountField)
#define OUT_IOFIELD_CNT_BLOCK_TACHO (BlockTachoCountField)
#define OUT_IOFIELD_CNT_ROTATION (RotationCountField)
#define OUT_IOFIELD_CNT_TACHO_LIMIT (TachoLimitField)
#define OUT_IOFIELD_OPTIONS (OutputOptionsField)
// *************
// * Functions *
// *************
// "The" functions to control the output ports...
#define OutSetIOField SetOutput
#define OutGetIOField GetOutput
// Functions for getting output port values
// Get modes / states
#define OutGetMode(oopz) MotorMode(oopz)
#define OutGetRegMode(oopz) MotorRegulation(oopz)
#define OutGetRunState(oopz) MotorRunState(oopz)
// Get speed values
#define OutGetPwr(oopz) MotorPower(oopz)
#define OutGetActualSpeed(oopz) MotorActualSpeed(oopz)
#define OutGetOverload(oopz) MotorOverload(oopz)
// Get regulation values
#define OutGetRegTurnRatio(oopz) MotorTurnRatio(oopz)
#define OutGetRegPValue(oopz) MotorRegValue(oopz)
#define OutGetRegIValue(oopz) MotorRegValue(oopz)
#define OutGetRegDValue(oopz) MotorRegValue(oopz)
// Get counter values
#define OutGetCntTacho(oopz) MotorTachoCount(oopz)
#define OutGetCntBlockTacho(oopz) MotorBlockTachoCount(oopz)
#define OutGetCntRotation(oopz) MotorRotationCount(oopz)
#define OutGetCntTachoLimit(oopz) MotorTachoLimit(oopz)
// Functions for setting output port values
// Stop the motors
#define OutSetCoast(oopz) Coast(oopz)
#define OutSetBrake(oopz) Off(oopz)
// Helper macros for those who really need fwd and rev...
#define OutPwrFwd(opwrz) (opwrz)
#define OutPwrRev(opwrz) (-(opwrz))
// Run motors endless
#define OutSetPwr(oopz, opwrz) OnFwd(oopz, opwrz)
#define OutSetPwrRstCnt(oopz, opwrz, orcz) OnFwdEx(oopz, opwrz, orcz)
// Run motors endless regulated
#define OutSetPwrReg(oopz, opwrz, ormz) OnFwdReg(oopz, opwrz, ormz)
#define OutSetPwrRegPID(oopz, opwrz, ormz, opz, oiz, odz) \
OnFwdRegPID(oopz, opwrz, ormz, opz, oiz, odz)
#define OutSetPwrRegRstCnt(oopz, opwrz, ormz, orcz) OnFwdRegEx(oopz, opwrz, ormz, orcz)
#define OutSetPwrRegRstCntPID(oopz, opwrz, ormz, orcz, opz, oiz, odz) \
OnFwdRegExPID(oopz, opwrz, ormz, orcz, opz, oiz, odz)
// Run motors endless synchronized
#define OutSetPwrSync(oopz, opwrz, otz) OnFwdSync(oopz, opwrz, otz)
#define OutSetPwrSyncPID(oopz, opwrz, otz, opz, oiz, odz) \
OnFwdSyncPID(oopz, opwrz, otz, opz, oiz, odz)
#define OutSetPwrSyncRstCnt(oopz, opwrz, otz, orcz) OnFwdSyncEx(oopz, opwrz, otz, orcz)
#define OutSetPwrSyncRstCntPID(oopz, opwrz, otz, orcz, opz, oiz, odz) \
OnFwdSyncExPID(oopz, opwrz, otz, orcz, opz, oiz, odz)
// Turn motor for the amount of an angle (How to explain this shorter?)
#define OutSetAngle(oopz, opwrz, oaz) \
{ Wait(OUT_INTERN_WAIT_FOR_UD); RotateMotor(oopz, opwrz, oaz); }
#define OutSetAngleBrake(oopz, opwrz, oaz, obz) \
{ Wait(OUT_INTERN_WAIT_FOR_UD); RotateMotorEx(oopz, opwrz, oaz, 0, FALSE, obz); }
#define OutSetAngleBrakePID(oopz, opwrz, oaz, obz, opz, oiz, odz) \
{ Wait(OUT_INTERN_WAIT_FOR_UD); RotateMotorPIDEx(oopz, opwrz, oaz, 0, FALSE, obz, opz, oiz, odz); }
// Turn motors synchronized for the amount of an angle (How to explain this shorter?)
#define OutSetAngleSync(oopz, opwrz, oaz, otz) \
{ Wait(OUT_INTERN_WAIT_FOR_UD); RotateMotorEx(oopz, opwrz, oaz, otz, TRUE, TRUE); }
#define OutSetAngleSyncBrake(oopz, opwrz, oaz, otz, obz) \
{ Wait(OUT_INTERN_WAIT_FOR_UD); RotateMotorEx(oopz, opwrz, oaz, otz, TRUE, obz); }
#define OutSetAngleSyncBrakePID(oopz, opwrz, oaz, otz, TRUE, obz, opz, oiz, odz) \
{ Wait(OUT_INTERN_WAIT_FOR_UD); RotateMotorPIDEx(oopz, opwrz, oaz, otz, TRUE, obz, opz, oiz, odz); }
// Functions for resetting counter values
#define OutRstCntSetCoast(oopz, ocntz) CoastEx(oopz, ocntz) // Switches power off, too!!!
#define OutRstCntTacho(oopz) ResetTachoCount(oopz)
#define OutRstCntBlockTacho(oopz) ResetBlockTachoCount(oopz)
#define OutRstCntRotation(oopz) ResetRotationCount(oopz)
#define OutRstCntAll(oopz) ResetAllTachoCounts(oopz)
#endif // _OUTPUT_H_