// Lua: realfrequency = setup( idR,idG,idB,frequency) static int lpwmled_setup( lua_State* L ) { s32 freq; // signed, to error check for negative values int err; if((err=checkid(L,&(led.pwmidr),1))!=1) return err; if((err=checkid(L,&(led.pwmidg),2))!=1) return err; if((err=checkid(L,&(led.pwmidb),3))!=1) return err; freq = luaL_checkinteger( L, 4 ); if ( freq <= 0 ) return luaL_error( L, "wrong arg range" ); led.frequency=freq; if ( led.frequency > 0 ) led.frequency = platform_pwm_setup( led.pwmidr,led.frequency, 0 ); if ( led.frequency > 0 ) led.frequency = platform_pwm_setup( led.pwmidg,led.frequency, 0 ); if ( led.frequency > 0 ) led.frequency = platform_pwm_setup( led.pwmidb,led.frequency, 0 ); if(led.frequency==0) return luaL_error( L, "too many pwms." ); lua_pushinteger( L, led.frequency ); platform_pwm_start( led.pwmidr ); platform_pwm_start( led.pwmidg ); platform_pwm_start( led.pwmidb ); led.maxduty_r=1023; led.maxduty_g=1023; led.maxduty_b=1023; return 1; }
vector<int> exclusiveTime(int n, vector<string>& logs) { vector<int> ret(n,0); //func id as index, time is value stack<pair<int,int>> s; //func id, current execution time, when start, push, when end, pop for(int i=0;i<logs.size();i++){ int index = 0; int id = checkid(logs[i],index); int label = checklabel(logs[i],index); int t = checktime(logs[i],index); if(label==0){ s.push(make_pair(id,t)); }else{ pair<int,int> cur = s.top(); int last = t-cur.second+1; ret[cur.first]+=last; s.pop(); if (!s.empty()) { //to remove over count, not time for s.top().first id ret[s.top().first] -= last; } } } return ret; }
int getdata() { int t; gotoxy(20,3);printf("Enter the Information Below"); gotoxy(20,4);printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2"); gotoxy(20,5); printf("\xB2");gotoxy(53,5);printf("\xB2"); gotoxy(20,6); printf("\xB2");gotoxy(53,6);printf("\xB2"); gotoxy(20,7); printf("\xB2");gotoxy(53,7);printf("\xB2"); gotoxy(20,8); printf("\xB2");gotoxy(53,8);printf("\xB2"); gotoxy(20,9); printf("\xB2");gotoxy(53,9);printf("\xB2"); gotoxy(20,10); printf("\xB2");gotoxy(53,10);printf("\xB2"); gotoxy(20,11); printf("\xB2");gotoxy(53,11);printf("\xB2"); gotoxy(20,12); printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2"); gotoxy(21,6); printf("Book ID:\t"); gotoxy(30,6); scanf("%d",&t); if(checkid(t) == 0) { gotoxy(21,13); printf("\aThe book id already exists\a"); getch(); menu(); return 0; } add.book_id=t; fflush(stdin); gotoxy(21,7); printf("Book Name:"); fflush(stdin); gotoxy(31,7); gets(add.bookname); fflush(stdin); gotoxy(21,8); printf("Author:");gotoxy(28,8); fflush(stdin); gets(add.author); gotoxy(21,9); printf("Year:");gotoxy(28,9); scanf("%d",&add.year); fflush(stdin); gotoxy(21,10); printf("Quantity:");gotoxy(31,10); scanf("%d",&add.quantity); gotoxy(21,10); return 1; }
void editbooks() { /* Edit details of books like shelf, no. of copies, etc.*/ system("clear"); int c = 0, y, d, e; printf(" Edit Books\n"); while(1) { system("clear"); printf("Enter Book Id to be edited:"); scanf("%d", &d); fp = fopen("Library.dat", "rb+"); while(fread(&a, sizeof(a), 1, fp) == 1) { if(checkid(d) == 0) { printf("The book is availble\n"); printf("The Book ID:"); scanf("%d", &a.id); printf("Enter new name:"); scanf("%s", a.name); printf("Enter new Author:"); scanf("%s" ,a.Author); printf("Enter new quantity:"); scanf("%d", &a.quantity); printf("Enter new price:"); scanf("%f", &a.Price); printf("Enter new rackno:"); scanf("%d", &a.rackno); printf("The record is modified\n"); fseek(fp, ftell(fp) - sizeof(a), 0); fwrite(&a, sizeof(a), 1, fp); fclose(fp); c = 1; } if(c == 0) { printf("No record found\n"); } } printf("Modify another Record?(Y/N) pres 1 for YES and 2 for NO"); scanf("%d", &y); if(y == 2) { mainmenu(); } else { system("clear"); editbooks(); } } }
int getdata() { /* accept data to add book */ int t; printf("Enter the Information Below\n"); printf("Category:"); printf("%s",branch[s-1]); printf("\nBook ID:\t"); scanf("%d",&t); if(checkid(t) == 0) { printf("The book id checkid exists\t"); mainmenu(); } a.id = t; printf("\nBook Name:"); scanf("%s", a.name); printf("\nAuthor:"); scanf("%s", a.Author); printf("\nQuantity:"); scanf("%d", &a.quantity); printf("\nPrice:"); scanf("%f", &a.Price); printf("\nRack No:"); scanf("%d", &a.rackno); return 1; }
void issuebooks() { /* Issue perticular book to student also check for returntime which is 15 days */ time_t td = time(0); // get time now struct tm * now = localtime( & td ); //int dd1, mm1, yy1; a.issued.dd = now->tm_mday; a.issued.mm = now->tm_mon; a.issued.yy = now->tm_year; printf("\n\n******** %d %d %d\n\n",a.issued.dd, a.issued.mm, a.issued.yy); int t, ch, y; system("clear"); printf("BOOK ISSUE SECTION\n"); printf("1. Issue a Book\n"); printf("2. View Issued Book\n"); printf("3. Search Issued Book\n"); printf("4. Remove Issued Book\n"); printf("5. Retutn to mainmemu\n"); printf("Enter a Choice:\n"); scanf("%d", &ch); FILE *fs; switch(ch) { x1: case 1: { /* issue book*/ system("clear"); int c = 0,y; while(1) { system("clear"); printf(" Book Issue Section\n"); printf("Enter the Book Id:\n"); scanf("%d", &t); fp = fopen("Library.dat","rb+"); fs = fopen("Issue.txt","ab+"); if(checkid(t) == 0) { printf("The book record is available\n"); printf("There are %d unissued books in library\n", a.quantity); printf("The name of book is %s\n", a.name); printf("Enter student name:\n"); scanf("%s", a.stname); a.issued.dd = now->tm_mday; a.issued.mm = now->tm_mon; a.issued.yy = now->tm_year; printf("Issued date = %d - %d - %d\n", a.issued.dd, a.issued.mm, a.issued.yy); printf("The BOOK of ID %d is issued ", a.id); a.duedate.dd = a.issued.dd+RETURNTIME; a.duedate.mm = a.issued.mm; a.duedate.yy = a.issued.yy; if(a.duedate.dd > 30) { a.duedate.mm += a.duedate.dd / 30; a.duedate.dd -= 30; } if(a.duedate.mm > 12) { a.duedate.yy += a.duedate.mm / 12; a.duedate.mm -= 12; } printf("To be return:%d-%d-%d", a.duedate.dd, a.duedate.mm, a.duedate.yy); fseek(fs, sizeof(a), SEEK_END); fwrite(&a, sizeof(a), 1, fs); fclose(fs); c = 1; } if(c == 0) { printf("No record found\n"); } fclose(fp); printf("\nIssue any more(Y/N) press 1 for YES and 2 for NO:"); scanf("%d", &y); if(y == 1) goto x1; else issuebooks(); } break; } case 2: { /* view issued books */ system("clear"); int j = 4; printf("\tIssued book list\n"); fs = fopen("Issue.txt","rb"); while(fread(&a,sizeof(a),1,fs)==1) { printf("\n\nSTUDENT NAME: %s", a.stname); printf("\nCATEGORY : %s", a.cat); printf("\nID : %d", a.id); printf("\nBOOK NAME: %s", a.name); printf("\nISSUED DATE : %d-%d-%d", a.issued.dd, a.issued.mm, a.issued.yy ); printf("\nRETURN DATE : %d-%d-%d\n", a.duedate.dd, a.duedate.mm, a.duedate.yy); j++; } fclose(fs); printf("Press 1 to go issue books again:"); scanf("%d", &y); if(y == 1) issuebooks(); } break; x2: case 3: { /* Search for issued book */ system("clear"); int p, c = 0; printf("Enter Book ID:"); scanf("%d", &p); while(1) { fs = fopen("Issue.txt", "rb"); while(fread(&a, sizeof(a), 1, fs) == 1) { if(a.id == p) issuerecord(); c = 1; } fclose(fs); if(c == 0) printf("No Record Found"); printf("Try Another Search?(Y/N) press 1 for YES and 2 for NO:"); scanf("%d", &y); if(y == 1) goto x2; else issuebooks(); } } break; x3: case 4: { /* Remove issued book */ int y; system("clear"); int b; FILE *fg; while(1) { printf("Enter book id to remove:"); scanf("%d", &b); fs = fopen("Issue.txt", "rb+"); while(fread(&a, sizeof(a), 1, fs) == 1) { if(a.id == b) { issuerecord(); flag = 10; } if(flag == 10) { printf("Do You Want to Remove it?(Y/N) press 1 for YES and 2 for NO"); scanf("%d", &y); if(y == 1) { fg = fopen("record.txt", "wb+"); rewind(fs); while(fread(&a, sizeof(a), 1, fs) == 1) { if(a.id!=b) { fseek(fs, 0, SEEK_CUR); fwrite(&a, sizeof(a), 1, fg); } } fclose(fs); fclose(fg); remove("Issue.txt"); rename("record.dat", "Issue.txt"); printf("The issued book is removed from list\n"); printf("Delete any more?(Y/N) press 1 fo1r YES and 2 for NO"); scanf("%d", &y); if(y == 1) goto x3; else issuebooks(); } } if(flag!= 10) { printf("No Record Found\n"); printf("Press 1 to go issue books again:"); scanf("%d", &y); if(y == 1) issuebooks(); } } } } case 5: mainmenu(); break; default: printf("Wrong Entry!!\n"); return 0; issuebooks(); break; } }
void findelectrodes::find() { thresh = img.clone(); //tresholding image by using adaptive thresholding, 203 is the matrix size and has to be adapted if thresholding fails cv::adaptiveThreshold(img,thresh,255,CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY,203,0); //searching for contours in thresholded image, building hierachy of contours (outercontour -> innercontours) cv::findContours( thresh.clone() , contours, hierarchy,CV_RETR_TREE, CV_CHAIN_APPROX_NONE ); //checking every found contour... for( int i = 0; i< contours.size(); i++ ) { //counting innercountours from the left and from the right side int innercontours = -1; for(int next = hierarchy[i][2]; next >= 0; next = hierarchy[next][0]) { innercontours++; } for(int prev = hierarchy[i][2]; prev >= 0; prev = hierarchy[prev][1]) { innercontours++; } //checking if contour matches expected perimeter range and expected innercontour size, has to be adapted if image properties are changed extremely if( cv::arcLength(contours[i], true) > img.cols/4 && cv::arcLength(contours[i], true) < 30*img.cols && innercontours >= 7 && innercontours <= 24) { //checking for image contours which are large enough to be considered as an electrode marker int largeinnercontours = 0; for(int next = hierarchy[i][2]; next >= 0; next = hierarchy[next][0]) { if( (cv::contourArea( contours[next], false) >= cv::contourArea( contours[i], false)/200) && (cv::contourArea( contours[next], false) <= cv::contourArea( contours[i], false)/20) ) { largeinnercontours++; } } for(int prev = hierarchy[ hierarchy[i][2] ][1]; prev >= 0; prev = hierarchy[prev][1]) { if( (cv::contourArea( contours[prev], false) >= cv::contourArea( contours[i], false)/200) && (cv::contourArea( contours[prev], false) <= cv::contourArea( contours[i], false)/20) ) { largeinnercontours++; } } if(largeinnercontours >= 7 && largeinnercontours <= 24) { //saving contour id in list for further analysis std::cout << "Contour-Id: " << i << " Innercontours:" << innercontours << " Largeinnercontours:" << largeinnercontours << std::endl; stripes.push_back(i); } } } for(int i = 0; i < stripes.size(); i++) //analysing every found stripe { //checking for sizes of inner countous, if size is in the range of electrode marker, calculate centroid of marker cv::Moments moment; std::vector<cv::Point2f> centroids; std::vector<double> centroidPerimeter; std::vector<int> centroidContour; for(int next = hierarchy[stripes[i]][2]; next >= 0; next = hierarchy[next][0]) { if( (cv::contourArea( contours[next], false) >= cv::contourArea( contours[stripes[i]], false)/300) && (cv::contourArea( contours[next], false) <= cv::contourArea( contours[stripes[i]], false)/20) ) { moment = cv::moments(contours[next], false); centroids.push_back( cv::Point2f(moment.m10/moment.m00 ,moment.m01/moment.m00)); centroidPerimeter.push_back( cv::arcLength( contours[next], true)); centroidContour.push_back( next); } } for(int prev = hierarchy[ hierarchy[stripes[i]][2] ][1]; prev >= 0; prev = hierarchy[prev][1]) { if( (cv::contourArea( contours[prev], false) >= cv::contourArea( contours[stripes[i]], false)/300) && (cv::contourArea( contours[prev], false) <= cv::contourArea( contours[stripes[i]], false)/20) ) { moment = cv::moments(contours[prev], false); centroids.push_back( cv::Point2f(moment.m10/moment.m00 ,moment.m01/moment.m00)); centroidPerimeter.push_back( cv::arcLength( contours[prev], true)); centroidContour.push_back( prev); } } //if two centroids are too close, thresholding for marker might be wrong and both centroids have to be joined for( int j=0; j < centroids.size(); j++) { for( int k = j+1; k < centroids.size(); k++) { double dist = sqrt( (centroids[j].x - centroids[k].x)*(centroids[j].x - centroids[k].x) + (centroids[j].y - centroids[k].y)*(centroids[j].y - centroids[k].y) ); double averagePerimeter = ( cv::arcLength( contours[centroidContour[j]], true) + cv::arcLength( contours[centroidContour[k]], true))/2; if( dist <= averagePerimeter/2 //&& additional requirement removed: after long thinking, does not make sense why rectangular markers should not be merged and rhombic markers not //cv::minAreaRect(contours[centroidContour[j]]).size.area() <= 1.7 * cv::contourArea(contours[centroidContour[j]], false) && //cv::minAreaRect(contours[centroidContour[k]]).size.area() <= 1.7 * cv::contourArea(contours[centroidContour[k]], false) ) { centroids[j].x = (centroids[j].x + centroids[k].x)/2; centroids[j].y = (centroids[j].y + centroids[k].y)/2; centroidPerimeter[j] += centroidPerimeter[k]; centroids[k].x = -1; centroids[k].y = -1; } } } //delete second marker of two joined markers (joined marker is written to first marker position) for( int j=0; j < centroids.size(); j++) { if( centroids[j].x == -1 ) { centroids.erase(centroids.begin()+j); centroidPerimeter.erase(centroidPerimeter.begin()+j); j--; } } sortCentroids(centroids); //sort found centroids by x or y coordinates std::cout << centroids << std::endl; std::cout << "\nPotentieller Streifen: "; if(centroids.size() <= 12 && centroids.size() > 0) //stripe size is not allowed to be bigger than 12 or smaller than 1 { //getting subpixel information for found centroids cv::cornerSubPix(img, centroids, cvSize(4,4), cvSize(1,1) ,cvTermCriteria ( CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,100,0.001 )); std::vector<int> id; for( int j=0; j < centroids.size(); j++) { //for every centroid: calculating connection vector of actual and next centroid //scaling the vector to the half of the diagonal length of one of the four rectangles which are belonging to an elctrode marker //rotating the vector in four directions which are roughly pointing to the diagonal of each reactangle //checking for colors of calculated points for getting bit of electrode marker //adding bit to id vector: '0','1' are set for bit values, '-2' is set for errors (bit not decoded) cv::Point2f direction; cv::Point2f rot1; cv::Point2f rot2; cv::Point2f rot3; cv::Point2f rot4; if( j!= (centroids.size()-1) ) { direction.x = centroids[j+1].x - centroids[j].x; direction.y = centroids[j+1].y - centroids[j].y; } else { direction.x = centroids[j].x - centroids[j-1].x; direction.y = centroids[j].y - centroids[j-1].y; } double length = sqrt( direction.x * direction.x + direction.y * direction.y ); direction.x = 0.09 * centroidPerimeter[j] * direction.x/length; direction.y = 0.09 * centroidPerimeter[j] * direction.y/length; rot1.x = ( 0.94 * direction.x - 0.34 * direction.y ); //20° rot1.y = ( 0.34 * direction.x + 0.94 * direction.y ); rot2.x = ( -0.94 * direction.x - 0.34 * direction.y ); rot2.y = ( 0.34 * direction.x - 0.94 * direction.y ); rot3.x = ( -0.94 * direction.x + 0.34 * direction.y ); rot3.y = ( -0.34 * direction.x - 0.94 * direction.y ); rot4.x = ( 0.94 * direction.x + 0.34 * direction.y ); rot4.y = ( -0.34 * direction.x + 0.94 * direction.y ); if( thresh.at<uchar>(centroids[j].y + rot1.y, centroids[j].x + rot1.x) == 255 && thresh.at<uchar>(centroids[j].y + rot2.y, centroids[j].x + rot2.x) == 0 && thresh.at<uchar>(centroids[j].y + rot3.y, centroids[j].x + rot3.x) == 255 && thresh.at<uchar>(centroids[j].y + rot4.y, centroids[j].x + rot4.x) == 0 ) { id.push_back(1); std::cout << 1; } else if( thresh.at<uchar>(centroids[j].y + rot1.y, centroids[j].x + rot1.x) == 0 && thresh.at<uchar>(centroids[j].y + rot2.y, centroids[j].x + rot2.x) == 255 && thresh.at<uchar>(centroids[j].y + rot3.y, centroids[j].x + rot3.x) == 0 && thresh.at<uchar>(centroids[j].y + rot4.y, centroids[j].x + rot4.x) == 255 ) { id.push_back(0); std::cout << 0; } else if( thresh.at<uchar>(centroids[j].y + 0.8 * rot1.y, centroids[j].x + 0.8 * rot1.x) == 255 && thresh.at<uchar>(centroids[j].y + 0.8 *rot2.y, centroids[j].x + 0.8 * rot2.x) == 0 && thresh.at<uchar>(centroids[j].y + 0.8 *rot3.y, centroids[j].x + 0.8 * rot3.x) == 255 && thresh.at<uchar>(centroids[j].y + 0.8 *rot4.y, centroids[j].x + 0.8 * rot4.x) == 0 ) { id.push_back(1); std::cout << 1; } else if( thresh.at<uchar>(centroids[j].y + 0.8 *rot1.y, centroids[j].x + 0.8 * rot1.x) == 0 && thresh.at<uchar>(centroids[j].y + 0.8 *rot2.y, centroids[j].x + 0.8 * rot2.x) == 255 && thresh.at<uchar>(centroids[j].y + 0.8 *rot3.y, centroids[j].x + 0.8 * rot3.x) == 0 && thresh.at<uchar>(centroids[j].y + 0.8 *rot4.y, centroids[j].x + 0.8 * rot4.x) == 255 ) { id.push_back(0); std::cout << 0; } else { id.push_back(-2); std::cout << -2; } /*cv::circle(thresh, centroids[j] + rot1 , 2, cv::Scalar(0,0,0), 1); cv::circle(thresh, centroids[j] + rot2 , 2, cv::Scalar(0,0,0), 1); cv::circle(thresh, centroids[j] + rot3 , 2, cv::Scalar(0,0,0), 1); cv::circle(thresh, centroids[j] + rot4 , 2, cv::Scalar(0,0,0), 1);*/ } std::cout << std::endl << std::endl; checkid(id, centroids); } centroids.clear(); } }
void editbookrecords() { system("cls"); int c=0; int d,e; gotoxy(20,4); printf("****Edit Books Section****"); char another='y'; while(another=='y') { system("cls"); gotoxy(20,2); printf("Enter Book ID to be edited:"); scanf("%d",&d); fp=fopen("quizdata.txt","rb+"); while(fread(&add,sizeof(add),1,fp)==1) { if(checkid(d)==0) { gotoxy(25,4); printf("The book is AVAILABLE."); gotoxy(25,6); printf("The Book ID: %d",add.book_id); gotoxy(1,8); printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2"); gotoxy(1,9); printf("\xB2 Current name: ");puts(add.bookname);gotoxy(38,9);printf("\xB2"); gotoxy(1,10); printf("\xB2 Current Author:");puts(add.author);gotoxy(38,10);printf("\xB2"); gotoxy(1,11); printf("\xB2 Current year:");printf("%d",add.year);gotoxy(38,11);printf("\xB2"); gotoxy(1,12); printf("\xB2 Current quantity:");printf("%d",add.quantity);gotoxy(38,12);printf("\xB2"); gotoxy(1,13); printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2"); fflush(stdin); gotoxy(40,8); printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2"); gotoxy(40,9);printf("\xB2"); gotoxy(40,10);printf("\xB2"); gotoxy(40,11);printf("\xB2"); gotoxy(40,12);printf("\xB2"); gotoxy(78,9);printf("\xB2"); gotoxy(78,10);printf("\xB2"); gotoxy(78,11);printf("\xB2"); gotoxy(78,12);printf("\xB2"); gotoxy(40,13); printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2"); gotoxy(40,9); printf("\xB2 Enter new name:");gets(add.bookname); gotoxy(40,10); printf("\xB2 Enter new Author:");gets(add.author); gotoxy(40,11); printf("\xB2 Enter new year:");scanf("%d",&add.year); gotoxy(40,12); printf("\xB2 Enter new quantity:");scanf("%d",&add.quantity); gotoxy(22,17); printf("The record is modified"); fseek(fp,-sizeof(add),1); fwrite(&add,sizeof(add),1,fp); fclose(fp); c=1; } if(c==0) { gotoxy(15,9); printf("No record found"); } } gotoxy(22,20); printf("Modify another Record?(Y/N)"); fflush(stdin); gotoxy(50,20); another=getch() ; } returnfunc(); }