'------------------------------------------------------------------------------------------------
' EBAC Calculator v1.0 by George McGinn, APRIL, 2021
' Copyright (C)2021 by George McGinn
' All Rights Reserved.
'
' 6/19/2021 - Updated To use Zenity and SHELL commands to run on Linux with a simple GUI. (GJM)
'
' Original code designed to run as an App in iTunes to run specifically
' on an iPhone so those who would like to know their esitmate BAC and
' how long it will take to get to a level to drive safely and legally.
'
' Modified to run on the Desktop for use at home. You can judge the amount of drinks your guests
' have had and use this program to help in deciding who can drive and approximately how long
' someone needs to wait before they could possibly drive.
'
' *** There is no warranty (See Disclaimer) as to the fitness or accuracy of the results.
' *** While every effort has been made to provide accurate results, other factors not considered
' *** also play a role in determining one's Blood Alcohol Content.
'
' Blood alcohol content (BAC) can be estimated by a method developed by Swedish professor
' Erik Widmark in the 1920s.
'
' Gives the estimated BAC (EBAC), algorhithm reduced as:
' EBAC=A/(r*Wt)*1.055-B*T (verified by Wiki source listed below)
' Where:
' EBAC: Estimated Blood Alcohol Content (%)
' A: Alcohol consumed (in ounces to grams) or FLoz
' r: Ratio of body water to total weight (men: .68, women: .55)
' Wt: Body weight (in pounds to kilograms)
' B: Rate alcohol metabolized (men: .019/hr, women: .017/hr)
' T: Time alcohol in blood or time since consumption began
' 1.055: Constant value of density of blood
'
' The formula can also be reduced as follows (called the 8/10 formula):
' For Men: EBAC = 7.97*A/Wt-B*T
' For Women: EBAC = 9.86*A/Wt-B*T
'
' Factors 7.97 and 9.86 are derived from "r" and 1.055 among other factors.
' Also, this formula is geared to fluid ounces and pounds, not
' grams and kilograms. While the suggested values were 8 and 10,
' testing showed the results were not accurate. So I reworked it
' and found that 7.97 and 9.86 gives better results.
'
' It will be this reduced formula the program will use.
'
' Each shot, wine glass and bottle/can of beer contains .6oz of
' alcohol. User records the number of drinks, which will then be
' converted to ounces (drinks * .6)
'
' *** Formulas created/translated by George McGinn using the following sources:
' * Blood alcohol content can be estimated by a method developed
' by Swedish professor Erik Widmark [sv] in the 1920s.
' (https://web.archive.org/web/20031202155933/http://www.dui-law.com/810art.htm)
' * https://www.ou.edu/police/faid/blood-alcohol-calculator
' * https://www.gambonelaw.com/faqs/the-widmark-formula-and-calculating-your-bac-level/
' * https://en.wikipedia.org/wiki/Blood_alcohol_content
' * https://en.wikipedia.org/wiki/Binge_drinking
' * https://en.wikipedia.org/wiki/Alcohol_intoxication
' * https://en.wikipedia.org/wiki/Breathalyzer
' * https://ndaa.org/wp-content/uploads/toxicology_final.pdf
' * http://njlaw.rutgers.edu/collections/courts/supreme/a-96-06.doc.html
' * https://www.researchgate.net/profile/Alan-Jones-14/publication/318055507_Profiles_in_Forensic_Toxicology_Professor_Erik_Widmark_1889-1945/links/595794330f7e9ba95e0fd91d/Profiles-in-Forensic-Toxicology-Professor-Erik-Widmark-1889-1945.pdf?origin=publication_detail
' * https://www.cdc.gov/alcohol
'------------------------------------------------------------------------------------------------
'
' PROGRAM NOTES:
' --------------
' This blood alcohol content or BAC, for short, calculator can estimate
' your blood alcohol levels. Metabolism, body fat percentage and
' medication are some of the other factors that can affect the rate of absorption
' by the body, and these are not considered in this calculation.
'
' Blood alcohol content (BAC) or blood alcohol level is the
' concentration of alcohol in the bloodstream. It is usually measured
' as mass per volume. For example, a Blood alcohol content BAC of
' 0.04% means 0.4% (permille) or 0.04 grams of alcohol per 100 grams
' of individuals blood. Use this BAC Calculator for informational
' purposes only, and not to drink and drive or drink and work.
'
' Important Note: There is no BAC calculator that is 100% accurate.
' This is due to the number of factors that come into play regarding
' the consumption and alcohol processing rates of different people.
' Other factors not considered here include:
' * Overall health and wellness
' * Body type
' * Metabolism (Program uses an Average for Males & Females)
' * Alcohol tolerance
' * What kind of alcohol is consumed (Program uses average drink of 40% ETOH)
' * Medications
' * Amount of food eaten
' * Emotional, mental state
'
' The best that can be done is a rough estimation of the
' bloodstreams alcohol content or the BAC level based on known inputs.
'
' Every state in the U.S. has a legal Blood Alcohol (BAC) limit of 0.05% or 0.08%,
' (depending on the state you are driving in). Most states also have lower legal
' BAC limits for young and inexperienced drivers, professional drivers and commercial
' drivers. Sentences for drunk driving include imprisonment, large fines, lengthy drivers
' license suspension and/or revocation, house arrest, community service, DUI schools,
' alcohol treatment programs, vehicle forfeiture and ignition interlock restrictions.
'
'------------------------------------------------------------------------------------------------
' Program Copyright (C)2021 by George McGinn
' All Rights Reserved.
'
' EBAC Calculator by George McGinn is licensed under a Creative Commons
' Attribution-NonCommercial 4.0 International License.
'
' Full License Link: https://creativecommons.org/licenses/by-nc/4.0/
'
'
' You are free to (For non-commerical purposes only):
' Share - copy and redistribute the material in any medium or format.
' Adapt - remix, transform, and build upon the material.
' Attribution - You must give appropriate credit, provide a link to
' the license, and indicate if changes were made. You may do so in any
' reasonable manner, but not in any way that suggests the licensor
' endorses you or your use.
' Non Commercial - You may not use the material for commercial purposes.
'
' *** None of this code is considered in the Public Domain. Rights granted under CC 4.0
' are outlined as above and the disclaimer below:
'
' *** DISCLAIMER ***
' Unless otherwise separately undertaken by the Licensor, to the extent possible,
' the Licensor offers the Licensed Material as-is and as-available, and makes no
' representations or warranties of any kind concerning the Licensed Material, whether
' express, implied, statutory, or other. This includes, without limitation, warranties
' of title, merchantability, fitness for a particular purpose, non-infringement, absence
' of latent or other defects, accuracy, or the presence or absence of errors, whether or
' not known or discoverable. Where disclaimers of warranties are not allowed in full or
' in part, this disclaimer may not apply to You.
'
' To the extent possible, in no event will the Licensor be liable to You on any legal theory
' (including, without limitation, negligence) or otherwise for any direct, special, indirect,
' incidental, consequential, punitive, exemplary, or other losses, costs, expenses, or damages
' arising out of this Public License or use of the Licensed Material, even if the Licensor has
' been advised of the possibility of such losses, costs, expenses, or damages. Where a limitation
' of liability is not allowed in full or in part, this limitation may not apply to You.
'
' The disclaimer of warranties and limitation of liability provided above shall be interpreted
' in a manner that, to the extent possible, most closely approximates an absolute disclaimer and
' waiver of all liability.
'------------------------------------------------------------------------------------------------
'--------------------------------
'*** Initialize
'*** If You comment the line below out, you will see the [DEBUG] PRINT statements in the QB64 Window. You can add your own PRINT statements if needed
'--------------------------------
'*** Setup NUMERIC Check Table
numeric(I) = -1
EBACMain:
'--------------------------------
'*** Setup constant variables
A = 0
Wt = 0
B = .0
T = 0: St = 0
I = 0
Bdl = 1.055
OZ = .5
TRUE = -1: FALSE = 0
SOBER = FALSE
EBACForm:
'--------------------------------
'*** Produce the Input Form
stdout = ""
cmd
= "zenity --forms --title=" + CHR$(34) + "EBAC Blood Alcohol Calculator" + CHR$(34) + _
" --text=" + CHR$(34) + "Enter information about your friend." + CHR$(34) + _
" --add-entry=" + CHR$(34) + "Sex (M or F)" + CHR$(34) + _
" --add-entry=" + CHR$(34) + "Weight (lbs)" + CHR$(34) + _
" --add-entry=" + CHR$(34) + "Number of drinks" + CHR$(34) + _
" --add-entry=" + CHR$(34) + "Time (hrs) from first drink" + CHR$(34) + _
" --extra-button=HELP --extra-button=QUIT"
result = pipecom(cmd, stdout, stderr)
stdbutton
= LEFT$(stdout
, 4)
cmd = "zenity --text-info " + _
" --title=" + CHR$(34) + "HELP/ABOUT: EBAC Calculator" + CHR$(34) + _
" --filename=" + "EBACHelp.txt" + _
" --width=525 --height=600"
SHELL ("zenity --error --text=" + CHR$(34) + "Main Form did not load. Program Terminated." + CHR$(34) + " --width=175 --height=100")
'----------------------------------------------------------
'*** Populate QB64 variables from the form's stdout results
'--------------------------------------------------
'*** Get and Validate Sex Input
Sex
= MID$(stdout
, 1, INSTR(stdout
, "|") - 1) SHELL ("zenity --warning --text=" + CHR$(34) + "SEX must be either M or F." + CHR$(34) + " --width=175 --height=100")
'---------------------------------------------------
'*** Get and Validate Weight Input
stdout
= MID$(stdout
, INSTR(stdout
, "|") + 1) Weight
= MID$(stdout
, 1, INSTR(stdout
, "|") - 1) SHELL ("zenity --warning --text=" + CHR$(34) + "Weight must be numeric." + CHR$(34) + " --width=175 --height=100")
'-----------------------------------------------------
'*** Get and Validate Number of Drinks Input
stdout
= MID$(stdout
, INSTR(stdout
, "|") + 1) Drinks
= MID$(stdout
, 1, INSTR(stdout
, "|") - 1) SHELL ("zenity --warning --text=" + CHR$(34) + "Number of drinks must be numeric." + CHR$(34) + " --width=175 --height=100")
'-------------------------------------------------------------------
'*** Get and Validate Time Since First Drink (Time Elapsed) Input
TimeElapsed
= MID$(stdout
, INSTR(stdout
, "|") + 1) TimeElapsed$
= LEFT$(TimeElapsed
, l
- 1) IF ISNUMERIC
(TimeElapsed
) THEN SHELL ("zenity --warning --text=" + CHR$(34) + "Time since first drink must be numeric." + CHR$(34) + " --width=175 --height=100")
'--------------------------------------------------------------------------------------------------------
'*** Calculate the value of the Estimated Blood Alcohol Content (EBAC) and check for sucessful completion
result = calcEBAC
SHELL ("zenity --error --text=" + CHR$(34) + "Calulating the EBAC results failed. Program Terminated" + CHR$(34) + " --width=175 --height=100")
'---------------------------------------------------------------------------------------------------------------------------------------------
'*** Create a temp file for the Zenity Information Box and write out prt_text (This box cannot have text passed to it unless it is in a file)
PRINT #1, USING "ESTIMATED BLOOD ALCOHOL CONTENT (EBAC) in g/dL = #.###"; EBAC
displayEBAC:
'------------------------------------------------------------------
'*** Prepare the Zenity command and execute, check for good return
cmd = "zenity --text-info " + _
" --title=" + CHR$(34) + "EBAC Blood Alcohol Calculator" + CHR$(34) + _
" --width=525 --height=400" + _
" --filename=tempfile" + _
" --checkbox=" + CHR$(34) + "I agree that this program and its results are not legally binding." + CHR$(34)
result
= SHELL("zenity --warning --text=" + CHR$(34) + "You must agree to the terms of use. Please check the confirmation box." + CHR$(34) + " --width=175 --height=100")
result
= SHELL("zenity --question --text=" + CHR$(34) + "Do you want to run another calculation?" + CHR$(34) + " --width=175 --height=100")
endPROG:
result
= SHELL("zenity --info --text=" + CHR$(34) + "Thank You for using EBAC Calculator. Please Don't Drink and Drive." + CHR$(34) + " --width=175 --height=100")
'-------------------------------------------------------------
'*** Convert Drinks into Fluid Ounces of EtOH (Pure Alcohol).
'*** A is number of drinks. 1 drink is about .6 FLoz of alcohol
FLoz = A * OZ
'-----------------------------------------------------
'*** Set/calculate EBAC values based on Sex
B = .017
EBAC = 7.97 * FLoz / Wt - B * T
B = .019
EBAC = 9.86 * FLoz / Wt - B * T
'----------------------------------------------------------------------------------------------
'*** Populate the EBAC string with the EBAC value formatted to 3 decimal places for FORM output
' prt_text = "ESTIMATED BLOOD ALCOHOL CONTENT (EBAC) in g/dL = " + EBAC_ST$ + CHR$(10) + CHR$(10)
'-----------------------------------------------------------------------------------------
'*** Based on EBAC range values, populate the FORM output string with the approriate text
prt_text
= prt_text
+ "*** ALERT: CALL AN AMBULANCE, DEATH LIKELY" + CHR$(10) prt_text = prt_text + "Unconsious/coma, unresponsive, high likelihood of death. It is illegal to operate a motor vehicle at this level of intoxication in all states."
prt_text
= prt_text
+ "*** ALERT: CALL AN AMBULANCE, DEATH POSSIBLE" + CHR$(10) prt_text = prt_text + "Onset of coma, and possible death due to respiratory arrest. It is illegal to operate a motor vehicle at this level of intoxication in all states."
prt_text
= prt_text
+ "*** ALERT: CALL AN AMBULANCE, SEVERE ALCOHOL POISONING" + CHR$(10) prt_text = prt_text + " Coma is possible. This is the level of surgical anesthesia. It is illegal to operate a motor vehicle at this level of intoxication in all states."
prt_text
= prt_text
+ "*** ALERT: YOU ARE IN A DRUNKEN STUP0R, AT RISK TO PASSING OUT" + CHR$(10) prt_text = prt_text + "STUPOR. You have little comprehension of where you are. You may pass out suddenly and be difficult to awaken. It is illegal to operate a motor vehicle at this level of intoxication in all states."
prt_text
= prt_text
+ "*** ALERT: SEVERLY IMPAIRED - DRUNK ENOUGH TO CAUSE SEVERE INJURY/DEATH TO SELF" + CHR$(10) prt_text = prt_text + "All mental, physical and sensory functions are severely impaired. Increased risk of asphyxiation from choking on vomit and of seriously injuring yourself by falls or other accidents. It is illegal to operate a motor vehicle at this level of intoxication in all states."
prt_text
= prt_text
+ "YOU ARE EXTREMELY DRUNK" + CHR$(10) prt_text = prt_text + "Feeling dazed/confused or otherwise disoriented. May need help to stand/walk. If you injure yourself you may not feel the pain. Some people have nausea and vomiting at this level. The gag reflex is impaired and you can choke if you do vomit. Blackouts are likely at this level so you may not remember what has happened. It is illegal to operate a motor vehicle at this level of intoxication in all states."
prt_text
= prt_text
+ "YOUR ARE SEVERLY DRUNK - ENOUGH TO BECOME VERY SICK" + CHR$(10) prt_text
= prt_text
+ "Dysphoria predominates, nausea may appear. The drinker has the appearance of a 'sloppy drunk.' It is illegal to operate a motor vehicle at this level of intoxication in all states." + CHR$(10) + CHR$(10) prt_text = prt_text + "* Dysphoria: An emotional state of anxiety, depression, or unease."
prt_text
= prt_text
+ "YOU ARE VERY DRUNK - ENOUGH TO LOSE PHYSICAL & MENTAL CONTROL" + CHR$(10) prt_text
= prt_text
+ "Gross motor impairment and lack of physical control. Blurred vision and major loss of balance. Euphoria is reduced and dysphoria* is beginning to appear. Judgment and perception are severely impaired. It is illegal to operate a motor vehicle at this level of intoxication in all states." + CHR$(10) + CHR$(10) prt_text = prt_text + "* Dysphoria: An emotional state of anxiety, depression, or unease."
prt_text
= prt_text
+ "YOU ARE LEGALLY DRUNK" + CHR$(10) prt_text = prt_text + "Significant impairment of motor coordination and loss of good judgment. Speech may be slurred; balance, vision, reaction time and hearing will be impaired. Euphoria. It is illegal to operate a motor vehicle at this level of intoxication in all states."
prt_text
= prt_text
+ "YOU MAY BE LEGALLY DRUNK" + CHR$(10) prt_text
= prt_text
+ "Slight impairment of balance, speech, vision, reaction time, and hearing. Euphoria. Judgment and self-control are reduced, and caution, reason and memory are impaired (in some* states .08 is legally impaired and it is illegal to drive at this level). You will probably believe that you are functioning better than you really are." + CHR$(10) + CHR$(10) prt_text = prt_text + "(*** As of July, 2004 ALL states had passed .08 BAC Per Se Laws. The final one took effect in August of 2005.)"
prt_text
= prt_text
+ "YOU MAY BE LEGALLY BUZZED" + CHR$(10) prt_text = prt_text + "Feeling of well-being, relaxation, lower inhibitions, sensation of warmth. Euphoria. Some minor impairment of reasoning and memory, lowering of caution. Your behavior may become exaggerated and emotions intensified (Good emotions are better, bad emotions are worse)"
prt_text
= prt_text
+ "YOU MAY BE OK TO DRIVE, BUT IMPAIRMENT BEGINS" + CHR$(10) prt_text = prt_text + "No loss of coordination, slight euphoria and loss of shyness. Depressant effects are not apparent. Mildly relaxed and maybe a little lightheaded."
prt_text
= prt_text
+ "YOU ARE OK TO DRIVE" + CHR$(10)
'-----------------------------------------------------------
'*** Determine if Drunk (>.08 EBAC) and calculate:
'*** - When user will be less than .08
'*** - How long it will take to become completely sober
SOBER = FALSE
CEBAC = EBAC
st = T
T = T + 1
IF CEBAC
> .0799 THEN I
= I
+ 1
B = .017
CEBAC = 7.97 * FLoz / Wt - B * T
B = .019
CEBAC = 9.86 * FLoz / Wt - B * T
prt_text
= prt_text
+ CHR$(10) + CHR$(10) + "It will take about " + STR$(I
) + " hours from your last drink to be able to drive." + CHR$(10) legalToDrive = TRUE
prt_text
= prt_text
+ "It will take about " + STR$(T
- st
) + " hours from your last drink to be completely sober." SOBER = TRUE
'-----------------------------------------------------
'*** Numeric Check of a STRING
ISNUMERIC = TRUE
ISNUMERIC = FALSE
'$INCLUDE:'pipecomqb64.bas'