Guest User

Untitled

a guest
May 16th, 2016
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <windows.h> // Header file used for the 'sleep' function.
  5. #include <errno.h>
  6. #include <math.h>
  7.  
  8. using namespace std;
  9.  
  10. // Optional: your choice if you want to use these definitions for MAX-MIN-Range
  11. int open_ousb(char * command)
  12. {
  13. FILE * fpipe;
  14. char line[256];
  15.  
  16. if (!(fpipe = (FILE*)_popen(command, "r")))
  17. {
  18. // Error exists if FPIPE returns NULL
  19. cout << "Z" << endl;
  20.  
  21. return 0;
  22. }
  23. while (fgets(line, sizeof(line), fpipe))
  24. {
  25. cout << line << endl;
  26. }
  27. _pclose(fpipe);
  28.  
  29. return (atoi(line)); // output current led states
  30. }
  31.  
  32.  
  33. int main(int argc, char *argv[])
  34. {//--- When no parameters MUST print student ID string in CSV format.
  35. int portb = 0;
  36. int Y = 0;
  37. char * command = "ousb -r io PORTB";
  38. if (argc == 1)
  39. {
  40. // no parameters on command line just the program name
  41. //--- edit string below: eg: "studentNumber,student_email,student_name"
  42.  
  43. cout << "**********************************" << endl;
  44.  
  45. //--- When run with just the program name, meaning without parameters
  46. // your code MUST print student ID string in CSV format.
  47. // i.e. simply running
  48. // the output MUST print student ID string in Comman Separated Version (CSV)
  49. // format, such as: (NOTE the two commas !)
  50. // "***********************"
  51. // eg: "*********************************"
  52.  
  53. return(0); // The convention is to return Zero to signal NO ERRORS, if you change it the AutoTester will
  54. //assume you have made some major error. Leave it as zero.
  55. }
  56.  
  57.  
  58.  
  59. else if (argc == 2 || argc == 3)
  60. { //***************************
  61.  
  62. //READ
  63. cout << "value after reading" << endl;
  64. portb = open_ousb(command);//reading the current led state and storing it in port b variable
  65.  
  66. int X = (int)atof(argv[1]);// convert float to int from atof
  67. if (argc == 2){
  68. Y = 1;// if 2 arguments, third arg is 1
  69. }
  70. else if (argc == 3){
  71. Y = (int)atof(argv[2]); // if 3 arguments, third argument is the forced integer value after converting string to a float.
  72. }
  73. //error check 3
  74. if (X > 255 || X < -255)
  75. {
  76. cout << "R1" << endl;
  77.  
  78. return 0;
  79. }
  80. if (Y<1 || Y>65536){
  81. cout << "R2" << endl;
  82.  
  83. return 0;
  84. }
  85. for (int i = 1; i <= Y; i++);
  86. {
  87. portb = portb + X; //update value to be written
  88. //error check 4
  89. if (portb>255)
  90. {
  91. portb = portb % 255;
  92. }
  93.  
  94. else if (portb < 0)
  95. {
  96. portb = portb + 256;
  97. }
  98. char writecommand[100];//array to store write command
  99. //Write
  100. sprintf_s(writecommand, "ousb -r io PORTB %i", portb);
  101. cout << "after writing, the value is: " << endl;
  102. portb = open_ousb(writecommand);
  103. }
  104. }
  105. else {
  106. cout << "P" << endl;
  107. }
  108. return (0);
  109. }
Advertisement
Add Comment
Please, Sign In to add comment