1
Dependencies: DebugLibrary MCP4xxxx_SPI mbed
Fork of MCP41xxxApp by
main.cpp
- Committer:
- Yann
- Date:
- 2013-01-27
- Revision:
- 1:a463807bf483
- Parent:
- 0:62020bd58748
- Child:
- 3:7dbac45b8fd3
File content as of revision 1:a463807bf483:
#include <string>
#include <iostream>
#include <iomanip>
#include "MCP4xxxx_SPI.h"
struct UserChoice {
char choice;
unsigned char moduleId;
};
/*
* Declare functions
*/
void AvailableIndicator(); // LED1 flashing for program while program is alive
UserChoice DisplayMenuAndGetChoice(); // Display and get the user choice
/*
* Declare statics
*/
DigitalOut g_availableLed(LED1); // To verify if program in running
Ticker g_available; // LED1 will flash with a period of 2s
DigitalOut g_chipSelect(p8); // /CS to select MCP4xxxx device
CMCP4xxxx_SPI g_digitalPot(p5, p6, p7, NC, NC); // Create an instance of the class CMCP4xxxx_SPI, p5/p6/p7: SPI#1, /RESET input not connected, , /SHDN input not connected
UserChoice g_userChoice; // Used to store user choice from displayed menu
int main() {
unsigned char potLevel = 0x80; // Initial digital potentiometer value
g_chipSelect = 1; // Device is disabled
// Launch available indicator
g_available.attach(&AvailableIndicator, 2.0);
while (true) {
g_userChoice = DisplayMenuAndGetChoice(); // Retrieve the user selection
switch (g_userChoice.choice) {
case 'a':
potLevel += 1;
g_chipSelect.write(0);
g_digitalPot.Write(CMCP4xxxx_SPI::WriteToPot1, potLevel);
g_chipSelect.write(1);
break;
case 'b':
potLevel += 1;
g_chipSelect.write(0);
g_digitalPot.Write(CMCP4xxxx_SPI::WriteToPot2, potLevel);
g_chipSelect.write(1);
break;
case 'c':
potLevel -= 1;
g_chipSelect.write(0);
g_digitalPot.Write(CMCP4xxxx_SPI::WriteToPot1, potLevel);
g_chipSelect.write(1);
break;
case 'd':
potLevel -= 1;
g_chipSelect.write(0);
g_digitalPot.Write(CMCP4xxxx_SPI::WriteToPot2, potLevel);
g_chipSelect.write(1);
break;
case 'e':
potLevel -= 1;
g_chipSelect.write(0);
g_digitalPot.Write(CMCP4xxxx_SPI::ShutdownPot1);
g_chipSelect.write(1);
break;
case 'f':
potLevel -= 1;
g_chipSelect.write(0);
g_digitalPot.Write(CMCP4xxxx_SPI::ShutdownPot2);
g_chipSelect.write(1);
break;
case 'r':
g_chipSelect.write(0);
g_digitalPot.Reset();
g_chipSelect.write(1);
break;
default:
std::cout << "Invalid user choice\r" << std::endl;
break;
} // End of 'switch' statement
} // End of 'while' statement
} // End of program - nerver reached
void AvailableIndicator() {
g_availableLed = !g_availableLed;
} // End of AvailableIndicator
UserChoice DisplayMenuAndGetChoice() {
static UserChoice userChoice;
// Display the title
std::cout << "\r" << std::endl << "MCP4xxxx_SPI v0.1\r" << std::endl;
// Display the menu
std::cout << "\tIncrease level on pot #1:\t\t\ta\r" << std::endl;
std::cout << "\tIncrease level on pot #2:\t\t\tb\r" << std::endl;
std::cout << "\tDecrease level on pot #1:\t\t\tc\r" << std::endl;
std::cout << "\tDecrease level on pot #2:\t\t\td\r" << std::endl;
std::cout << "\tShutdown pot #1 :\t\t\te\r" << std::endl;
std::cout << "\tShutdown pot #2 :\t\t\tf\r" << std::endl;
std::cout << "\tReset MCP4xxxx :\t\t\tr\r" << std::endl;
std::cout << "Enter your choice: " << std::flush;
userChoice.choice = getchar();
// Display the menu
std::cout << "\r" << std::endl << std::flush;
return userChoice;
}

