Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <cstdlib>
- #include <windows.h> // Header file used for the 'sleep' function.
- #include <errno.h>
- #include <math.h>
- using namespace std;
- // Optional: your choice if you want to use these definitions for MAX-MIN-Range
- int open_ousb(char * command)
- {
- FILE * fpipe;
- char line[256];
- if (!(fpipe = (FILE*)_popen(command, "r")))
- {
- // Error exists if FPIPE returns NULL
- cout << "Z" << endl;
- return 0;
- }
- while (fgets(line, sizeof(line), fpipe))
- {
- cout << line << endl;
- }
- _pclose(fpipe);
- return (atoi(line)); // output current led states
- }
- int main(int argc, char *argv[])
- {//--- When no parameters MUST print student ID string in CSV format.
- int portb = 0;
- int Y = 0;
- char * command = "ousb -r io PORTB";
- if (argc == 1)
- {
- // no parameters on command line just the program name
- //--- edit string below: eg: "studentNumber,student_email,student_name"
- cout << "**********************************" << endl;
- //--- When run with just the program name, meaning without parameters
- // your code MUST print student ID string in CSV format.
- // i.e. simply running
- // the output MUST print student ID string in Comman Separated Version (CSV)
- // format, such as: (NOTE the two commas !)
- // "***********************"
- // eg: "*********************************"
- return(0); // The convention is to return Zero to signal NO ERRORS, if you change it the AutoTester will
- //assume you have made some major error. Leave it as zero.
- }
- else if (argc == 2 || argc == 3)
- { //***************************
- //READ
- cout << "value after reading" << endl;
- portb = open_ousb(command);//reading the current led state and storing it in port b variable
- int X = (int)atof(argv[1]);// convert float to int from atof
- if (argc == 2){
- Y = 1;// if 2 arguments, third arg is 1
- }
- else if (argc == 3){
- Y = (int)atof(argv[2]); // if 3 arguments, third argument is the forced integer value after converting string to a float.
- }
- //error check 3
- if (X > 255 || X < -255)
- {
- cout << "R1" << endl;
- return 0;
- }
- if (Y<1 || Y>65536){
- cout << "R2" << endl;
- return 0;
- }
- for (int i = 1; i <= Y; i++);
- {
- portb = portb + X; //update value to be written
- //error check 4
- if (portb>255)
- {
- portb = portb % 255;
- }
- else if (portb < 0)
- {
- portb = portb + 256;
- }
- char writecommand[100];//array to store write command
- //Write
- sprintf_s(writecommand, "ousb -r io PORTB %i", portb);
- cout << "after writing, the value is: " << endl;
- portb = open_ousb(writecommand);
- }
- }
- else {
- cout << "P" << endl;
- }
- return (0);
- }
Advertisement
Add Comment
Please, Sign In to add comment