Soving knapsack problem

Dependencies:   mbed mbed-rtos

Revision:
0:1ea8993f6b18
Child:
1:39e36a5c11e0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Oct 20 03:54:08 2015 +0000
@@ -0,0 +1,227 @@
+#include "mbed.h"
+#include "rtos.h"
+
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+uint32_t button_pressed;
+Thread *thread2;
+Thread *thread3;
+Thread *thread4;
+Thread *thread5;
+Thread *thread6;
+
+osMutexDef (mutex_1);    // Declare mutex
+osMutexId  (mutex_1_id); // Mutex ID
+
+osMutexDef (mutex_2);    // Declare mutex
+osMutexId  (mutex_2_id); // Mutex ID
+
+osMessageQDef(message_q, 10, uint32_t); // Declare a message queue
+osMessageQId (message_q_id);           // Declare an ID for the message queue
+Serial pc(USBTX, USBRX);
+
+struct Problema
+{
+   int tid;
+   int pid;
+   int cap;
+   int weights[30];
+   int values[30];
+   int ts_begin;   
+   int ts_end;
+   int proc_time;
+}; 
+
+int printP(struct Problema p){
+    int i;
+    printf("\n--------------------\n");
+    printf("pid: %i\n", p.pid);
+    printf("tid: %i\n", 0);
+    printf("cap: %i\n", p.cap);
+    printf("W \t V\n");
+    i=0;
+    while (p.weights[i]>=0 && p.values[i]>=0){ 
+        printf("%i \t %i\n", p.weights[i],p.values[i]);
+        i++;
+    }
+    
+    return 1;
+}
+int my_atoi(char *str) {
+    int res = 0; // Initialize result
+    // Iterate through all characters of input string and update result
+    for (int i = 0; str[i] != '\0'; ++i)
+        res = res*10 + str[i] - '0';
+    return res;
+}
+
+void led_func(uint32_t n){
+    //mutex
+    uint32_t temp = ~((n%7)+1);
+    led3 = temp&(uint32_t)1;
+    led2 = (temp&(uint32_t)2)>>1;
+    led1 = (temp&(uint32_t)4)>>2;   
+}
+
+void led_thread(void const *argument)
+{
+    uint32_t cont =0;
+    while (true) {
+        led_func(cont);
+        Thread::wait((uint32_t)argument );
+        cont++;
+    }
+}
+
+void count_thread(void const *argument)
+{
+     while (true) {
+        osMutexWait(mutex_2_id, osWaitForever);
+        button_pressed++;
+        osMutexRelease(mutex_2_id);
+        
+        osMutexWait(mutex_1_id, osWaitForever);
+        osMessagePut(message_q_id, button_pressed, osWaitForever);
+        osMutexRelease(mutex_1_id);
+        Thread::wait(2000);
+    }
+}
+
+int main(){
+    
+    char buffer_counter=0;
+    char buffer[4];
+    char input_char;
+    buffer[0]='\0';
+    buffer[3]='\0';
+    char stage = 0;
+    int number_threads=0;
+    int i;
+    pc.baud (115200);
+    char parseFlag =1;
+    
+    struct  Problema problemas[10];
+    char problema_counter=0;
+    char item_counter=0;
+    
+    /*thread2 = new Thread(count_thread);
+    thread3 = new Thread(count_thread);
+    thread4 = new Thread(count_thread);
+    thread5 = new Thread(count_thread);
+    thread6 = new Thread(count_thread);
+    osEvent event;*/
+    
+    /*message_q_id = osMessageCreate(osMessageQ(message_q), NULL);
+    osEvent event = osMessageGet(message_q_id, osWaitForever);
+    
+    if (0== osMutexWait(mutex_1_id, 0))  {
+        printf(" %d \n\r", event.value.p);
+    }else{
+        printf("Else \n\r");
+    }
+    osMutexRelease(mutex_1_id);
+    */
+    
+    while (true) {
+        parseFlag=1;
+        while(parseFlag){        
+            input_char = getchar();
+            if (input_char=='&'){
+                buffer[0]='\0';
+                buffer_counter=0;
+                problema_counter=0;
+                item_counter=0;
+                number_threads=0;
+                stage = 1;
+                printf("Stage %i\n", stage);
+            }else if (input_char=='#'){    
+                buffer[0]='\0';
+                buffer_counter=0;
+                stage = 2;
+                printf("Stage %i\n", stage);
+            }else if (input_char >= '0' && input_char <= '9'){
+                buffer[buffer_counter++]=input_char;
+                buffer[buffer_counter]='\0';
+            }else if (input_char=='*'){    
+                if (stage==1){
+                    number_threads = my_atoi(&buffer[0]);
+                    printf("Number of threads = %i\n", number_threads);
+                    printf("Problema = %i\n", problema_counter);
+                    buffer[0]='\0';
+                    buffer_counter=0;
+                }else if (stage==4){
+                    problemas[problema_counter].values[item_counter++]=my_atoi(&buffer[0]);
+                    problema_counter++;
+                    item_counter=0;
+                    printf("4Problema = %i\n", problema_counter);
+                }else if (stage==5){
+                    problemas[problema_counter].values[item_counter++]=my_atoi(&buffer[0]);
+                    problemas[problema_counter].values[item_counter]=-5;
+                    problemas[problema_counter].weights[item_counter]=-5;
+                    problema_counter++;
+                    item_counter=0;
+                    printf("Problema = %i\n", problema_counter);
+                }  
+                
+            }else if (input_char==','){
+                if (stage==2){
+                    problemas[problema_counter].pid = my_atoi(&buffer[0]);
+                    stage=3;
+                    printf("Stage %i\n", stage);
+                }else if (stage==3){
+                    problemas[problema_counter].cap = my_atoi(&buffer[0]);
+                    stage=4;
+                    printf("Stage %i\n", stage);
+                } else if (stage==4){
+                    problemas[problema_counter].weights[item_counter]=my_atoi(&buffer[0]);
+                    stage=5;
+                    printf("Stage %i\n", stage);
+                }else if (stage==5){
+                    problemas[problema_counter].values[item_counter++]=my_atoi(&buffer[0]);
+                    stage=4;
+                    printf("Stage %i\n", stage);
+                }
+                buffer[0]='\0';
+                buffer_counter=0;
+            
+            }else if (input_char=='\n'){    
+                buffer[0]='\0';
+                buffer_counter=0;
+            }else if (input_char=='X'){    
+                buffer[0]='\0';
+                buffer_counter=0;
+                stage=0;
+                printf("\nNumber of threads = %i\n", number_threads);
+                for (i=0; i<problema_counter; i++) printP(problemas[i]);
+                parseFlag =0;
+            }
+        }
+        printf("\nNumber of threads = %i\n", number_threads);
+        printf("\nNumber of problems = %i\n", problema_counter);
+        printf("\n Finished \n");
+        Thread thread(led_thread, (void *)1000);
+        osDelay(6000);
+        thread.terminate();
+    }
+}
+
+//osStatus status;
+//mutex_1_id = osMutexCreate(osMutex(mutex_1));
+//osMutexWait(mutex_1_id, osWaitForever);
+//if (0== osMutexWait(mutex_1_id, 0))  {
+//}else{
+//  printf("Else \n\r");
+//}
+//osMutexRelease(mutex_1_id);
+//message_q_id = osMessageCreate(osMessageQ(message_q), NULL);
+//(mutex_1_id, osWaitForever);
+//osEvent event = osMessageGet(message_q_id, osWaitForever);
+//printf(" %d \n\r", event.value.p);
+//fflush(stdout);
+//osMutexRelease(mutex_1_id);
+//Thread::wait(100);
+//osMutexWait(mutex_2_id, 1000);
+//osMutexRelease(mutex_2_id);
+//printf("\n(%c)\n", input_char);
+//fflush(stdout);