Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of test_patient_structure by
patients.h@0:c435f76658b2, 2017-11-02 (annotated)
- Committer:
- rossatmsoe
- Date:
- Thu Nov 02 18:31:39 2017 +0000
- Revision:
- 0:c435f76658b2
- Child:
- 1:060801821b1c
Demonstration of the use of structures in C. Inspiration to learn object-oriented programming for a more elegant solution.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rossatmsoe | 0:c435f76658b2 | 1 | // Some functions use the serial monitor so define it here. |
rossatmsoe | 0:c435f76658b2 | 2 | Serial pc(USBTX,USBRX); |
rossatmsoe | 0:c435f76658b2 | 3 | |
rossatmsoe | 0:c435f76658b2 | 4 | // The patient_record structure stores name and vitals for a patient. |
rossatmsoe | 0:c435f76658b2 | 5 | struct patient_record { |
rossatmsoe | 0:c435f76658b2 | 6 | char firstname[30]; |
rossatmsoe | 0:c435f76658b2 | 7 | char lastname[30]; |
rossatmsoe | 0:c435f76658b2 | 8 | int systolic; |
rossatmsoe | 0:c435f76658b2 | 9 | int diastolic; |
rossatmsoe | 0:c435f76658b2 | 10 | float spO2; |
rossatmsoe | 0:c435f76658b2 | 11 | }; |
rossatmsoe | 0:c435f76658b2 | 12 | |
rossatmsoe | 0:c435f76658b2 | 13 | // It is assumed that we will have an array of these patient records. |
rossatmsoe | 0:c435f76658b2 | 14 | // We define the maximum number of patients that we will support. |
rossatmsoe | 0:c435f76658b2 | 15 | #define MAX_PATIENTS 100 |
rossatmsoe | 0:c435f76658b2 | 16 | |
rossatmsoe | 0:c435f76658b2 | 17 | //Print the values in the structure for a particular patient. |
rossatmsoe | 0:c435f76658b2 | 18 | void print_patient_record(struct patient_record *first_patient, int N) { |
rossatmsoe | 0:c435f76658b2 | 19 | pc.printf("%s %s \t ID: %d\n",first_patient[N].firstname, first_patient[N].lastname,N); |
rossatmsoe | 0:c435f76658b2 | 20 | pc.printf("BP: %d/%d SpO2: %f\n",first_patient[N].systolic, first_patient[N].diastolic, first_patient[N].spO2); |
rossatmsoe | 0:c435f76658b2 | 21 | } |
rossatmsoe | 0:c435f76658b2 | 22 | |
rossatmsoe | 0:c435f76658b2 | 23 | //Put patient into record. |
rossatmsoe | 0:c435f76658b2 | 24 | void newPatient(struct patient_record *patient, char *first, char*last) |
rossatmsoe | 0:c435f76658b2 | 25 | { |
rossatmsoe | 0:c435f76658b2 | 26 | // We can't just set the firstname pointer equal to the |
rossatmsoe | 0:c435f76658b2 | 27 | // first pointer; that doesn't actually move the information into |
rossatmsoe | 0:c435f76658b2 | 28 | // the structure. We need to copy each letter of the patient's name |
rossatmsoe | 0:c435f76658b2 | 29 | // into the structure. strcpy does this. |
rossatmsoe | 0:c435f76658b2 | 30 | strcpy(patient->firstname, first); |
rossatmsoe | 0:c435f76658b2 | 31 | strcpy(patient->lastname,last); |
rossatmsoe | 0:c435f76658b2 | 32 | } |
rossatmsoe | 0:c435f76658b2 | 33 | |
rossatmsoe | 0:c435f76658b2 | 34 | //Fill in vital signs directly. Note that we use -> when we have a |
rossatmsoe | 0:c435f76658b2 | 35 | //pointer to a struct and we want to access one of the members. |
rossatmsoe | 0:c435f76658b2 | 36 | void recordBP(struct patient_record *patient, int sys, int dias, float o2) { |
rossatmsoe | 0:c435f76658b2 | 37 | patient->systolic=sys; |
rossatmsoe | 0:c435f76658b2 | 38 | patient->diastolic=dias; |
rossatmsoe | 0:c435f76658b2 | 39 | patient->spO2 = o2; |
rossatmsoe | 0:c435f76658b2 | 40 | } |
rossatmsoe | 0:c435f76658b2 | 41 | |
rossatmsoe | 0:c435f76658b2 | 42 | //Fill in vital signs through the serial monitor (for BP) and the oxygen sensor, |
rossatmsoe | 0:c435f76658b2 | 43 | //assumed to be an analog input giving the SpO2 level as a fraction. |
rossatmsoe | 0:c435f76658b2 | 44 | void recordBPinteractive(struct patient_record *first_patient, AnalogIn oxygen) { |
rossatmsoe | 0:c435f76658b2 | 45 | int N; |
rossatmsoe | 0:c435f76658b2 | 46 | int s; |
rossatmsoe | 0:c435f76658b2 | 47 | int d; |
rossatmsoe | 0:c435f76658b2 | 48 | |
rossatmsoe | 0:c435f76658b2 | 49 | pc.printf("Enter the patient number:\n"); |
rossatmsoe | 0:c435f76658b2 | 50 | pc.scanf("%d",&N); |
rossatmsoe | 0:c435f76658b2 | 51 | pc.printf("Enter the systolic BP:\n"); |
rossatmsoe | 0:c435f76658b2 | 52 | pc.scanf("%d",&s); |
rossatmsoe | 0:c435f76658b2 | 53 | pc.printf("Enter the diastolic BP:\n"); |
rossatmsoe | 0:c435f76658b2 | 54 | pc.scanf("%d",&d); |
rossatmsoe | 0:c435f76658b2 | 55 | |
rossatmsoe | 0:c435f76658b2 | 56 | //Note that [N] turns first_patient into a struct rather than a |
rossatmsoe | 0:c435f76658b2 | 57 | //pointer to a struct (dereferences the pointer). Therefore, we |
rossatmsoe | 0:c435f76658b2 | 58 | //use . to access members. |
rossatmsoe | 0:c435f76658b2 | 59 | first_patient[N].systolic=s; |
rossatmsoe | 0:c435f76658b2 | 60 | first_patient[N].diastolic=d; |
rossatmsoe | 0:c435f76658b2 | 61 | first_patient[N].spO2=oxygen; |
rossatmsoe | 0:c435f76658b2 | 62 | |
rossatmsoe | 0:c435f76658b2 | 63 | //Print information to confirm |
rossatmsoe | 0:c435f76658b2 | 64 | pc.printf("Data entered:\n"); |
rossatmsoe | 0:c435f76658b2 | 65 | print_patient_record(first_patient,N); |
rossatmsoe | 0:c435f76658b2 | 66 | } |
rossatmsoe | 0:c435f76658b2 | 67 | |
rossatmsoe | 0:c435f76658b2 | 68 | //Look up the patient number by searching through the array of patients |
rossatmsoe | 0:c435f76658b2 | 69 | //and matching the first and last name. strcmp returns 0 if two strings |
rossatmsoe | 0:c435f76658b2 | 70 | //are identical. Return the patient's position in the array if name is |
rossatmsoe | 0:c435f76658b2 | 71 | //found, -1 if it is not found. |
rossatmsoe | 0:c435f76658b2 | 72 | int lookup_patient_name(struct patient_record *first_patient,char*first,char*last) |
rossatmsoe | 0:c435f76658b2 | 73 | { |
rossatmsoe | 0:c435f76658b2 | 74 | for(int k=0; k<MAX_PATIENTS; k++) { |
rossatmsoe | 0:c435f76658b2 | 75 | if(strcmp(first_patient[k].lastname,last) == 0) { |
rossatmsoe | 0:c435f76658b2 | 76 | if(strcmp(first_patient[k].firstname,first) == 0) { |
rossatmsoe | 0:c435f76658b2 | 77 | return k; |
rossatmsoe | 0:c435f76658b2 | 78 | } |
rossatmsoe | 0:c435f76658b2 | 79 | } |
rossatmsoe | 0:c435f76658b2 | 80 | } |
rossatmsoe | 0:c435f76658b2 | 81 | return -1; |
rossatmsoe | 0:c435f76658b2 | 82 | } |
rossatmsoe | 0:c435f76658b2 | 83 |